Skip to content

Commit

Permalink
Fix broken links generated from Node modules in DevTools
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Cong Liu committed Apr 7, 2016
1 parent 65b297d commit 0ffdf14
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/vm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
'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 fixFilename(filename) {
return filename = 'file://' + encodeURI(filename.replace(/\\/g, '/'));
}

function Script(code, options) {
if (typeof options === 'string') {
options = fixFilename(options);
} else if (options && options.filename) {
options.filename = fixFilename(options.filename);
}
var script = new WeakScript(code, options);
compiledScripts.add(script);
return script;
}

// The binding provides a few useful primitives:
// - ContextifyScript(code, { filename = "evalmachine.anonymous",
Expand All @@ -13,7 +33,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);
};
Expand Down

0 comments on commit 0ffdf14

Please sign in to comment.