36 using v8::ArrayBuffer;
40 using v8::EscapableHandleScope;
43 using v8::FunctionCallbackInfo;
44 using v8::FunctionTemplate;
45 using v8::HandleScope;
52 using v8::NamedPropertyHandlerConfiguration;
55 using v8::ObjectTemplate;
57 using v8::PropertyAttribute;
58 using v8::PropertyCallbackInfo;
59 using v8::PropertyDescriptor;
61 using v8::ScriptCompiler;
62 using v8::ScriptOrigin;
66 using v8::UnboundScript;
68 using v8::WeakCallbackInfo;
72 class ContextifyContext {
76 enum { kSandboxObjectIndex = 1 };
82 ContextifyContext(Environment* env, Local<Object> sandbox_obj) : env_(env) {
83 Local<Context> v8_context = CreateV8Context(env, sandbox_obj);
84 context_.Reset(env->isolate(), v8_context);
87 if (context_.IsEmpty())
89 context_.SetWeak(
this, WeakCallback, v8::WeakCallbackType::kParameter);
90 context_.MarkIndependent();
94 ~ContextifyContext() {
99 inline Environment* env()
const {
104 inline Local<Context> context()
const {
105 return PersistentToLocal(env()->isolate(), context_);
109 inline Local<Object> global_proxy()
const {
110 return context()->Global();
114 inline Local<Object> sandbox()
const {
115 return Local<Object>::Cast(context()->GetEmbedderData(kSandboxObjectIndex));
139 void CopyProperties() {
140 HandleScope scope(env()->isolate());
142 Local<Context> context = PersistentToLocal(env()->isolate(), context_);
143 Local<Object> global =
144 context->Global()->GetPrototype()->ToObject(env()->isolate());
145 Local<Object> sandbox_obj = sandbox();
147 Local<Function> clone_property_method;
149 Local<Array> names = global->GetOwnPropertyNames();
150 int length = names->Length();
151 for (
int i = 0; i < length; i++) {
152 Local<String> key = names->Get(i)->ToString(env()->isolate());
153 Maybe<bool> has = sandbox_obj->HasOwnProperty(context, key);
159 if (!has.FromJust()) {
160 Local<Object> desc_vm_context =
161 global->GetOwnPropertyDescriptor(context, key)
162 .ToLocalChecked().As<Object>();
165 desc_vm_context->Has(context, env()->get_string()).FromJust() ||
166 desc_vm_context->Has(context, env()->set_string()).FromJust();
168 auto define_property_on_sandbox = [&] (PropertyDescriptor* desc) {
169 desc->set_configurable(desc_vm_context
170 ->Get(context, env()->configurable_string()).ToLocalChecked()
171 ->BooleanValue(context).FromJust());
172 desc->set_enumerable(desc_vm_context
173 ->Get(context, env()->enumerable_string()).ToLocalChecked()
174 ->BooleanValue(context).FromJust());
175 CHECK(sandbox_obj->DefineProperty(context, key, *desc).FromJust());
179 Local<Function>
get =
180 desc_vm_context->Get(context, env()->get_string())
181 .ToLocalChecked().As<Function>();
182 Local<Function>
set =
183 desc_vm_context->Get(context, env()->set_string())
184 .ToLocalChecked().As<Function>();
186 PropertyDescriptor desc(
get,
set);
187 define_property_on_sandbox(&desc);
190 desc_vm_context->Get(context, env()->value_string())
194 desc_vm_context->Get(context, env()->writable_string())
195 .ToLocalChecked()->BooleanValue(context).FromJust();
197 PropertyDescriptor desc(value, writable);
198 define_property_on_sandbox(&desc);
210 Local<Value> CreateDataWrapper(Environment* env) {
211 EscapableHandleScope scope(env->isolate());
212 Local<Object> wrapper =
213 env->script_data_constructor_function()
214 ->NewInstance(env->context()).FromMaybe(Local<Object>());
215 if (wrapper.IsEmpty())
219 return scope.Escape(wrapper);
223 Local<Context> CreateV8Context(Environment* env, Local<Object> sandbox_obj) {
224 EscapableHandleScope scope(env->isolate());
225 Local<FunctionTemplate> function_template =
227 function_template->SetHiddenPrototype(
true);
229 function_template->SetClassName(sandbox_obj->GetConstructorName());
231 Local<ObjectTemplate> object_template =
232 function_template->InstanceTemplate();
234 NamedPropertyHandlerConfiguration config(GlobalPropertyGetterCallback,
235 GlobalPropertySetterCallback,
236 GlobalPropertyQueryCallback,
237 GlobalPropertyDeleterCallback,
238 GlobalPropertyEnumeratorCallback,
239 CreateDataWrapper(env));
240 object_template->SetHandler(config);
242 Local<Context>
ctx =
Context::New(env->isolate(),
nullptr, object_template);
245 env->ThrowError(
"Could not instantiate context");
246 return Local<Context>();
249 ctx->SetSecurityToken(env->context()->GetSecurityToken());
257 ctx->SetEmbedderData(kSandboxObjectIndex, sandbox_obj);
258 sandbox_obj->SetPrivate(env->context(),
259 env->contextify_global_private_symbol(),
262 env->AssignToContext(ctx);
264 return scope.Escape(ctx);
268 static void Init(Environment* env, Local<Object> target) {
269 Local<FunctionTemplate> function_template =
271 function_template->InstanceTemplate()->SetInternalFieldCount(1);
272 env->set_script_data_constructor_function(function_template->GetFunction());
274 env->SetMethod(target,
"runInDebugContext", RunInDebugContext);
275 env->SetMethod(target,
"makeContext", MakeContext);
276 env->SetMethod(target,
"isContext", IsContext);
280 static void RunInDebugContext(
const FunctionCallbackInfo<Value>& args) {
281 Local<String> script_source(args[0]->ToString(args.GetIsolate()));
282 if (script_source.IsEmpty())
284 Local<Context> debug_context = Debug::GetDebugContext(args.GetIsolate());
285 Environment* env = Environment::GetCurrent(args);
286 if (debug_context.IsEmpty()) {
288 auto dummy_event_listener = [] (
const Debug::EventDetails&) {};
289 Debug::SetDebugEventListener(args.GetIsolate(), dummy_event_listener);
290 debug_context = Debug::GetDebugContext(args.GetIsolate());
291 CHECK(!debug_context.IsEmpty());
298 const int index = Environment::kContextEmbedderDataIndex;
299 debug_context->SetAlignedPointerInEmbedderData(index, env);
302 Context::Scope context_scope(debug_context);
303 MaybeLocal<Script>
script = Script::Compile(debug_context, script_source);
304 if (script.IsEmpty())
306 args.GetReturnValue().Set(script.ToLocalChecked()->Run());
310 static void MakeContext(
const FunctionCallbackInfo<Value>& args) {
311 Environment* env = Environment::GetCurrent(args);
313 if (!args[0]->IsObject()) {
314 return env->ThrowTypeError(
"sandbox argument must be an object.");
316 Local<Object> sandbox = args[0].As<Object>();
320 !sandbox->HasPrivate(
322 env->contextify_context_private_symbol()).FromJust());
324 TryCatch try_catch(env->isolate());
325 ContextifyContext* context =
new ContextifyContext(env, sandbox);
327 if (try_catch.HasCaught()) {
332 if (context->context().IsEmpty())
337 env->contextify_context_private_symbol(),
342 static void IsContext(
const FunctionCallbackInfo<Value>& args) {
343 Environment* env = Environment::GetCurrent(args);
345 if (!args[0]->IsObject()) {
346 env->ThrowTypeError(
"sandbox must be an object");
349 Local<Object> sandbox = args[0].As<Object>();
352 sandbox->HasPrivate(env->context(),
353 env->contextify_context_private_symbol());
354 args.GetReturnValue().Set(result.FromJust());
358 static void WeakCallback(
const WeakCallbackInfo<ContextifyContext>&
data) {
359 ContextifyContext* context = data.GetParameter();
364 static ContextifyContext* ContextFromContextifiedSandbox(
366 const Local<Object>& sandbox) {
367 MaybeLocal<Value> maybe_value =
368 sandbox->GetPrivate(env->context(),
369 env->contextify_context_private_symbol());
370 Local<Value> context_external_v;
371 if (maybe_value.ToLocal(&context_external_v) &&
372 context_external_v->IsExternal()) {
373 Local<External> context_external = context_external_v.As<External>();
374 return static_cast<ContextifyContext*
>(context_external->Value());
380 static void GlobalPropertyGetterCallback(
381 Local<Name> property,
382 const PropertyCallbackInfo<Value>& args) {
383 ContextifyContext*
ctx;
384 ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
387 if (ctx->context_.IsEmpty())
390 Local<Context> context = ctx->context();
391 Local<Object> sandbox = ctx->sandbox();
392 MaybeLocal<Value> maybe_rv =
393 sandbox->GetRealNamedProperty(context, property);
394 if (maybe_rv.IsEmpty()) {
396 ctx->global_proxy()->GetRealNamedProperty(context, property);
400 if (maybe_rv.ToLocal(&rv)) {
402 rv = ctx->global_proxy();
404 args.GetReturnValue().Set(rv);
409 static void GlobalPropertySetterCallback(
410 Local<Name> property,
412 const PropertyCallbackInfo<Value>& args) {
413 ContextifyContext*
ctx;
414 ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
417 if (ctx->context_.IsEmpty())
420 auto attributes = PropertyAttribute::None;
422 ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
426 static_cast<int>(attributes) &
427 static_cast<int>(PropertyAttribute::ReadOnly);
429 if (is_declared && read_only)
436 bool is_contextual_store = ctx->global_proxy() != args.This();
445 bool is_function = value->IsFunction();
447 if (!is_declared && args.ShouldThrowOnError() && is_contextual_store &&
451 ctx->sandbox()->Set(property, value);
455 static void GlobalPropertyQueryCallback(
456 Local<Name> property,
457 const PropertyCallbackInfo<Integer>& args) {
458 ContextifyContext*
ctx;
459 ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
462 if (ctx->context_.IsEmpty())
465 Local<Context> context = ctx->context();
466 Maybe<PropertyAttribute> maybe_prop_attr =
467 ctx->sandbox()->GetRealNamedPropertyAttributes(context, property);
469 if (maybe_prop_attr.IsNothing()) {
471 ctx->global_proxy()->GetRealNamedPropertyAttributes(context,
475 if (maybe_prop_attr.IsJust()) {
476 PropertyAttribute prop_attr = maybe_prop_attr.FromJust();
477 args.GetReturnValue().Set(prop_attr);
482 static void GlobalPropertyDeleterCallback(
483 Local<Name> property,
484 const PropertyCallbackInfo<Boolean>& args) {
485 ContextifyContext*
ctx;
486 ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
489 if (ctx->context_.IsEmpty())
492 Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), property);
494 if (success.FromMaybe(
false))
499 args.GetReturnValue().Set(
false);
503 static void GlobalPropertyEnumeratorCallback(
504 const PropertyCallbackInfo<Array>& args) {
505 ContextifyContext*
ctx;
506 ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Data().As<Object>());
509 if (ctx->context_.IsEmpty())
512 args.GetReturnValue().Set(ctx->sandbox()->GetPropertyNames());
516 class ContextifyScript :
public BaseObject {
518 Persistent<UnboundScript> script_;
521 static void Init(Environment* env, Local<Object> target) {
522 HandleScope scope(env->isolate());
523 Local<String> class_name =
524 FIXED_ONE_BYTE_STRING(env->isolate(),
"ContextifyScript");
526 Local<FunctionTemplate> script_tmpl = env->NewFunctionTemplate(
New);
527 script_tmpl->InstanceTemplate()->SetInternalFieldCount(1);
528 script_tmpl->SetClassName(class_name);
529 env->SetProtoMethod(script_tmpl,
"runInContext", RunInContext);
530 env->SetProtoMethod(script_tmpl,
"runInThisContext", RunInThisContext);
532 target->Set(class_name, script_tmpl->GetFunction());
533 env->set_script_context_constructor_template(script_tmpl);
538 static void New(
const FunctionCallbackInfo<Value>& args) {
539 Environment* env = Environment::GetCurrent(args);
541 if (!args.IsConstructCall()) {
542 return env->ThrowError(
"Must call vm.Script as a constructor.");
545 ContextifyScript* contextify_script =
546 new ContextifyScript(env, args.This());
548 TryCatch try_catch(env->isolate());
549 Local<String> code = args[0]->ToString(env->isolate());
551 Local<Value> options = args[1];
552 MaybeLocal<String> filename = GetFilenameArg(env, options);
553 MaybeLocal<Integer> lineOffset = GetLineOffsetArg(env, options);
554 MaybeLocal<Integer> columnOffset = GetColumnOffsetArg(env, options);
555 Maybe<bool> maybe_display_errors = GetDisplayErrorsArg(env, options);
556 MaybeLocal<Uint8Array> cached_data_buf = GetCachedData(env, options);
557 Maybe<bool> maybe_produce_cached_data = GetProduceCachedData(env, options);
558 if (try_catch.HasCaught()) {
563 bool display_errors = maybe_display_errors.ToChecked();
564 bool produce_cached_data = maybe_produce_cached_data.ToChecked();
566 ScriptCompiler::CachedData* cached_data =
nullptr;
567 Local<Uint8Array> ui8;
568 if (cached_data_buf.ToLocal(&ui8)) {
569 ArrayBuffer::Contents contents = ui8->Buffer()->GetContents();
570 cached_data =
new ScriptCompiler::CachedData(
571 static_cast<uint8_t*>(contents.Data()) + ui8->ByteOffset(),
575 ScriptOrigin origin(filename.ToLocalChecked(), lineOffset.ToLocalChecked(),
576 columnOffset.ToLocalChecked());
577 ScriptCompiler::Source
source(code, origin, cached_data);
578 ScriptCompiler::CompileOptions compile_options =
579 ScriptCompiler::kNoCompileOptions;
581 if (source.GetCachedData() !=
nullptr)
582 compile_options = ScriptCompiler::kConsumeCodeCache;
583 else if (produce_cached_data)
584 compile_options = ScriptCompiler::kProduceCodeCache;
586 MaybeLocal<UnboundScript> v8_script = ScriptCompiler::CompileUnboundScript(
591 if (v8_script.IsEmpty()) {
592 if (display_errors) {
593 DecorateErrorStack(env, try_catch);
598 contextify_script->script_.Reset(env->isolate(),
599 v8_script.ToLocalChecked());
601 if (compile_options == ScriptCompiler::kConsumeCodeCache) {
603 env->cached_data_rejected_string(),
604 Boolean::New(env->isolate(), source.GetCachedData()->rejected));
605 }
else if (compile_options == ScriptCompiler::kProduceCodeCache) {
606 const ScriptCompiler::CachedData* cached_data = source.GetCachedData();
607 bool cached_data_produced = cached_data !=
nullptr;
608 if (cached_data_produced) {
611 reinterpret_cast<const char*>(cached_data->data),
612 cached_data->length);
613 args.This()->Set(env->cached_data_string(), buf.ToLocalChecked());
616 env->cached_data_produced_string(),
622 static bool InstanceOf(Environment* env,
const Local<Value>& value) {
623 return !value.IsEmpty() &&
624 env->script_context_constructor_template()->HasInstance(value);
629 static void RunInThisContext(
const FunctionCallbackInfo<Value>& args) {
630 Environment* env = Environment::GetCurrent(args);
633 TryCatch try_catch(args.GetIsolate());
634 Maybe<int64_t> maybe_timeout = GetTimeoutArg(env, args[0]);
635 Maybe<bool> maybe_display_errors = GetDisplayErrorsArg(env, args[0]);
636 Maybe<bool> maybe_break_on_sigint = GetBreakOnSigintArg(env, args[0]);
637 if (try_catch.HasCaught()) {
642 int64_t timeout = maybe_timeout.ToChecked();
643 bool display_errors = maybe_display_errors.ToChecked();
644 bool break_on_sigint = maybe_break_on_sigint.ToChecked();
647 EvalMachine(env, timeout, display_errors, break_on_sigint, args,
652 static void RunInContext(
const FunctionCallbackInfo<Value>& args) {
653 Environment* env = Environment::GetCurrent(args);
657 bool break_on_sigint;
660 if (!args[0]->IsObject()) {
661 return env->ThrowTypeError(
662 "contextifiedSandbox argument must be an object.");
665 Local<Object> sandbox = args[0].As<Object>();
667 TryCatch try_catch(env->isolate());
668 Maybe<int64_t> maybe_timeout = GetTimeoutArg(env, args[1]);
669 Maybe<bool> maybe_display_errors = GetDisplayErrorsArg(env, args[1]);
670 Maybe<bool> maybe_break_on_sigint = GetBreakOnSigintArg(env, args[1]);
671 if (try_catch.HasCaught()) {
676 timeout = maybe_timeout.ToChecked();
677 display_errors = maybe_display_errors.ToChecked();
678 break_on_sigint = maybe_break_on_sigint.ToChecked();
682 ContextifyContext* contextify_context =
683 ContextifyContext::ContextFromContextifiedSandbox(env, sandbox);
684 if (contextify_context ==
nullptr) {
685 return env->ThrowTypeError(
686 "sandbox argument must have been converted to a context.");
689 if (contextify_context->context().IsEmpty())
693 TryCatch try_catch(env->isolate());
695 Context::Scope context_scope(contextify_context->context());
696 if (EvalMachine(contextify_context->env(),
702 contextify_context->CopyProperties();
705 if (try_catch.HasCaught()) {
712 static void DecorateErrorStack(Environment* env,
const TryCatch& try_catch) {
713 Local<Value> exception = try_catch.Exception();
715 if (!exception->IsObject())
718 Local<Object> err_obj = exception.As<Object>();
724 Local<Value> stack = err_obj->Get(env->stack_string());
725 MaybeLocal<Value> maybe_value =
728 env->arrow_message_private_symbol());
731 if (!(maybe_value.ToLocal(&arrow) && arrow->IsString())) {
735 if (stack.IsEmpty() || !stack->IsString()) {
739 Local<String> decorated_stack = String::Concat(
740 String::Concat(arrow.As<String>(),
741 FIXED_ONE_BYTE_STRING(env->isolate(),
"\n")),
743 err_obj->Set(env->stack_string(), decorated_stack);
746 env->decorated_private_symbol(),
747 True(env->isolate()));
750 static Maybe<bool> GetBreakOnSigintArg(Environment* env,
751 Local<Value> options) {
752 if (options->IsUndefined() || options->IsString()) {
755 if (!options->IsObject()) {
756 env->ThrowTypeError(
"options must be an object");
757 return Nothing<bool>();
760 Local<String> key = FIXED_ONE_BYTE_STRING(env->isolate(),
"breakOnSigint");
761 MaybeLocal<Value> maybe_value =
762 options.As<Object>()->Get(env->context(), key);
763 if (maybe_value.IsEmpty())
764 return Nothing<bool>();
766 Local<Value> value = maybe_value.ToLocalChecked();
767 return Just(value->IsTrue());
770 static Maybe<int64_t> GetTimeoutArg(Environment* env, Local<Value> options) {
771 if (options->IsUndefined() || options->IsString()) {
772 return Just<int64_t>(-1);
774 if (!options->IsObject()) {
775 env->ThrowTypeError(
"options must be an object");
776 return Nothing<int64_t>();
779 MaybeLocal<Value> maybe_value =
780 options.As<Object>()->Get(env->context(), env->timeout_string());
781 if (maybe_value.IsEmpty())
782 return Nothing<int64_t>();
784 Local<Value> value = maybe_value.ToLocalChecked();
785 if (value->IsUndefined()) {
786 return Just<int64_t>(-1);
789 Maybe<int64_t> timeout = value->IntegerValue(env->context());
791 if (timeout.IsJust() && timeout.ToChecked() <= 0) {
792 env->ThrowRangeError(
"timeout must be a positive number");
793 return Nothing<int64_t>();
800 static Maybe<bool> GetDisplayErrorsArg(Environment* env,
801 Local<Value> options) {
802 if (options->IsUndefined() || options->IsString()) {
805 if (!options->IsObject()) {
806 env->ThrowTypeError(
"options must be an object");
807 return Nothing<bool>();
810 Local<String> key = FIXED_ONE_BYTE_STRING(env->isolate(),
"displayErrors");
811 MaybeLocal<Value> maybe_value =
812 options.As<Object>()->Get(env->context(), key);
813 if (maybe_value.IsEmpty())
814 return Nothing<bool>();
816 Local<Value> value = maybe_value.ToLocalChecked();
817 if (value->IsUndefined())
820 return value->BooleanValue(env->context());
824 static MaybeLocal<String> GetFilenameArg(Environment* env,
825 Local<Value> options) {
826 Local<String> defaultFilename =
827 FIXED_ONE_BYTE_STRING(env->isolate(),
"evalmachine.<anonymous>");
829 if (options->IsUndefined()) {
830 return defaultFilename;
832 if (options->IsString()) {
833 return options.As<String>();
835 if (!options->IsObject()) {
836 env->ThrowTypeError(
"options must be an object");
837 return Local<String>();
840 Local<String> key = FIXED_ONE_BYTE_STRING(env->isolate(),
"filename");
841 MaybeLocal<Value> maybe_value =
842 options.As<Object>()->Get(env->context(), key);
843 if (maybe_value.IsEmpty())
844 return MaybeLocal<String>();
846 Local<Value> value = maybe_value.ToLocalChecked();
847 if (value->IsUndefined())
848 return defaultFilename;
849 return value->ToString(env->context());
853 static MaybeLocal<Uint8Array> GetCachedData(Environment* env,
854 Local<Value> options) {
855 if (!options->IsObject()) {
856 return MaybeLocal<Uint8Array>();
859 MaybeLocal<Value> maybe_value =
860 options.As<Object>()->Get(env->context(), env->cached_data_string());
861 if (maybe_value.IsEmpty())
862 return MaybeLocal<Uint8Array>();
864 Local<Value> value = maybe_value.ToLocalChecked();
865 if (value->IsUndefined()) {
866 return MaybeLocal<Uint8Array>();
869 if (!value->IsUint8Array()) {
870 env->ThrowTypeError(
"options.cachedData must be a Buffer instance");
871 return MaybeLocal<Uint8Array>();
874 return value.As<Uint8Array>();
878 static Maybe<bool> GetProduceCachedData(Environment* env,
879 Local<Value> options) {
880 if (!options->IsObject()) {
884 MaybeLocal<Value> maybe_value =
885 options.As<Object>()->Get(env->context(),
886 env->produce_cached_data_string());
887 if (maybe_value.IsEmpty())
888 return Nothing<bool>();
890 Local<Value> value = maybe_value.ToLocalChecked();
891 return Just(value->IsTrue());
895 static MaybeLocal<Integer> GetLineOffsetArg(Environment* env,
896 Local<Value> options) {
897 Local<Integer> defaultLineOffset =
Integer::New(env->isolate(), 0);
899 if (!options->IsObject()) {
900 return defaultLineOffset;
903 Local<String> key = FIXED_ONE_BYTE_STRING(env->isolate(),
"lineOffset");
904 MaybeLocal<Value> maybe_value =
905 options.As<Object>()->Get(env->context(), key);
906 if (maybe_value.IsEmpty())
907 return MaybeLocal<Integer>();
909 Local<Value> value = maybe_value.ToLocalChecked();
910 if (value->IsUndefined())
911 return defaultLineOffset;
913 return value->ToInteger(env->context());
917 static MaybeLocal<Integer> GetColumnOffsetArg(Environment* env,
918 Local<Value> options) {
919 Local<Integer> defaultColumnOffset =
Integer::New(env->isolate(), 0);
921 if (!options->IsObject()) {
922 return defaultColumnOffset;
925 Local<String> key = FIXED_ONE_BYTE_STRING(env->isolate(),
"columnOffset");
926 MaybeLocal<Value> maybe_value =
927 options.As<Object>()->Get(env->context(), key);
928 if (maybe_value.IsEmpty())
929 return MaybeLocal<Integer>();
931 Local<Value> value = maybe_value.ToLocalChecked();
932 if (value->IsUndefined())
933 return defaultColumnOffset;
935 return value->ToInteger(env->context());
939 static bool EvalMachine(Environment* env,
940 const int64_t timeout,
941 const bool display_errors,
942 const bool break_on_sigint,
943 const FunctionCallbackInfo<Value>& args,
944 TryCatch* try_catch) {
945 if (!ContextifyScript::InstanceOf(env, args.Holder())) {
947 "Script methods can only be called on script instances.");
951 ContextifyScript* wrapped_script;
952 ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(),
false);
953 Local<UnboundScript> unbound_script =
954 PersistentToLocal(env->isolate(), wrapped_script->script_);
955 Local<Script>
script = unbound_script->BindToCurrentContext();
958 bool timed_out =
false;
959 bool received_signal =
false;
960 if (break_on_sigint && timeout != -1) {
961 Watchdog wd(env->isolate(), timeout, &timed_out);
962 SigintWatchdog swd(env->isolate(), &received_signal);
963 result = script->Run();
964 }
else if (break_on_sigint) {
965 SigintWatchdog swd(env->isolate(), &received_signal);
966 result = script->Run();
967 }
else if (timeout != -1) {
968 Watchdog wd(env->isolate(), timeout, &timed_out);
969 result = script->Run();
971 result = script->Run();
974 if (timed_out || received_signal) {
979 env->ThrowError(
"Script execution timed out.");
980 }
else if (received_signal) {
981 env->ThrowError(
"Script execution interrupted.");
983 env->isolate()->CancelTerminateExecution();
986 if (try_catch->HasCaught()) {
987 if (!timed_out && !received_signal && display_errors) {
989 DecorateErrorStack(env, *try_catch);
997 try_catch->ReThrow();
1002 args.GetReturnValue().Set(result);
1007 ContextifyScript(Environment* env, Local<Object>
object)
1008 : BaseObject(env,
object) {
1009 MakeWeak<ContextifyScript>(
this);
1013 ~ContextifyScript()
override {
1019 void InitContextify(Local<Object> target,
1020 Local<Value> unused,
1021 Local<Context> context) {
1022 Environment* env = Environment::GetCurrent(context);
bool IsExceptionDecorated(Environment *env, Local< Value > er)
void AppendExceptionLine(Environment *env, Local< Value > er, Local< Message > message, enum ErrorHandlingMode mode)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
Persistent< Context > context_
union node::cares_wrap::@8::CaresAsyncData::@0 data
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
void Init(int *argc, const char **argv, int *exec_argc, const char ***exec_argv)
MaybeLocal< Object > Copy(Isolate *isolate, const char *data, size_t length)