Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: refactor to use more primordials #36304

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
fixup! events: refactor to use more primordials
  • Loading branch information
aduh95 committed Dec 22, 2020
commit 8d5b6767bfac6b134d26b0da4c88cbf0855f5ef6
22 changes: 10 additions & 12 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
function identicalSequenceRange(a, b) {
for (let i = 0; i < a.length - 3; i++) {
// Find the first entry of b that matches the current entry of a.
const pos = ArrayPrototypeIndexOf(b, a[i]);
const pos = b.indexOf(a[i]);
if (pos !== -1) {
const rest = b.length - pos;
if (rest > 3) {
Expand Down Expand Up @@ -318,18 +318,16 @@ function enhanceStackTrace(err, own) {
} catch {}
const sep = `\nEmitted 'error' event${ctorInfo} at:\n`;

const errStack =
ArrayPrototypeSlice(StringPrototypeSplit(err.stack, '\n'), 1);
const ownStack =
ArrayPrototypeSlice(StringPrototypeSplit(own.stack, '\n'), 1);
const errStack = err.stack.split('\n').slice(1);
const ownStack = own.stack.split('\n').slice(1);

const [ len, off ] = identicalSequenceRange(ownStack, errStack);
if (len > 0) {
ArrayPrototypeSplice(ownStack, off + 1, len - 2,
' [... lines matching original stack trace ...]');
ownStack.splice(off + 1, len - 2,
' [... lines matching original stack trace ...]');
}

return err.stack + sep + ArrayPrototypeJoin(ownStack, '\n');
return err.stack + sep + ownStack.join('\n');
}

EventEmitter.prototype.emit = function emit(type, ...args) {
Expand All @@ -353,7 +351,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
const capture = {};
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
value: FunctionPrototypeBind(enhanceStackTrace, this, er, capture),
value: enhanceStackTrace.bind(this, er, capture),
configurable: true
});
} catch {}
Expand Down Expand Up @@ -449,7 +447,7 @@ function _addListener(target, type, listener, prepend) {
} else if (prepend) {
existing.unshift(listener);
} else {
ArrayPrototypePush(existing, listener);
existing.push(listener);
}

// Check for listener leak
Expand Down Expand Up @@ -496,7 +494,7 @@ function onceWrapper() {

function _onceWrap(target, type, listener) {
const state = { fired: false, wrapFn: undefined, target, type, listener };
const wrapped = FunctionPrototypeBind(onceWrapper, state);
const wrapped = onceWrapper.bind(state);
wrapped.listener = listener;
state.wrapFn = wrapped;
return wrapped;
Expand Down Expand Up @@ -646,7 +644,7 @@ EventEmitter.listenerCount = function(emitter, type) {
if (typeof emitter.listenerCount === 'function') {
return emitter.listenerCount(type);
}
return FunctionPrototypeCall(listenerCount, emitter, type);
return listenerCount.call(emitter, type);
};

EventEmitter.prototype.listenerCount = listenerCount;
Expand Down