diff --git a/lib/vm.js b/lib/vm.js index b4a2b999909..6d337de2bdd 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -1,7 +1,18 @@ 'use strict'; const binding = process.binding('contextify'); -const Script = binding.ContextifyScript; +const WeakScript = binding.ContextifyScript; + +// Hold compiled script here to survival from GC for Node modules. +// Otherwise the links from console will be invalid shortly. +// See https://github.com/nwjs/nw.js/issues/4269 +const compiledScripts = new Set(); + +function Script(code, options) { + var script = new WeakScript(code, options); + compiledScripts.add(script); + return script; +} // The binding provides a few useful primitives: // - ContextifyScript(code, { filename = "evalmachine.anonymous", @@ -13,7 +24,7 @@ const Script = binding.ContextifyScript; // - isContext(sandbox) // From this we build the entire documented API. -Script.prototype.runInNewContext = function(sandbox, options) { +WeakScript.prototype.runInNewContext = function(sandbox, options) { var context = exports.createContext(sandbox); return this.runInContext(context, options); };