Skip to content

vm.createContext creates more than described. #46976

Open
@axkibe

Description

@axkibe

Affected URL(s)

https://nodejs.org/api/vm.html#vmcreatecontextcontextobject-options

Description of the problem

See following test case:

const vm = require('node:vm');
const context = vm.createContext({ foo: console.log });

(async () => {
    const module = new vm.SourceTextModule(
    'foo( console ); console.log( "bar" );',
    { context: context, });
    await module.link(() => {});
    await module.evaluate();
})();

From basic understand as document in the API this should fail, as the "console" has never been handed down into the new context.. yet it is there. Albeit the log is not working.

I guess this is a documentation issue, as the standard javascript globals are created regardless? If I hand down the console object into the created context, console.log works, but if not IMO it should either throw a call to undefined function error, or as said, the docs should describe better what is actually happening on creating a new Context.

PS: I'm aware that nodes vm.SourceTextModule probably may not be properly sandboxed, in my case this doesn't matter, I just found it confusing, the "console" object exists at all in the new context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.v8 engineIssues and PRs related to the V8 dependency.vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions