From 28edc1db992a47eda2f97b4d9e2b56883ad30583 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Mon, 4 Dec 2017 11:45:15 -0500 Subject: [PATCH] events: use Reflect.apply MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of callback bound apply, instead use the standard Reflect.apply. This is both safer and appears to offer a slight performance benefit. Backport-PR-URL: https://github.com/nodejs/node/pull/18487 PR-URL: https://github.com/nodejs/node/pull/17456 Refs: https://github.com/nodejs/node/issues/12956 Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/domain.js | 4 ++-- lib/events.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/domain.js b/lib/domain.js index cef09dca1c8857..b68e642c490cc3 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -400,7 +400,7 @@ const eventEmit = EventEmitter.prototype.emit; EventEmitter.prototype.emit = function emit(...args) { const domain = this.domain; if (domain === null || domain === undefined || this === process) { - return eventEmit.apply(this, args); + return Reflect.apply(eventEmit, this, args); } const type = args[0]; @@ -415,7 +415,7 @@ EventEmitter.prototype.emit = function emit(...args) { domain.enter(); try { - return eventEmit.apply(this, args); + return Reflect.apply(eventEmit, this, args); } catch (er) { if (typeof er === 'object' && er !== null) { er.domainEmitter = this; diff --git a/lib/events.js b/lib/events.js index ed2c9343d9b445..b9149d2b9b51a7 100644 --- a/lib/events.js +++ b/lib/events.js @@ -124,12 +124,12 @@ EventEmitter.prototype.emit = function emit(type, ...args) { return false; if (typeof handler === 'function') { - handler.apply(this, args); + Reflect.apply(handler, this, args); } else { const len = handler.length; const listeners = arrayClone(handler, len); for (var i = 0; i < len; ++i) - listeners[i].apply(this, args); + Reflect.apply(listeners[i], this, args); } return true; @@ -216,7 +216,7 @@ function onceWrapper(...args) { if (!this.fired) { this.target.removeListener(this.type, this.wrapFn); this.fired = true; - this.listener.apply(this.target, args); + Reflect.apply(this.listener, this.target, args); } }