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

vm: implement isContext() directly in JS land with private symbol #51685

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
12 changes: 6 additions & 6 deletions lib/internal/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const {
const {
ContextifyScript,
compileFunction,
isContext: _isContext,
} = internalBinding('contextify');
const {
runInContext,
Expand All @@ -21,18 +20,19 @@ const {
} = internalBinding('symbols');
const {
validateFunction,
validateObject,
kValidateObjectAllowArray,
} = require('internal/validators');

const {
getOptionValue,
} = require('internal/options');
const {
privateSymbols: {
contextify_context_private_symbol,
},
} = internalBinding('util');

function isContext(object) {
validateObject(object, 'object', kValidateObjectAllowArray);

return _isContext(object);
return object[contextify_context_private_symbol] !== undefined;
}

function getHostDefinedOptionId(importModuleDynamically, hint) {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const {
TypeError,
} = primordials;

const { isContext } = internalBinding('contextify');
const {
isModuleNamespaceObject,
} = require('internal/util/types');
Expand Down Expand Up @@ -75,6 +74,8 @@ const kContext = Symbol('kContext');
const kPerContextModuleId = Symbol('kPerContextModuleId');
const kLink = Symbol('kLink');

const { isContext } = require('internal/vm');

class Module {
constructor(options) {
emitExperimentalWarning('VM Modules');
Expand Down
15 changes: 14 additions & 1 deletion lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const {
validateString,
validateStringArray,
validateUint32,
kValidateObjectAllowArray,
kValidateObjectAllowNullable,
} = require('internal/validators');
const {
Expand All @@ -59,14 +60,26 @@ const {
const {
getHostDefinedOptionId,
internalCompileFunction,
isContext,
isContext: _isContext,
registerImportModuleDynamically,
} = require('internal/vm');
const {
vm_dynamic_import_main_context_default,
} = internalBinding('symbols');
const kParsingContext = Symbol('script parsing context');

/**
* Check if object is a context object created by vm.createContext().
* @throws {TypeError} If object is not an object in the first place, throws TypeError.
* @param {object} object Object to check.
* @returns {boolean}
*/
function isContext(object) {
validateObject(object, 'object', kValidateObjectAllowArray);

return _isContext(object);
}

class Script extends ContextifyScript {
constructor(code, options = kEmptyObject) {
code = `${code}`;
Expand Down
16 changes: 0 additions & 16 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,15 +342,13 @@ void ContextifyContext::CreatePerIsolateProperties(
IsolateData* isolate_data, Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->isolate();
SetMethod(isolate, target, "makeContext", MakeContext);
SetMethod(isolate, target, "isContext", IsContext);
SetMethod(isolate, target, "compileFunction", CompileFunction);
SetMethod(isolate, target, "containsModuleSyntax", ContainsModuleSyntax);
}

void ContextifyContext::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(MakeContext);
registry->Register(IsContext);
registry->Register(CompileFunction);
registry->Register(ContainsModuleSyntax);
registry->Register(PropertyGetterCallback);
Expand Down Expand Up @@ -415,20 +413,6 @@ void ContextifyContext::MakeContext(const FunctionCallbackInfo<Value>& args) {
}
}


void ContextifyContext::IsContext(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[0]->IsObject());
Local<Object> sandbox = args[0].As<Object>();

Maybe<bool> result =
sandbox->HasPrivate(env->context(),
env->contextify_context_private_symbol());
args.GetReturnValue().Set(result.FromJust());
}


void ContextifyContext::WeakCallback(
const WeakCallbackInfo<ContextifyContext>& data) {
ContextifyContext* context = data.GetParameter();
Expand Down
Loading