This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
vm misses const definitions after recursive loads (only in v0.11.15) #9084
Closed
Description
We have a system configured with scripts in a DSL that is loaded with vm module.
This DSL have a "include" action so that a script can load other scripts.
We use "const" to declare constants in the script.
With Node.js upto 0.11.14 everything behaves as expected.
But with 0.11.15, something strange happens with const declarations:
consts before the first include behave as usual,
but consts after the first include disappear and we got an undefined.
We have simplified the DSL to the following program:
//------------------------------------------------------------
// load-conf.js
var vm = require('vm');
var fs = require('fs');
function include(script) {
vm.runInContext(fs.readFileSync(script), ctxt, script);
}
var sandbox = {
include: include,
console: console
};
var ctxt = vm.createContext(sandbox);
for (var i = 2; i < process.argv.length; i++) include(process.argv[i]);
The following configuration scripts can be used to test the problem.
//------------------------------------------------------------
// const-root.conf
console.info("const-root.conf");
function f0(x) {return x;}
const x0 = 10;
var y0 = 20;
console.info("Root before include (0):", f0, x0, y0);
include("const-included.conf");
function f2(x) {return x;}
const x2 = 10;
var y2 = 20;
console.info("Root after include (0):", f0, x0, y0);
console.info("Root after include (2):", f2, x2, y2);
//------------------------------------------------------------
// const-included.conf
console.info("const-included.conf");
function f1(x) {return x;}
const x1 = 10;
var y1 = 20;
console.info("Included (1):", f1, x1, y1);
When executed with node-v0.10.35 or node-v0.11.14, we get the following output:
$ node-v0.11.14/node load-conf.js const-root.conf
const-root.conf
Root before include (0): function f0(x) {return x;} 10 20
const-included.conf
Included (1): function f1(x) {return x;} 10 20
Root after include (0): function f0(x) {return x;} 10 20
Root after include (2): function f2(x) {return x;} 10 20
But when executed with node-0.11.15, we get:
$ node-v0.11.15/node load-conf.js const-root.conf
const-root.conf
Root before include (0): function f0(x) {return x;} 10 20
const-included.conf
Included (1): function f1(x) {return x;} 10 20
Root after include (0): function f0(x) {return x;} 10 20
Root after include (2): function f2(x) {return x;} undefined 20
Note the undefined
in the last line of the output!