You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionmain(){varcode=` var foo = {m: 1}; function bar() { "use strict"; return foo; } `;eval(code);bar();// foo is not defined}main();
Node session:
> vm.runInNewContext(fs.readFileSync('/Users/dmitrys/test.js', 'utf-8'))
undefined:6
return foo;
^
ReferenceError: foo is not defined
at bar (eval at main (evalmachine.<anonymous>:13:8), <anonymous>:6:14)
at main (evalmachine.<anonymous>:15:3)
at evalmachine.<anonymous>:19:1
at ContextifyScript.Script.runInNewContext (vm.js:18:15)
at Object.exports.runInNewContext (vm.js:49:17)
at repl:1:4
at REPLServer.defaultEval (repl.js:154:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:308:12)
Also the code works if there is no "use strict" for foo.
How likely this can be fixed in soon time? Is there any ETA? We're blocked by this issue at the moment, and will have to restructure our tests to not to use eval(...) (we use it to evaluate a translated to JS code, and run it). Seems we don't have any alternative to eval in this case (since Function doesn't capture environment either).
@trevnorris, seems strict functions just don't capture the local scope at all (and see only global environment, similarly to the Function(...) functions). Notice that the first example works, since bar is global, and sees the global foo in first case. But once it's wrapped into the main function, the bar doesn't see foo anymore.
@targos, the --use-strict adds a Program-level strict mode? Then the bar function correctly won't be created in the caller's environment (in the environment of main).
However, we run the non-strict mode, and only the function bar itself is strict. The strictness of a function should not affect (per ES5) the environment which should be captured regardless.
OK, seems it's really Node/V8 generic evaluation issue, not limited to the vm module. Updated example and the outputs in the Node 0.8 (works), and Node 0.10 (doesn't work):
The ~/test.js:
functionmain(){varcode=["var foo = {m: 1};","","function bar() {"," 'use strict';"," console.log(foo);",// foo isn't captured in 0.10"}",].join('\n');eval(code);bar();// foo is not defined, since node 0.10, works in 0.8}main();
Tests:
dmitrys-mbp:~ dmitrys$ nvm use 0.8
Now using node v0.8.28 (npm v1.2.30)
dmitrys-mbp:~ dmitrys$ node ~/test.js
{ m: 1 }
dmitrys-mbp:~ dmitrys$ nvm use 0.10
Now using node v0.10.38 (npm v1.4.28)
dmitrys-mbp:~ dmitrys$ node ~/test.js
undefined:5
console.log(foo);
^
ReferenceError: foo is not defined
at bar (eval at main (/Users/dmitrys/test.js:12:8), <anonymous>:5:15)
at main (/Users/dmitrys/test.js:14:3)
at Object.<anonymous> (/Users/dmitrys/test.js:18:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:935:3
dmitrys-mbp:~ dmitrys$
Yeah, if this is related to nodejs/node-v0.x-archive#8721, it is more a Node issue than a V8 issue, as we were unable to reproduce the problem in D8 :-/. (I still believe the problem stems from a V8 bug though, given that it regressed in a V8 upgrade.)
Activity
domenic commentedon Jul 26, 2015
Possibly related: #548
This is almost certainly a V8 bug. @trevnorris tracked down #548 to V8s after 3.28.22 failing. I'll go see if there's any obvious culprit...
amasad commentedon Jul 27, 2015
could be related to: nodejs/node-v0.x-archive#8721?
DmitrySoshnikov commentedon Jul 27, 2015
How likely this can be fixed in soon time? Is there any ETA? We're blocked by this issue at the moment, and will have to restructure our tests to not to use
eval(...)(we use it to evaluate a translated to JS code, and run it). Seems we don't have any alternative toevalin this case (sinceFunctiondoesn't capture environment either).trevnorris commentedon Jul 27, 2015
@domenic this related to the fact we wrap all our scripts to prevent global variable pollution?
DmitrySoshnikov commentedon Jul 27, 2015
@trevnorris, seems strict functions just don't capture the local scope at all (and see only global environment, similarly to the
Function(...)functions). Notice that the first example works, sincebaris global, and sees the globalfooin first case. But once it's wrapped into themainfunction, thebardoesn't seefooanymore.targos commentedon Jul 29, 2015
I does not seem to be confined to the
vmmodule. I get the same error if I directly runnode test.jsEdit: and with
node --use-strict test.jsit changes toReferenceError: bar is not definedDmitrySoshnikov commentedon Jul 29, 2015
@targos, the
--use-strictadds a Program-level strict mode? Then thebarfunction correctly won't be created in the caller's environment (in the environment ofmain).However, we run the non-strict mode, and only the function
baritself is strict. The strictness of a function should not affect (per ES5) the environment which should be captured regardless.So I'd say it's still the issue of the
vmmodule.DmitrySoshnikov commentedon Jul 29, 2015
OK, seems it's really Node/V8 generic evaluation issue, not limited to the
vmmodule. Updated example and the outputs in the Node0.8(works), and Node0.10(doesn't work):The
~/test.js:Tests:
domenic commentedon Jul 29, 2015
Yeah, if this is related to nodejs/node-v0.x-archive#8721, it is more a Node issue than a V8 issue, as we were unable to reproduce the problem in D8 :-/. (I still believe the problem stems from a V8 bug though, given that it regressed in a V8 upgrade.)
DmitrySoshnikov commentedon Jul 29, 2015
Yeah, seems it's the same issue. Would still be great to get some ETA of fixing it.
ChALkeR commentedon Sep 17, 2015
#2245 (comment) works for me on 4.0.0.
#2245 (comment) works for me on 4.0.0 (both parts).
Could we close this?
targos commentedon Sep 17, 2015
The issue in nodejs/node-v0.x-archive#8721 is also fixed in 4.0.0
ide commentedon Nov 16, 2015
Is there a regression test for this (in Node or V8)? Just seems like a subtle enough bug that it's worth checking for in the test suite.
ChALkeR commentedon Nov 17, 2015
@ide Ah, adding a test and closing this would make sense.
jasnell commentedon Apr 2, 2016
A test was added in #5250 and this now appears to be working in master. closing.
Fix bug where `let` doesn't work in runInContext in old Node versions.
test: rename test-regress-nodejsGH-2245
test: name test files appropriately
test: name test files appropriately
test: name test files appropriately
test: name test files appropriately
test: name test files appropriately