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.runInNewContext doesn't raise a ReferenceError on console #35207

Closed
cdipiet opened this issue Sep 15, 2020 · 3 comments
Closed

vm.runInNewContext doesn't raise a ReferenceError on console #35207

cdipiet opened this issue Sep 15, 2020 · 3 comments
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers. vm Issues and PRs related to the vm subsystem.

Comments

@cdipiet
Copy link

cdipiet commented Sep 15, 2020

What steps will reproduce the bug?

from command line:
node -e "const vm=require('vm');vm.runInNewContext('console.log(\"test\")');"

from script:
vm.runInNewContext('console.log("test")');

How often does it reproduce? Is there a required condition?

always

What is the expected behavior?

ReferenceError: console is not defined

What do you see instead?

nothing is printed, ReferenceError is not raised, test is not printed

Additional information

console object seems to be accessibile from new context:

node -p "const vm=require('vm');vm.runInNewContext('typeof console');"
object

For example this works:
node -e "const vm=require('vm');vm.runInNewContext('let x = y;');"
evalmachine.:1
let x = y;
^
ReferenceError: y is not defined

@bnoordhuis
Copy link
Member

That's because console is a built-in object, like Object and Array, except the default implementation in a "plain" context only has no-op methods. Node.js overrides it in "node-y" contexts.

Closing as not-a-bug but let me know if you have follow-up questions and I'll move it to nodejs/help.

@bnoordhuis bnoordhuis added invalid Issues and PRs that are invalid. question Issues that look for answers. vm Issues and PRs related to the vm subsystem. labels Sep 15, 2020
@cdipiet
Copy link
Author

cdipiet commented Sep 15, 2020

Thank you, my question come from: https://stackoverflow.com/questions/39416716/what-is-the-difference-between-new-function-and-vm where it seems that in past version of node.js the behaviour was different. Thanks to your explanation I just found also: #16755. So it could be interesting having somewhere in the nodejs documentation some details on this point(may be doc on console class and vm module). Thanks

@bnoordhuis
Copy link
Member

What would you document? It's true console wasn't always a built-in but every version of Node.js where that isn't the case has been long out of support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Issues and PRs that are invalid. question Issues that look for answers. vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants