@@ -321,7 +321,7 @@ class ContextifyScript : ObjectWrap {
321321 }
322322
323323
324- // args: code, [filename ]
324+ // args: code, [options ]
325325 static void New (const FunctionCallbackInfo<Value>& args) {
326326 HandleScope scope (node_isolate);
327327
@@ -331,20 +331,25 @@ class ContextifyScript : ObjectWrap {
331331
332332 ContextifyScript *contextify_script = new ContextifyScript ();
333333 contextify_script->Wrap (args.Holder ());
334+
335+ TryCatch try_catch;
334336 Local<String> code = args[0 ]->ToString ();
335337 Local<String> filename = GetFilenameArg (args, 1 );
336- bool display_exception = GetDisplayArg (args, 2 );
338+ bool display_errors = GetDisplayErrorsArg (args, 1 );
339+ if (try_catch.HasCaught ()) {
340+ try_catch.ReThrow ();
341+ return ;
342+ }
337343
338344 Local<Context> context = Context::GetCurrent ();
339345 Context::Scope context_scope (context);
340346
341- TryCatch try_catch;
342-
343347 Local<Script> v8_script = Script::New (code, filename);
344348
345349 if (v8_script.IsEmpty ()) {
346- if (display_exception)
350+ if (display_errors) {
347351 DisplayExceptionLine (try_catch.Message ());
352+ }
348353 try_catch.ReThrow ();
349354 return ;
350355 }
@@ -358,42 +363,40 @@ class ContextifyScript : ObjectWrap {
358363 }
359364
360365
361- // args: [timeout ]
366+ // args: [options ]
362367 static void RunInThisContext (const FunctionCallbackInfo<Value>& args) {
363368 HandleScope scope (node_isolate);
364369
365370 // Assemble arguments
366371 TryCatch try_catch;
367372 uint64_t timeout = GetTimeoutArg (args, 0 );
373+ bool display_errors = GetDisplayErrorsArg (args, 0 );
368374 if (try_catch.HasCaught ()) {
369375 try_catch.ReThrow ();
370376 return ;
371377 }
372378
373- bool display_exception = GetDisplayArg (args, 1 );
374-
375379 // Do the eval within this context
376- EvalMachine (timeout, display_exception , args, try_catch);
380+ EvalMachine (timeout, display_errors , args, try_catch);
377381 }
378382
379- // args: sandbox, [timeout ]
383+ // args: sandbox, [options ]
380384 static void RunInContext (const FunctionCallbackInfo<Value>& args) {
381385 HandleScope scope (node_isolate);
382386
383387 // Assemble arguments
384388 TryCatch try_catch;
385389 if (!args[0 ]->IsObject ()) {
386- return ThrowTypeError (" sandbox argument must be an object." );
390+ return ThrowTypeError (" contextifiedSandbox argument must be an object." );
387391 }
388392 Local<Object> sandbox = args[0 ].As <Object>();
389- uint64_t timeout = GetTimeoutArg (args, 1 );
393+ int64_t timeout = GetTimeoutArg (args, 1 );
394+ bool display_errors = GetDisplayErrorsArg (args, 1 );
390395 if (try_catch.HasCaught ()) {
391396 try_catch.ReThrow ();
392397 return ;
393398 }
394399
395- bool display_exception = GetDisplayArg (args, 2 );
396-
397400 // Get the context from the sandbox
398401 Local<Context> context =
399402 ContextifyContext::V8ContextFromContextifiedSandbox (sandbox);
@@ -404,43 +407,76 @@ class ContextifyScript : ObjectWrap {
404407
405408 // Do the eval within the context
406409 Context::Scope context_scope (context);
407- EvalMachine (timeout, display_exception , args, try_catch);
410+ EvalMachine (timeout, display_errors , args, try_catch);
408411 }
409412
410413 static int64_t GetTimeoutArg (const FunctionCallbackInfo<Value>& args,
411414 const int i) {
412- if (args[i]->IsUndefined ()) {
413- return 0 ;
415+ if (args[i]->IsUndefined () || args[i]->IsString ()) {
416+ return -1 ;
417+ }
418+ if (!args[i]->IsObject ()) {
419+ ThrowTypeError (" options must be an object" );
420+ return -1 ;
421+ }
422+
423+ Local<String> key = FIXED_ONE_BYTE_STRING (node_isolate, " timeout" );
424+ Local<Value> value = args[i].As <Object>()->Get (key);
425+ if (value->IsUndefined ()) {
426+ return -1 ;
414427 }
428+ int64_t timeout = value->IntegerValue ();
415429
416- int64_t timeout = args[i]->IntegerValue ();
417- if (timeout < 0 ) {
430+ if (timeout <= 0 ) {
418431 ThrowRangeError (" timeout must be a positive number" );
432+ return -1 ;
419433 }
420434 return timeout;
421435 }
422436
423- static bool GetDisplayArg (const FunctionCallbackInfo<Value>& args,
424- const int i) {
425- bool display_exception = true ;
426437
427- if (args[i]->IsBoolean ())
428- display_exception = args[i]->BooleanValue ();
438+ static bool GetDisplayErrorsArg (const FunctionCallbackInfo<Value>& args,
439+ const int i) {
440+ if (args[i]->IsUndefined () || args[i]->IsString ()) {
441+ return true ;
442+ }
443+ if (!args[i]->IsObject ()) {
444+ ThrowTypeError (" options must be an object" );
445+ return false ;
446+ }
429447
430- return display_exception;
448+ Local<String> key = FIXED_ONE_BYTE_STRING (node_isolate, " displayErrors" );
449+ Local<Value> value = args[i].As <Object>()->Get (key);
450+
451+ return value->IsUndefined () ? true : value->BooleanValue ();
431452 }
432453
433454
434455 static Local<String> GetFilenameArg (const FunctionCallbackInfo<Value>& args,
435456 const int i) {
436- return !args[i]->IsUndefined ()
437- ? args[i]->ToString ()
438- : FIXED_ONE_BYTE_STRING (node_isolate, " evalmachine.<anonymous>" );
457+ Local<String> defaultFilename =
458+ FIXED_ONE_BYTE_STRING (node_isolate, " evalmachine.<anonymous>" );
459+
460+ if (args[i]->IsUndefined ()) {
461+ return defaultFilename;
462+ }
463+ if (args[i]->IsString ()) {
464+ return args[i].As <String>();
465+ }
466+ if (!args[i]->IsObject ()) {
467+ ThrowTypeError (" options must be an object" );
468+ return Local<String>();
469+ }
470+
471+ Local<String> key = FIXED_ONE_BYTE_STRING (node_isolate, " filename" );
472+ Local<Value> value = args[i].As <Object>()->Get (key);
473+
474+ return value->IsUndefined () ? defaultFilename : value->ToString ();
439475 }
440476
441477
442478 static void EvalMachine (const int64_t timeout,
443- const bool display_exception ,
479+ const bool display_errors ,
444480 const FunctionCallbackInfo<Value>& args,
445481 TryCatch& try_catch) {
446482 if (!ContextifyScript::InstanceOf (args.This ())) {
@@ -452,15 +488,9 @@ class ContextifyScript : ObjectWrap {
452488 ObjectWrap::Unwrap<ContextifyScript>(args.This ());
453489 Local<Script> script = PersistentToLocal (node_isolate,
454490 wrapped_script->script_ );
455- if (script.IsEmpty ()) {
456- if (display_exception)
457- DisplayExceptionLine (try_catch.Message ());
458- try_catch.ReThrow ();
459- return ;
460- }
461491
462492 Local<Value> result;
463- if (timeout) {
493+ if (timeout != - 1 ) {
464494 Watchdog wd (timeout);
465495 result = script->Run ();
466496 } else {
@@ -474,8 +504,9 @@ class ContextifyScript : ObjectWrap {
474504
475505 if (result.IsEmpty ()) {
476506 // Error occurred during execution of the script.
477- if (display_exception)
507+ if (display_errors) {
478508 DisplayExceptionLine (try_catch.Message ());
509+ }
479510 try_catch.ReThrow ();
480511 return ;
481512 }
0 commit comments