summaryrefslogtreecommitdiff
path: root/lib/vm.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-01-14 19:43:33 -0800
committerJames M Snell <jasnell@gmail.com>2017-01-18 08:27:10 -0800
commitef1e77d186c021539731e8279eb68e2c7aae0a21 (patch)
tree93b75793e24e0f498f504140c0c5a6f7ad07f83c /lib/vm.js
parent030dd14793da2aa1c252e38d33abb37570f8ee8a (diff)
downloadandroid-node-v8-ef1e77d186c021539731e8279eb68e2c7aae0a21.tar.gz
android-node-v8-ef1e77d186c021539731e8279eb68e2c7aae0a21.tar.bz2
android-node-v8-ef1e77d186c021539731e8279eb68e2c7aae0a21.zip
vm: improve performance of vm.runIn*()
Optimize for common cases in vm.runInContext() and vm.runInThisContext(). PR-URL: https://github.com/nodejs/node/pull/10816 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Brian White <mscdex@mscdex.net>
Diffstat (limited to 'lib/vm.js')
-rw-r--r--lib/vm.js26
1 files changed, 11 insertions, 15 deletions
diff --git a/lib/vm.js b/lib/vm.js
index 4869d36880..cf672fbbc1 100644
--- a/lib/vm.js
+++ b/lib/vm.js
@@ -17,22 +17,17 @@ const realRunInThisContext = Script.prototype.runInThisContext;
const realRunInContext = Script.prototype.runInContext;
Script.prototype.runInThisContext = function(options) {
- if (options && options.breakOnSigint) {
- const realRunInThisContextScript = () => {
- return realRunInThisContext.call(this, options);
- };
- return sigintHandlersWrap(realRunInThisContextScript);
+ if (options && options.breakOnSigint && process._events.SIGINT) {
+ 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);
+ if (options && options.breakOnSigint && process._events.SIGINT) {
+ return sigintHandlersWrap(realRunInContext, this,
+ [contextifiedSandbox, options]);
} else {
return realRunInContext.call(this, contextifiedSandbox, options);
}
@@ -83,19 +78,20 @@ 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 (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.