Skip to content

Commit

Permalink
events: use Reflect.apply
Browse files Browse the repository at this point in the history
Instead of callback bound apply, instead use the standard
Reflect.apply. This is both safer and appears to offer
a slight performance benefit.

PR-URL: nodejs#17456
Refs: nodejs#12956
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
apapirovski committed Dec 6, 2017
1 parent 694e7ba commit f5eb803
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,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;
Expand Down Expand Up @@ -215,7 +215,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);
}
}

Expand Down

0 comments on commit f5eb803

Please sign in to comment.