From d932e802317f9f61bd10988189fa43ed03ad0f61 Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Mon, 17 Jul 2017 13:06:23 +0800 Subject: vm: support parsing a script in a specific context PR-URL: https://github.com/nodejs/node/pull/14888 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Eugene Ostroukhov --- src/node_contextify.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/node_contextify.cc') diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 792b1c4f80..c2037c4cbe 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -61,6 +61,7 @@ using v8::Script; using v8::ScriptCompiler; using v8::ScriptOrigin; using v8::String; +using v8::Symbol; using v8::TryCatch; using v8::Uint8Array; using v8::UnboundScript; @@ -531,6 +532,16 @@ class ContextifyScript : public BaseObject { target->Set(class_name, script_tmpl->GetFunction()); env->set_script_context_constructor_template(script_tmpl); + + Local parsing_context_symbol = + Symbol::New(env->isolate(), + FIXED_ONE_BYTE_STRING(env->isolate(), + "script parsing context")); + env->set_vm_parsing_context_symbol(parsing_context_symbol); + target->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "kParsingContext"), + parsing_context_symbol) + .FromJust(); } @@ -555,6 +566,7 @@ class ContextifyScript : public BaseObject { Maybe maybe_display_errors = GetDisplayErrorsArg(env, options); MaybeLocal cached_data_buf = GetCachedData(env, options); Maybe maybe_produce_cached_data = GetProduceCachedData(env, options); + MaybeLocal maybe_context = GetContext(env, options); if (try_catch.HasCaught()) { try_catch.ReThrow(); return; @@ -583,6 +595,8 @@ class ContextifyScript : public BaseObject { else if (produce_cached_data) compile_options = ScriptCompiler::kProduceCodeCache; + Context::Scope scope(maybe_context.FromMaybe(env->context())); + MaybeLocal v8_script = ScriptCompiler::CompileUnboundScript( env->isolate(), &source, @@ -935,6 +949,41 @@ class ContextifyScript : public BaseObject { return value->ToInteger(env->context()); } + static MaybeLocal GetContext(Environment* env, + Local options) { + if (!options->IsObject()) + return MaybeLocal(); + + MaybeLocal maybe_value = + options.As()->Get(env->context(), + env->vm_parsing_context_symbol()); + Local value; + if (!maybe_value.ToLocal(&value)) + return MaybeLocal(); + + if (!value->IsObject()) { + if (!value->IsNullOrUndefined()) { + env->ThrowTypeError( + "contextifiedSandbox argument must be an object."); + } + return MaybeLocal(); + } + + ContextifyContext* sandbox = + ContextifyContext::ContextFromContextifiedSandbox( + env, value.As()); + if (!sandbox) { + env->ThrowTypeError( + "sandbox argument must have been converted to a context."); + return MaybeLocal(); + } + + Local context = sandbox->context(); + if (context.IsEmpty()) + return MaybeLocal(); + return context; + } + static bool EvalMachine(Environment* env, const int64_t timeout, -- cgit v1.2.3