Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "vm: support parsing a script in a specific context" #16136

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 9 additions & 32 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@

'use strict';

const {
ContextifyScript: Script,
kParsingContext,

makeContext,
isContext,
runInDebugContext
} = process.binding('contextify');
const binding = process.binding('contextify');
const Script = binding.ContextifyScript;

// The binding provides a few useful primitives:
// - Script(code, { filename = "evalmachine.anonymous",
Expand Down Expand Up @@ -68,11 +62,11 @@ Script.prototype.runInNewContext = function(sandbox, options) {
function createContext(sandbox) {
if (sandbox === undefined) {
sandbox = {};
} else if (isContext(sandbox)) {
} else if (binding.isContext(sandbox)) {
return sandbox;
}

makeContext(sandbox);
binding.makeContext(sandbox);
return sandbox;
}

Expand Down Expand Up @@ -105,33 +99,16 @@ function sigintHandlersWrap(fn, thisArg, argsArray) {
}
}

function runInDebugContext(code) {
return binding.runInDebugContext(code);
}

function runInContext(code, contextifiedSandbox, options) {
if (typeof options === 'string') {
options = {
filename: options,
[kParsingContext]: contextifiedSandbox
};
} else {
options = Object.assign({}, options, {
[kParsingContext]: contextifiedSandbox
});
}
return createScript(code, options)
.runInContext(contextifiedSandbox, options);
}

function runInNewContext(code, sandbox, options) {
sandbox = createContext(sandbox);
if (typeof options === 'string') {
options = {
filename: options,
[kParsingContext]: sandbox
};
} else {
options = Object.assign({}, options, {
[kParsingContext]: sandbox
});
}
return createScript(code, options).runInNewContext(sandbox, options);
}

Expand All @@ -147,5 +124,5 @@ module.exports = {
runInContext,
runInNewContext,
runInThisContext,
isContext
isContext: binding.isContext
};
1 change: 0 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ class ModuleWrap;
V(tls_wrap_constructor_function, v8::Function) \
V(tty_constructor_template, v8::FunctionTemplate) \
V(udp_constructor_function, v8::Function) \
V(vm_parsing_context_symbol, v8::Symbol) \
V(url_constructor_function, v8::Function) \
V(write_wrap_constructor_function, v8::Function) \

Expand Down
49 changes: 0 additions & 49 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ using v8::Script;
using v8::ScriptCompiler;
using v8::ScriptOrigin;
using v8::String;
using v8::Symbol;
using v8::TryCatch;
using v8::Uint8Array;
using v8::UnboundScript;
Expand Down Expand Up @@ -527,16 +526,6 @@ 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();
}


Expand All @@ -561,7 +550,6 @@ 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;
Expand Down Expand Up @@ -590,8 +578,6 @@ 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,
Expand Down Expand Up @@ -944,41 +930,6 @@ 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,
Expand Down