Skip to content

vm.runInContext Function Prototype Issue #56413

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).

  2. mathewgeola commented on Dec 31, 2024

    @mathewgeola
    Author

    Thank you for your response and explanation!

    I understand that functions created syntactically do not use the globals, including the Function constructor that I inject into the context. This indeed explains why my previous attempts have not been successful.

    To further address this issue, I am looking for a way to ensure that all functions defined within vm.runInContext, regardless of whether they are created syntactically or through other means, share the same Function.prototype. If there are any suggestions or alternative approaches to achieve this, I would greatly appreciate it.

    Thank you once again for your help and time!

  3. ljharb commented on Dec 31, 2024

    @ljharb
    SponsorMember

    There's no way to do that - Function.prototype is what's called "undeniable" which makes it unvirtualizable. In other words, you can't change its object identity.

    What's the overarching goal of this?

  4. Patrikoska1 commented on Jan 1, 2025

    @Patrikoska1

    😃

  5. github-actions commented on Jul 1, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  6. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jul 1, 2025
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.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions