require('vm') runInNewContext - Bug in treating objects #1518
Comments
No. Each context has distinct root objects. That is, It's similar to how in Java a class Foo loaded with classloader A is distinct from that same class loaded with classloader B. |
hi, |
You probably want |
I couldn't find documentation about createcontext and runincontext.....also I couldn't understand what runinthscontext means.....what Thanks. |
|
I saw that. What was not explained is, what "this" in the runInThisContext, how can i pass come context information to the code that will be executed. With runInNewContext - it was possible using the sandbox. Is there a way to same some context with runInThisContext. |
var x = 42
console.error(require('vm').runInThisContext('x'))
// prints "42" Please submit a pull request if the documentation has room for improvement. |
Hey, thanks for help me out. I tried that, and it doesn't work like that. For the above code, i get an error "x is not defined". That's why i kept coming back for an explanation. |
The example above doesn't work in the REPL or if |
I ran it as standalone script. May be in my version this is not supported yet. |
Hi,
I see a problem in the way objects are handled between the current context and the vm context.
I can explain better with the following example what is happening :
var vm = require('vm');
var val = {};
console.log('Outside the VM');
console.log('Result - ' + (val.constructor == Object));
console.log('JSON conversion Result - ' + (JSON.parse(JSON.stringify(val)).constructor == Object));
var val = vm.runInNewContext("var func = function(){ var val = { }; return val;}; func();", {});
console.log('Return value from VM');
console.log('Result - ' + (val.constructor == Object));
console.log('JSON conversion Result - ' + (JSON.parse(JSON.stringify(val)).constructor == Object));
The results for the above code is :
Outside the VM
Result - true
JSON conversion Result - true
Return value from VM
Result - false
JSON conversion Result - true
The first two results show the comparison in the current context. The object is treated the same way, no issues.
The next two results show an object return from running inside the vm and observe the first equality where it fails. But when you use the conversion (stringify <-> parse) and compare the results, they become equal.
Is this a bug in the execution context of the vm or can some one explain what i am doing wrong ?
Thanks.
The text was updated successfully, but these errors were encountered: