From 24acd1f7b4c66dfce595a7e98b5ebdeafbd97f58 Mon Sep 17 00:00:00 2001 From: Cong Liu Date: Mon, 28 Mar 2016 13:51:57 +0800 Subject: [PATCH] Fix broken links generated from Node modules in DevTools Compiled scripts from Node were set to weak, which caused the script object in V8 being GCed shortly. Hence links of the script shown in console of DevTools become invalid after GC. This patch saved compiled scripts from Node globally to survival from GC. Fixed nwjs/nw.js#4269 --- lib/vm.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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); };