diff --git a/eval.js b/eval.js index 76ad90f..8cd4e8f 100644 --- a/eval.js +++ b/eval.js @@ -5,9 +5,7 @@ var requireLike = require('require-like') function merge (a, b) { if (!a || !b) return a - // Include all non-enumerable variables, including console (v10+), - // process (v12+), URL, etc. - var keys = Object.getOwnPropertyNames(b) + var keys = Object.keys(b) for (var k, i = 0, n = keys.length; i < n; i++) { k = keys[i] a[k] = b[k] @@ -37,7 +35,19 @@ module.exports = function (content, filename, scope, includeGlobals) { var _filename = filename || module.parent.filename; if (includeGlobals) { + // Merge enumerable variables on `global` merge(sandbox, global) + // Merge all non-enumerable variables on `global`, including console (v10+), + // process (v12+), URL, etc. We first filter out anything that's already in + // the VM scope (i.e. those in the ES standard) so we don't have two copies + var vmGlobals = Object.getOwnPropertyNames( + new vm.Script("vmGlobals = globalThis").runInNewContext().vmGlobals + ) + Object.getOwnPropertyNames(global).forEach((name) => { + if (!vmGlobals.includes(name)) { + sandbox[name] = global[name] + } + }) sandbox.require = requireLike(_filename) }