public
Created

runInThisContext fails to run within the context of the caller; it instead runs in root context

  • Download Gist
code.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var vm = process.binding('evals').Script;
var ctx = vm.createContext({vm:vm});
 
Object.foo = 1;
 
console.log('Object.foo ', Object.foo);
console.log('this Object.foo ', vm.runInThisContext("Object.foo"));
 
console.log('new Object.foo ', vm.runInNewContext(
'Object.foo=2; Object.foo', {vm:vm}));
console.log('new this Object.foo ',vm.runInNewContext(
'Object.foo=2; vm.runInThisContext("Object.foo")', {vm:vm}));
 
vm.runInContext('Object.foo = 2', ctx);
console.log('ctx Object.foo ', vm.runInContext(
'Object.foo', ctx));
console.log('ctx this Object.foo ', vm.runInContext(
'vm.runInThisContext("Object.foo")', ctx));
output.txt
1 2 3 4 5 6
Object.foo 1
this Object.foo 1
new Object.foo 2
new this Object.foo 1
ctx Object.foo 2
ctx this Object.foo 1

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.