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.runInContext Function Prototype Issue #56413

Open
mathewgeola opened this issue Dec 30, 2024 · 4 comments
Open

vm.runInContext Function Prototype Issue #56413

mathewgeola opened this issue Dec 30, 2024 · 4 comments
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@mathewgeola
Copy link

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

@mathewgeola mathewgeola added the feature request Issues that request new features to be added to Node.js. label Dec 30, 2024
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Dec 30, 2024
@ljharb
Copy link
Member

ljharb commented Dec 30, 2024

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

@ljharb ljharb marked this as a duplicate of #56412 Dec 30, 2024
@mathewgeola
Copy link
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!

@ljharb
Copy link
Member

ljharb commented Dec 31, 2024

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?

@Patrikoska1
Copy link

😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

3 participants