diff options
author | Timothy Gu <timothygu99@gmail.com> | 2017-07-17 13:06:23 +0800 |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2017-09-05 10:47:42 +0800 |
commit | d932e802317f9f61bd10988189fa43ed03ad0f61 (patch) | |
tree | 60ced59b8a4a75ef4b2d670ee7cf5ddf834a5799 /src/node_contextify.cc | |
parent | 86e7c61a070d056daec9a756b86aece9ec30c2a8 (diff) | |
download | android-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.tar.gz android-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.tar.bz2 android-node-v8-d932e802317f9f61bd10988189fa43ed03ad0f61.zip |
vm: support parsing a script in a specific context
PR-URL: https://github.com/nodejs/node/pull/14888
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Diffstat (limited to 'src/node_contextify.cc')
-rw-r--r-- | src/node_contextify.cc | 49 |
1 files changed, 49 insertions, 0 deletions
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<Symbol> 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<bool> maybe_display_errors = GetDisplayErrorsArg(env, options); MaybeLocal<Uint8Array> cached_data_buf = GetCachedData(env, options); Maybe<bool> maybe_produce_cached_data = GetProduceCachedData(env, options); + MaybeLocal<Context> 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<UnboundScript> v8_script = ScriptCompiler::CompileUnboundScript( env->isolate(), &source, @@ -935,6 +949,41 @@ class ContextifyScript : public BaseObject { return value->ToInteger(env->context()); } + static MaybeLocal<Context> GetContext(Environment* env, + Local<Value> options) { + if (!options->IsObject()) + return MaybeLocal<Context>(); + + MaybeLocal<Value> maybe_value = + options.As<Object>()->Get(env->context(), + env->vm_parsing_context_symbol()); + Local<Value> value; + if (!maybe_value.ToLocal(&value)) + return MaybeLocal<Context>(); + + if (!value->IsObject()) { + if (!value->IsNullOrUndefined()) { + env->ThrowTypeError( + "contextifiedSandbox argument must be an object."); + } + return MaybeLocal<Context>(); + } + + ContextifyContext* sandbox = + ContextifyContext::ContextFromContextifiedSandbox( + env, value.As<Object>()); + if (!sandbox) { + env->ThrowTypeError( + "sandbox argument must have been converted to a context."); + return MaybeLocal<Context>(); + } + + Local<Context> context = sandbox->context(); + if (context.IsEmpty()) + return MaybeLocal<Context>(); + return context; + } + static bool EvalMachine(Environment* env, const int64_t timeout, |