Skip to content

Commit cdd8898

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. The patch also transformed the filename representation of Node module into `file://` protocol. This fixed links of scripts shown in DevTools that are not loaded in current context. Fixed nwjs/nw.js#4269
1 parent 5c8f26b commit cdd8898

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

lib/vm.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
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 fixFilename(filename) {
12+
return filename = 'file://' + encodeURI(filename.replace(/\\/g, '/'));
13+
}
14+
15+
function Script(code, options) {
16+
if (typeof options === 'string') {
17+
options = fixFilename(options);
18+
} else if (options && options.filename) {
19+
options.filename = fixFilename(options.filename);
20+
}
21+
var script = new WeakScript(code, options);
22+
compiledScripts.add(script);
23+
return script;
24+
}
525

626
// The binding provides a few useful primitives:
727
// - ContextifyScript(code, { filename = "evalmachine.anonymous",
@@ -13,7 +33,7 @@ const Script = binding.ContextifyScript;
1333
// - isContext(sandbox)
1434
// From this we build the entire documented API.
1535

16-
Script.prototype.runInNewContext = function(sandbox, options) {
36+
WeakScript.prototype.runInNewContext = function(sandbox, options) {
1737
var context = exports.createContext(sandbox);
1838
return this.runInContext(context, options);
1939
};

0 commit comments

Comments
 (0)