-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Not really possible to use global symbols inside vm #11593
Comments
That's really more of a V8 question than a node.js question but the reason is efficiency. It's also a fundamental limitation that won't change.
vm.runInNewContext(
'var a = []; a.__proto__ = new Array(); a instanceof Array',
{ Array }); // true |
Hm, but there is no way to make outside Array be proto for all arrays created inside? |
No. Array literals are always instances of the native Array constructor. Same with object literals. |
This is one reason why > vm.runInNewContext("Array.isArray([])", { Array })
true
> vm.runInNewContext("Array.isArray([])")
true
> vm.runInNewContext("Array.isArray(a)", { a: [], Array })
true
> vm.runInNewContext("Array.isArray(a)", { a: [] })
true |
Yes, but Also, if I package code which does not expect to be run inside |
As a rule of thumb you should obtain references to globals from the other context and operate on those: const cx = vm.createContext();
const { Error: ThatError } = vm.runInContext('this', cx);
const e = vm.runInContext('new Error("boom")', cx);
console.log(e instanceof Error); // false
console.log(e instanceof ThatError); // true |
But things are still tricky. For example: const context = vm.createContext({Buffer: Buffer});
vm.runInContext('new Buffer(new ArrayBuffer(10))', context); Throws an error:
|
You probably need to upgrade your node, that was fixed a while ago. |
I used |
So in this issue it is suggested in this comment to pass global variables to the vm context if you want them shared:
But then arrays created inside fail:
So how can one share symbols between
vm
and outside context, fully? I would like thatArray
(andError
, buffers and others) all behave in the same way and that I can pass objects from inside to outside without issues.The text was updated successfully, but these errors were encountered: