Skip to content

vm.runInContext Function Prototype Issue #56412

Closed as duplicate
Closed as duplicate

Description

@mathewgeola

What is the problem this feature will solve?

I'm having an issue with the vm module in Node.js.The function prototypes defined within a script using vm.runInContext do not match Function.prototype.

const vm = require('vm');

const context = vm.createContext({
    console,
    Function: Function
});

const script = new vm.Script(`
    function test() {
        return "Hello, World!";
    }
    console.log(test.prototype === Function.prototype); // Expected: true, Actual: false
`);

script.runInContext(context);

What is the feature you are proposing to solve the problem?

Tried using proxies, manual prototype setting, custom constructors, but none worked.

What alternatives have you considered?

No response

Activity

  1. ljharb commented on Dec 30, 2024

    @ljharb
    SponsorMember

    This is expected, because things created syntactically (like test) don't use globals (like your injected Function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions