-
Notifications
You must be signed in to change notification settings - Fork 7.3k
vm.runInContext calls to external functions, can't access the context passed to them. #1461
Comments
I am running into this issue too. Here are my steps to reproduce. It appears that references to the global scope cannot be passed down to functions (test 2, test 4), though local variables can (test 1, test 3). Is this by design? My use case here is that I would like to be able to pass an anonymous function as a parameter (ie. a callback for "somefunc"). The passed callback should have access to the global scope. var vm = require("vm");
var sandbox;
var makeSandbox = function(){
return {
somefunc: function(somevar){
somevar.foo = "bar";
}
};
};
sandbox = makeSandbox();
vm.runInNewContext("var test1 = {}; somefunc(test1);", sandbox);
console.log("test 1",sandbox);
// test 1 { somefunc: [Function], test1: { foo: 'bar' } }
sandbox = makeSandbox();
vm.runInNewContext("var test2 = this; somefunc(test2);", sandbox);
console.log("test 2",sandbox);
// test 2 { somefunc: [Function], test2: [Circular] }
sandbox = makeSandbox();
sandbox.somevar = {};
vm.runInNewContext("var test3 = somevar; somefunc(test3);", sandbox);
console.log("test 3",sandbox);
// test 3 { somefunc: [Function], somevar: { foo: 'bar' }, test3: { foo: 'bar' } }
sandbox = makeSandbox();
sandbox.somevar = sandbox;
vm.runInNewContext("var test4 = somevar; somefunc(test4);", sandbox);
console.log("test 4",sandbox);
// test 4 { somefunc: [Function], somevar: [Circular], test4: [Circular] } |
As documented in #1801, I believe I’ve closed this with elliottcable@cf21650. I’d appreciate if you’d clone and compile against that, and see if you can still reproduce this issue. (Many of these issues seem related.) |
I'm not sure I understand what the issue is about. The test from @rehanift prints the following with master:
Looks okay to me. Am I missing something? |
@bnoordhuis as discussed in #1389, this seems to be the expected functionality for sandbox = makeSandbox();
vm.runInNewContext('somefunc(this)', sandbox);
console.log(sandbox);
// { somefunc: [Function] } Since |
It's not a bug, it's a feature! |
Shouldn't this be closed? |
Yeah, I think so. |
it looks like a bug. maybe in v8.
maybe something related to locks or to checking the context
pinpointed:
full thing,
displays global in each step:
The text was updated successfully, but these errors were encountered: