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.

Fixed nwjs/nw.js#4269
  • Loading branch information
Cong Liu committed Apr 7, 2016
1 parent 65b297d commit 24acd1f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/vm.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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);
};
Expand Down

0 comments on commit 24acd1f

Please sign in to comment.