23
23
24
24
const {
25
25
ArrayPrototypeForEach,
26
+ ArrayPrototypeIndexOf,
27
+ ArrayPrototypeJoin,
26
28
ArrayPrototypePush,
29
+ ArrayPrototypeShift,
27
30
ArrayPrototypeSlice,
31
+ ArrayPrototypeSplice,
32
+ ArrayPrototypeUnshift,
28
33
Boolean,
29
34
Error,
30
35
ErrorCaptureStackTrace,
36
+ FunctionPrototypeBind,
31
37
FunctionPrototypeCall,
32
38
MathMin,
33
39
NumberIsNaN,
@@ -42,9 +48,10 @@ const {
42
48
ReflectApply,
43
49
ReflectOwnKeys,
44
50
String,
51
+ StringPrototypeSplit,
45
52
Symbol,
46
53
SymbolFor,
47
- SymbolAsyncIterator
54
+ SymbolAsyncIterator,
48
55
} = primordials ;
49
56
const kRejection = SymbolFor ( 'nodejs.rejection' ) ;
50
57
@@ -274,7 +281,7 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
274
281
function identicalSequenceRange ( a , b ) {
275
282
for ( let i = 0 ; i < a . length - 3 ; i ++ ) {
276
283
// Find the first entry of b that matches the current entry of a.
277
- const pos = b . indexOf ( a [ i ] ) ;
284
+ const pos = ArrayPrototypeIndexOf ( b , a [ i ] ) ;
278
285
if ( pos !== - 1 ) {
279
286
const rest = b . length - pos ;
280
287
if ( rest > 3 ) {
@@ -303,16 +310,18 @@ function enhanceStackTrace(err, own) {
303
310
} catch { }
304
311
const sep = `\nEmitted 'error' event${ ctorInfo } at:\n` ;
305
312
306
- const errStack = err . stack . split ( '\n' ) . slice ( 1 ) ;
307
- const ownStack = own . stack . split ( '\n' ) . slice ( 1 ) ;
313
+ const errStack = ArrayPrototypeSlice (
314
+ StringPrototypeSplit ( err . stack , '\n' ) , 1 ) ;
315
+ const ownStack = ArrayPrototypeSlice (
316
+ StringPrototypeSplit ( own . stack , '\n' ) , 1 ) ;
308
317
309
318
const { 0 : len , 1 : off } = identicalSequenceRange ( ownStack , errStack ) ;
310
319
if ( len > 0 ) {
311
- ownStack . splice ( off + 1 , len - 2 ,
312
- ' [... lines matching original stack trace ...]' ) ;
320
+ ArrayPrototypeSplice ( ownStack , off + 1 , len - 2 ,
321
+ ' [... lines matching original stack trace ...]' ) ;
313
322
}
314
323
315
- return err . stack + sep + ownStack . join ( '\n' ) ;
324
+ return err . stack + sep + ArrayPrototypeJoin ( ownStack , '\n' ) ;
316
325
}
317
326
318
327
EventEmitter . prototype . emit = function emit ( type , ...args ) {
@@ -336,7 +345,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
336
345
const capture = { } ;
337
346
ErrorCaptureStackTrace ( capture , EventEmitter . prototype . emit ) ;
338
347
ObjectDefineProperty ( er , kEnhanceStackBeforeInspector , {
339
- value : enhanceStackTrace . bind ( this , er , capture ) ,
348
+ value : FunctionPrototypeBind ( enhanceStackTrace , this , er , capture ) ,
340
349
configurable : true
341
350
} ) ;
342
351
} catch { }
@@ -430,9 +439,9 @@ function _addListener(target, type, listener, prepend) {
430
439
prepend ? [ listener , existing ] : [ existing , listener ] ;
431
440
// If we've already got an array, just append.
432
441
} else if ( prepend ) {
433
- existing . unshift ( listener ) ;
442
+ ArrayPrototypeUnshift ( existing , listener ) ;
434
443
} else {
435
- existing . push ( listener ) ;
444
+ ArrayPrototypePush ( existing , listener ) ;
436
445
}
437
446
438
447
// Check for listener leak
@@ -472,14 +481,14 @@ function onceWrapper() {
472
481
this . target . removeListener ( this . type , this . wrapFn ) ;
473
482
this . fired = true ;
474
483
if ( arguments . length === 0 )
475
- return this . listener . call ( this . target ) ;
476
- return this . listener . apply ( this . target , arguments ) ;
484
+ return FunctionPrototypeCall ( this . listener , this . target ) ;
485
+ return ReflectApply ( this . listener , this . target , arguments ) ;
477
486
}
478
487
}
479
488
480
489
function _onceWrap ( target , type , listener ) {
481
490
const state = { fired : false , wrapFn : undefined , target, type, listener } ;
482
- const wrapped = onceWrapper . bind ( state ) ;
491
+ const wrapped = FunctionPrototypeBind ( onceWrapper , state ) ;
483
492
wrapped . listener = listener ;
484
493
state . wrapFn = wrapped ;
485
494
return wrapped ;
@@ -535,7 +544,7 @@ EventEmitter.prototype.removeListener =
535
544
return this ;
536
545
537
546
if ( position === 0 )
538
- list . shift ( ) ;
547
+ ArrayPrototypeShift ( list ) ;
539
548
else {
540
549
if ( spliceOne === undefined )
541
550
spliceOne = require ( 'internal/util' ) . spliceOne ;
@@ -629,7 +638,7 @@ EventEmitter.listenerCount = function(emitter, type) {
629
638
if ( typeof emitter . listenerCount === 'function' ) {
630
639
return emitter . listenerCount ( type ) ;
631
640
}
632
- return listenerCount . call ( emitter , type ) ;
641
+ return FunctionPrototypeCall ( listenerCount , emitter , type ) ;
633
642
} ;
634
643
635
644
EventEmitter . prototype . listenerCount = listenerCount ;
@@ -785,7 +794,7 @@ function on(emitter, event, options) {
785
794
const iterator = ObjectSetPrototypeOf ( {
786
795
next ( ) {
787
796
// First, we consume all unread events
788
- const value = unconsumedEvents . shift ( ) ;
797
+ const value = ArrayPrototypeShift ( unconsumedEvents ) ;
789
798
if ( value ) {
790
799
return PromiseResolve ( createIterResult ( value , false ) ) ;
791
800
}
@@ -867,7 +876,7 @@ function on(emitter, event, options) {
867
876
}
868
877
869
878
function eventHandler ( ...args ) {
870
- const promise = unconsumedPromises . shift ( ) ;
879
+ const promise = ArrayPrototypeShift ( unconsumedPromises ) ;
871
880
if ( promise ) {
872
881
promise . resolve ( createIterResult ( args , false ) ) ;
873
882
} else {
@@ -878,7 +887,7 @@ function on(emitter, event, options) {
878
887
function errorHandler ( err ) {
879
888
finished = true ;
880
889
881
- const toError = unconsumedPromises . shift ( ) ;
890
+ const toError = ArrayPrototypeShift ( unconsumedPromises ) ;
882
891
883
892
if ( toError ) {
884
893
toError . reject ( err ) ;
0 commit comments