vm: support parsing a script in a specific context · nodejs/node@7d95dc3 · GitHub
Skip to content

Commit 7d95dc3

Browse files
TimothyGujasnell
authored andcommitted
vm: support parsing a script in a specific context
PR-URL: #14888 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
1 parent f0a741d commit 7d95dc3

4 files changed

Lines changed: 183 additions & 9 deletions

File tree

lib/vm.js

Lines changed: 32 additions & 9 deletions

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ namespace node {
316316
V(tls_wrap_constructor_function, v8::Function) \
317317
V(tty_constructor_template, v8::FunctionTemplate) \
318318
V(udp_constructor_function, v8::Function) \
319+
V(vm_parsing_context_symbol, v8::Symbol) \
319320
V(url_constructor_function, v8::Function) \
320321
V(write_wrap_constructor_function, v8::Function) \
321322

src/node_contextify.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ using v8::Script;
6161
using v8::ScriptCompiler;
6262
using v8::ScriptOrigin;
6363
using v8::String;
64+
using v8::Symbol;
6465
using v8::TryCatch;
6566
using v8::Uint8Array;
6667
using v8::UnboundScript;
@@ -531,6 +532,16 @@ class ContextifyScript : public BaseObject {
531532

532533
target->Set(class_name, script_tmpl->GetFunction());
533534
env->set_script_context_constructor_template(script_tmpl);
535+
536+
Local<Symbol> parsing_context_symbol =
537+
Symbol::New(env->isolate(),
538+
FIXED_ONE_BYTE_STRING(env->isolate(),
539+
"script parsing context"));
540+
env->set_vm_parsing_context_symbol(parsing_context_symbol);
541+
target->Set(env->context(),
542+
FIXED_ONE_BYTE_STRING(env->isolate(), "kParsingContext"),
543+
parsing_context_symbol)
544+
.FromJust();
534545
}
535546

536547

@@ -555,6 +566,7 @@ class ContextifyScript : public BaseObject {
555566
Maybe<bool> maybe_display_errors = GetDisplayErrorsArg(env, options);
556567
MaybeLocal<Uint8Array> cached_data_buf = GetCachedData(env, options);
557568
Maybe<bool> maybe_produce_cached_data = GetProduceCachedData(env, options);
569+
MaybeLocal<Context> maybe_context = GetContext(env, options);
558570
if (try_catch.HasCaught()) {
559571
try_catch.ReThrow();
560572
return;
@@ -583,6 +595,8 @@ class ContextifyScript : public BaseObject {
583595
else if (produce_cached_data)
584596
compile_options = ScriptCompiler::kProduceCodeCache;
585597

598+
Context::Scope scope(maybe_context.FromMaybe(env->context()));
599+
586600
MaybeLocal<UnboundScript> v8_script = ScriptCompiler::CompileUnboundScript(
587601
env->isolate(),
588602
&source,
@@ -935,6 +949,41 @@ class ContextifyScript : public BaseObject {
935949
return value->ToInteger(env->context());
936950
}
937951

952+
static MaybeLocal<Context> GetContext(Environment* env,
953+
Local<Value> options) {
954+
if (!options->IsObject())
955+
return MaybeLocal<Context>();
956+
957+
MaybeLocal<Value> maybe_value =
958+
options.As<Object>()->Get(env->context(),
959+
env->vm_parsing_context_symbol());
960+
Local<Value> value;
961+
if (!maybe_value.ToLocal(&value))
962+
return MaybeLocal<Context>();
963+
964+
if (!value->IsObject()) {
965+
if (!value->IsNullOrUndefined()) {
966+
env->ThrowTypeError(
967+
"contextifiedSandbox argument must be an object.");
968+
}
969+
return MaybeLocal<Context>();
970+
}
971+
972+
ContextifyContext* sandbox =
973+
ContextifyContext::ContextFromContextifiedSandbox(
974+
env, value.As<Object>());
975+
if (!sandbox) {
976+
env->ThrowTypeError(
977+
"sandbox argument must have been converted to a context.");
978+
return MaybeLocal<Context>();
979+
}
980+
981+
Local<Context> context = sandbox->context();
982+
if (context.IsEmpty())
983+
return MaybeLocal<Context>();
984+
return context;
985+
}
986+
938987

939988
static bool EvalMachine(Environment* env,
940989
const int64_t timeout,
Lines changed: 101 additions & 0 deletions

0 commit comments

Comments
 (0)