Skip to content

Commit 24acd1f

Browse files
author
Cong Liu
committed
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
1 parent 65b297d commit 24acd1f

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/vm.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
'use strict';
22

33
const binding = process.binding('contextify');
4-
const Script = binding.ContextifyScript;
4+
const WeakScript = binding.ContextifyScript;
5+
6+
// Hold compiled script here to survival from GC for Node modules.
7+
// Otherwise the links from console will be invalid shortly.
8+
// See https://github.com/nwjs/nw.js/issues/4269
9+
const compiledScripts = new Set();
10+
11+
function Script(code, options) {
12+
var script = new WeakScript(code, options);
13+
compiledScripts.add(script);
14+
return script;
15+
}
516

617
// The binding provides a few useful primitives:
718
// - ContextifyScript(code, { filename = "evalmachine.anonymous",
@@ -13,7 +24,7 @@ const Script = binding.ContextifyScript;
1324
// - isContext(sandbox)
1425
// From this we build the entire documented API.
1526

16-
Script.prototype.runInNewContext = function(sandbox, options) {
27+
WeakScript.prototype.runInNewContext = function(sandbox, options) {
1728
var context = exports.createContext(sandbox);
1829
return this.runInContext(context, options);
1930
};

0 commit comments

Comments
 (0)