Open
Description
Node version: v10.13.0
If I run vm.script with "cachedData" option, the filename does not appear with the error.
What I hope is when I run cachedData and get JS error, I can locate the filename
Code
const vm = require('vm')
const readSourceHash = function (bytecodeBuffer) {
let sum = 0;
bytecodeBuffer.slice(8, 12).forEach((number, power) => sum += number * Math.pow(256, power));
return sum
};
const code = 'console.log(a);';
const cachedData = new vm.Script(code).createCachedData();
const s = new vm.Script(code, {
filename: "main.js"
});
try {
console.log(`Run without cachedData:`);
console.log(s.runInNewContext());
} catch(e) {console.log(e);console.log('\n')}
let length = readSourceHash(cachedData);
let dummyCode = "";
if (length > 1) {
dummyCode = '"' + "\u200b".repeat(length - 2) + '"'; // "\u200b" Zero width space
}
try {
console.log(`Run with cachedData:`);
const s = new vm.Script(dummyCode, {
cachedData,
filename: "main.js"
})
console.log(s.runInNewContext());
}catch(e) {console.log(e)}
try {
console.log(`Run with cachedData2:`);
const s = new vm.Script(code, {
cachedData,
filename: "main.js"
})
console.log(s.runInNewContext());
}catch(e) {console.log(e)}
Output
Run without cachedData:
main.js:1
console.log(a);
^
ReferenceError: a is not defined
at main.js:1:13
at Script.runInContext (vm.js:107:20)
at Script.runInNewContext (vm.js:113:17)
at Object.<anonymous> (/Users/scripts/bytecode/test2.js:18:17)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
Run with cachedData:
evalmachine.<anonymous>:1
""
^
ReferenceError: a is not defined
at evalmachine.<anonymous>:1:13
at Script.runInContext (vm.js:107:20)
at Script.runInNewContext (vm.js:113:17)
at Object.<anonymous> (/Users/scripts/bytecode/test2.js:33:17)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
Run with cachedData2:
evalmachine.<anonymous>:1
console.log(a);
^
ReferenceError: a is not defined
at evalmachine.<anonymous>:1:13
at Script.runInContext (vm.js:107:20)
at Script.runInNewContext (vm.js:113:17)
at Object.<anonymous> (/Users/scripts/bytecode/test2.js:42:17)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)