events: refactor to use more primordials

PR-URL: https://github.com/nodejs/node/pull/36304
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel 2020-11-18 00:31:30 +01:00 committed by Node.js GitHub Bot
parent 88fb8e45ee
commit 997f2fcbbc
3 changed files with 17 additions and 12 deletions

View File

@ -22,10 +22,13 @@
'use strict';
const {
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeSlice,
Boolean,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeCall,
MathMin,
NumberIsNaN,
ObjectCreate,
@ -81,7 +84,7 @@ const lazyDOMException = hideStackFrames((message, name) => {
function EventEmitter(opts) {
EventEmitter.init.call(this, opts);
FunctionPrototypeCall(EventEmitter.init, this, opts);
}
module.exports = EventEmitter;
module.exports.once = once;
@ -173,7 +176,7 @@ EventEmitter.setMaxListeners =
isEventTarget = require('internal/event_target').isEventTarget;
// Performance for forEach is now comparable with regular for-loop
eventTargets.forEach((target) => {
ArrayPrototypeForEach(eventTargets, (target) => {
if (isEventTarget(target)) {
target[kMaxEventTargetListeners] = n;
target[kMaxEventTargetListenersWarned] = false;
@ -224,7 +227,7 @@ function addCatch(that, promise, type, args) {
const then = promise.then;
if (typeof then === 'function') {
then.call(promise, undefined, function(err) {
FunctionPrototypeCall(then, promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
@ -670,7 +673,7 @@ function arrayClone(arr) {
case 5: return [arr[0], arr[1], arr[2], arr[3], arr[4]];
case 6: return [arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
}
return arr.slice();
return ArrayPrototypeSlice(arr);
}
function unwrapListeners(arr) {
@ -813,7 +816,7 @@ function on(emitter, event, options) {
// Wait until an event happens
return new Promise(function(resolve, reject) {
unconsumedPromises.push({ resolve, reject });
ArrayPrototypePush(unconsumedPromises, { resolve, reject });
});
},
@ -877,7 +880,7 @@ function on(emitter, event, options) {
if (promise) {
promise.resolve(createIterResult(args, false));
} else {
unconsumedEvents.push(args);
ArrayPrototypePush(unconsumedEvents, args);
}
}

View File

@ -4,6 +4,7 @@ const {
ArrayFrom,
Boolean,
Error,
FunctionPrototypeBind,
FunctionPrototypeCall,
NumberIsInteger,
ObjectAssign,
@ -212,7 +213,7 @@ class Listener {
this.callback =
typeof listener === 'function' ?
listener :
listener.handleEvent.bind(listener);
FunctionPrototypeBind(listener.handleEvent, listener);
}
same(listener, capture) {
@ -419,7 +420,7 @@ class EventTarget {
} else {
arg = createEvent();
}
const result = handler.callback.call(this, arg);
const result = FunctionPrototypeCall(handler.callback, this, arg);
if (result !== undefined && result !== null)
addCatch(this, result, createEvent());
} catch (err) {
@ -590,7 +591,7 @@ function isEventTarget(obj) {
function addCatch(that, promise, event) {
const then = promise.then;
if (typeof then === 'function') {
then.call(promise, undefined, function(err) {
FunctionPrototypeCall(then, promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, event);

View File

@ -2,7 +2,8 @@
const {
ArrayIsArray,
Set,
ArrayPrototypeJoin,
SafeSet,
Symbol,
} = primordials;
@ -27,7 +28,7 @@ const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');
const enabledTracingObjects = new Set();
const enabledTracingObjects = new SafeSet();
class Tracing {
constructor(categories) {
@ -63,7 +64,7 @@ class Tracing {
}
get categories() {
return this[kCategories].join(',');
return ArrayPrototypeJoin(this[kCategories], ',');
}
[customInspectSymbol](depth, opts) {