-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
vm: introduce vm.Context #30709
vm: introduce vm.Context #30709
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,40 +151,44 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context( | |
Local<Object> sandbox_obj, | ||
const ContextOptions& options) { | ||
EscapableHandleScope scope(env->isolate()); | ||
Local<FunctionTemplate> function_template = | ||
FunctionTemplate::New(env->isolate()); | ||
|
||
function_template->SetClassName(sandbox_obj->GetConstructorName()); | ||
|
||
Local<ObjectTemplate> object_template = | ||
function_template->InstanceTemplate(); | ||
|
||
Local<Object> data_wrapper; | ||
if (!CreateDataWrapper(env).ToLocal(&data_wrapper)) | ||
return MaybeLocal<Context>(); | ||
|
||
NamedPropertyHandlerConfiguration config( | ||
PropertyGetterCallback, | ||
PropertySetterCallback, | ||
PropertyDescriptorCallback, | ||
PropertyDeleterCallback, | ||
PropertyEnumeratorCallback, | ||
PropertyDefinerCallback, | ||
data_wrapper, | ||
PropertyHandlerFlags::kHasNoSideEffect); | ||
|
||
IndexedPropertyHandlerConfiguration indexed_config( | ||
IndexedPropertyGetterCallback, | ||
IndexedPropertySetterCallback, | ||
IndexedPropertyDescriptorCallback, | ||
IndexedPropertyDeleterCallback, | ||
PropertyEnumeratorCallback, | ||
IndexedPropertyDefinerCallback, | ||
data_wrapper, | ||
PropertyHandlerFlags::kHasNoSideEffect); | ||
|
||
object_template->SetHandler(config); | ||
object_template->SetHandler(indexed_config); | ||
Local<ObjectTemplate> object_template; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why move this away from where it’s initialized? |
||
|
||
if (!sandbox_obj.IsEmpty()) { | ||
Local<FunctionTemplate> function_template = | ||
FunctionTemplate::New(env->isolate()); | ||
|
||
function_template->SetClassName(sandbox_obj->GetConstructorName()); | ||
|
||
object_template = function_template->InstanceTemplate(); | ||
|
||
Local<Object> data_wrapper; | ||
if (!CreateDataWrapper(env).ToLocal(&data_wrapper)) | ||
return MaybeLocal<Context>(); | ||
|
||
NamedPropertyHandlerConfiguration config( | ||
PropertyGetterCallback, | ||
PropertySetterCallback, | ||
PropertyDescriptorCallback, | ||
PropertyDeleterCallback, | ||
PropertyEnumeratorCallback, | ||
PropertyDefinerCallback, | ||
data_wrapper, | ||
PropertyHandlerFlags::kHasNoSideEffect); | ||
|
||
IndexedPropertyHandlerConfiguration indexed_config( | ||
IndexedPropertyGetterCallback, | ||
IndexedPropertySetterCallback, | ||
IndexedPropertyDescriptorCallback, | ||
IndexedPropertyDeleterCallback, | ||
PropertyEnumeratorCallback, | ||
IndexedPropertyDefinerCallback, | ||
data_wrapper, | ||
PropertyHandlerFlags::kHasNoSideEffect); | ||
|
||
object_template->SetHandler(config); | ||
object_template->SetHandler(indexed_config); | ||
} | ||
|
||
Local<Context> ctx = NewContext(env->isolate(), object_template); | ||
|
||
|
@@ -194,16 +198,18 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context( | |
|
||
ctx->SetSecurityToken(env->context()->GetSecurityToken()); | ||
|
||
// We need to tie the lifetime of the sandbox object with the lifetime of | ||
// newly created context. We do this by making them hold references to each | ||
// other. The context can directly hold a reference to the sandbox as an | ||
// embedder data field. However, we cannot hold a reference to a v8::Context | ||
// directly in an Object, we instead hold onto the new context's global | ||
// object instead (which then has a reference to the context). | ||
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj); | ||
sandbox_obj->SetPrivate(env->context(), | ||
env->contextify_global_private_symbol(), | ||
ctx->Global()); | ||
if (!sandbox_obj.IsEmpty()) { | ||
// We need to tie the lifetime of the sandbox object with the lifetime of | ||
// newly created context. We do this by making them hold references to each | ||
// other. The context can directly hold a reference to the sandbox as an | ||
// embedder data field. However, we cannot hold a reference to a v8::Context | ||
// directly in an Object, we instead hold onto the new context's global | ||
// object instead (which then has a reference to the context). | ||
ctx->SetEmbedderData(ContextEmbedderIndex::kSandboxObject, sandbox_obj); | ||
sandbox_obj->SetPrivate(env->context(), | ||
env->contextify_global_private_symbol(), | ||
ctx->Global()); | ||
} | ||
|
||
Utf8Value name_val(env->isolate(), options.name); | ||
ctx->AllowCodeGenerationFromStrings(options.allow_code_gen_strings->IsTrue()); | ||
|
@@ -236,19 +242,23 @@ void ContextifyContext::Init(Environment* env, Local<Object> target) { | |
} | ||
|
||
|
||
// makeContext(sandbox, name, origin, strings, wasm); | ||
// makeContext(sandbox, name, origin, strings, wasm, instance); | ||
void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
CHECK_EQ(args.Length(), 5); | ||
CHECK(args[0]->IsObject()); | ||
Local<Object> sandbox = args[0].As<Object>(); | ||
CHECK_EQ(args.Length(), 6); | ||
|
||
// Don't allow contextifying a sandbox multiple times. | ||
CHECK( | ||
!sandbox->HasPrivate( | ||
env->context(), | ||
env->contextify_context_private_symbol()).FromJust()); | ||
Local<Object> sandbox; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
if (args[5]->IsUndefined()) { | ||
CHECK(args[0]->IsObject()); | ||
sandbox = args[0].As<Object>(); | ||
|
||
// Don't allow contextifying a sandbox multiple times. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same check should apply to |
||
CHECK( | ||
!sandbox->HasPrivate( | ||
env->context(), | ||
env->contextify_context_private_symbol()).FromJust()); | ||
} | ||
|
||
ContextOptions options; | ||
|
||
|
@@ -278,7 +288,15 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) { | |
if (context_ptr->context().IsEmpty()) | ||
return; | ||
|
||
sandbox->SetPrivate( | ||
Local<Object> instance; | ||
if (args[5]->IsUndefined()) { | ||
instance = sandbox; | ||
} else { | ||
CHECK(args[5]->IsObject()); | ||
args.GetReturnValue().Set(context_ptr->global_proxy()); | ||
instance = args[5].As<Object>(); | ||
} | ||
instance->SetPrivate( | ||
env->context(), | ||
env->contextify_context_private_symbol(), | ||
External::New(env->isolate(), context_ptr.release())); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
// Acorn does not support private fields. | ||
// Verify that apilinks creates empty output | ||
// instead of crashing. | ||
|
||
class Foo { | ||
#a = 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { Context, Script } = require('vm'); | ||
|
||
[ | ||
true, 0, null, '', 'hi', | ||
Symbol('symbol'), | ||
{ name: 0 }, | ||
{ origin: 0 }, | ||
{ codeGeneration: 0 }, | ||
{ codeGeneration: { strings: 0 } }, | ||
{ codeGeneration: { wasm: 0 } }, | ||
].forEach((v) => { | ||
assert.throws(() => { | ||
new Context(v); | ||
}, { | ||
code: 'ERR_INVALID_ARG_TYPE', | ||
}); | ||
}); | ||
|
||
{ | ||
const ctx = new Context(); | ||
ctx.global.a = 1; | ||
const script = new Script('this'); | ||
assert.strictEqual(script.runInContext(ctx), ctx.global); | ||
} | ||
|
||
// https://github.com/nodejs/node/issues/31808 | ||
{ | ||
const ctx = new Context(); | ||
Object.defineProperty(ctx.global, 'x', { | ||
enumerable: true, | ||
configurable: true, | ||
get: common.mustNotCall(), | ||
set: common.mustNotCall(), | ||
}); | ||
const script = new Script('function x() {}'); | ||
script.runInContext(ctx); | ||
assert.strictEqual(typeof ctx.global.x, 'function'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually refers to the global proxy: https://github.com/nodejs/node/pull/30709/files#r392627951