Closed
Description
Evaluated strict functions, when ran inside vm.ranInContext
do not capture environment.
This works (file test.js
):
var code = `
var foo = {m: 1};
function bar() {
"use strict";
return foo;
}
`;
eval(code);
bar();
Node session:
> vm.runInNewContext(fs.readFileSync('/Users/dmitrys/test.js', 'utf-8'))
{ m: 1 }
>
This doesn't:
function main() {
var code = `
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
.