Skip to content

Commit

Permalink
vm: improve performance of vm.runIn*()
Browse files Browse the repository at this point in the history
Optimize for common cases in vm.runInContext() and
vm.runInThisContext().
  • Loading branch information
Trott committed Jan 15, 2017
1 parent 95e90a0 commit ffdc082
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,16 @@ const realRunInContext = Script.prototype.runInContext;

Script.prototype.runInThisContext = function(options) {
if (options && options.breakOnSigint) {
const realRunInThisContextScript = () => {
return realRunInThisContext.call(this, options);
};
return sigintHandlersWrap(realRunInThisContextScript);
return sigintHandlersWrap(realRunInThisContext, this, [options]);
} else {
return realRunInThisContext.call(this, options);
}
};

Script.prototype.runInContext = function(contextifiedSandbox, options) {
if (options && options.breakOnSigint) {
const realRunInContextScript = () => {
return realRunInContext.call(this, contextifiedSandbox, options);
};
return sigintHandlersWrap(realRunInContextScript);
return sigintHandlersWrap(realRunInContext, this,
[contextifiedSandbox, options]);
} else {
return realRunInContext.call(this, contextifiedSandbox, options);
}
Expand Down Expand Up @@ -83,19 +78,22 @@ exports.isContext = binding.isContext;

// Remove all SIGINT listeners and re-attach them after the wrapped function
// has executed, so that caught SIGINT are handled by the listeners again.
function sigintHandlersWrap(fn) {
function sigintHandlersWrap(fn, thisArg, argsArray) {
// Using the internal list here to make sure `.once()` wrappers are used,
// not the original ones.
let sigintListeners = process._events.SIGINT;
if (!Array.isArray(sigintListeners))
sigintListeners = sigintListeners ? [sigintListeners] : [];
else
if (!sigintListeners)
return fn.apply(thisArg, argsArray);

if (Array.isArray(sigintListeners))
sigintListeners = sigintListeners.slice();
else
sigintListeners = [sigintListeners];

process.removeAllListeners('SIGINT');

try {
return fn();
return fn.apply(thisArg, argsArray);
} finally {
// Add using the public methods so that the `newListener` handler of
// process can re-attach the listeners.
Expand Down

0 comments on commit ffdc082

Please sign in to comment.