|
27 | 27 | // unless they address existing, critical bugs. |
28 | 28 |
|
29 | 29 | const { |
30 | | - Array, |
| 30 | + ArrayPrototypeEvery, |
| 31 | + ArrayPrototypeIndexOf, |
| 32 | + ArrayPrototypeLastIndexOf, |
| 33 | + ArrayPrototypePush, |
| 34 | + ArrayPrototypeSlice, |
| 35 | + ArrayPrototypeSplice, |
31 | 36 | Error, |
32 | | - Map, |
| 37 | + FunctionPrototypeCall, |
33 | 38 | ObjectDefineProperty, |
34 | 39 | ReflectApply, |
| 40 | + SafeMap, |
35 | 41 | Symbol, |
36 | 42 | } = primordials; |
37 | 43 |
|
@@ -61,7 +67,7 @@ ObjectDefineProperty(process, 'domain', { |
61 | 67 | } |
62 | 68 | }); |
63 | 69 |
|
64 | | -const pairing = new Map(); |
| 70 | +const pairing = new SafeMap(); |
65 | 71 | const asyncHook = createHook({ |
66 | 72 | init(asyncId, type, triggerAsyncId, resource) { |
67 | 73 | if (process.domain !== null && process.domain !== undefined) { |
@@ -149,7 +155,8 @@ exports._stack = stack; |
149 | 155 | useDomainTrampoline(topLevelDomainCallback); |
150 | 156 |
|
151 | 157 | function updateExceptionCapture() { |
152 | | - if (stack.every((domain) => domain.listenerCount('error') === 0)) { |
| 158 | + if (ArrayPrototypeEvery(stack, |
| 159 | + (domain) => domain.listenerCount('error') === 0)) { |
153 | 160 | setUncaughtExceptionCaptureCallback(null); |
154 | 161 | } else { |
155 | 162 | setUncaughtExceptionCaptureCallback(null); |
@@ -296,18 +303,18 @@ Domain.prototype.enter = function() { |
296 | 303 | // Note that this might be a no-op, but we still need |
297 | 304 | // to push it onto the stack so that we can pop it later. |
298 | 305 | exports.active = process.domain = this; |
299 | | - stack.push(this); |
| 306 | + ArrayPrototypePush(stack, this); |
300 | 307 | updateExceptionCapture(); |
301 | 308 | }; |
302 | 309 |
|
303 | 310 |
|
304 | 311 | Domain.prototype.exit = function() { |
305 | 312 | // Don't do anything if this domain is not on the stack. |
306 | | - const index = stack.lastIndexOf(this); |
| 313 | + const index = ArrayPrototypeLastIndexOf(stack, this); |
307 | 314 | if (index === -1) return; |
308 | 315 |
|
309 | 316 | // Exit all domains until this one. |
310 | | - stack.splice(index); |
| 317 | + ArrayPrototypeSplice(stack, index); |
311 | 318 |
|
312 | 319 | exports.active = stack[stack.length - 1]; |
313 | 320 | process.domain = exports.active; |
@@ -346,33 +353,21 @@ Domain.prototype.add = function(ee) { |
346 | 353 | value: this, |
347 | 354 | writable: true |
348 | 355 | }); |
349 | | - this.members.push(ee); |
| 356 | + ArrayPrototypePush(this.members, ee); |
350 | 357 | }; |
351 | 358 |
|
352 | 359 |
|
353 | 360 | Domain.prototype.remove = function(ee) { |
354 | 361 | ee.domain = null; |
355 | | - const index = this.members.indexOf(ee); |
| 362 | + const index = ArrayPrototypeIndexOf(this.members, ee); |
356 | 363 | if (index !== -1) |
357 | | - this.members.splice(index, 1); |
| 364 | + ArrayPrototypeSplice(this.members, index, 1); |
358 | 365 | }; |
359 | 366 |
|
360 | 367 |
|
361 | 368 | Domain.prototype.run = function(fn) { |
362 | | - let ret; |
363 | | - |
364 | 369 | this.enter(); |
365 | | - if (arguments.length >= 2) { |
366 | | - const len = arguments.length; |
367 | | - const args = new Array(len - 1); |
368 | | - |
369 | | - for (let i = 1; i < len; i++) |
370 | | - args[i - 1] = arguments[i]; |
371 | | - |
372 | | - ret = fn.apply(this, args); |
373 | | - } else { |
374 | | - ret = fn.call(this); |
375 | | - } |
| 370 | + const ret = ReflectApply(fn, this, ArrayPrototypeSlice(arguments, 1)); |
376 | 371 | this.exit(); |
377 | 372 |
|
378 | 373 | return ret; |
@@ -394,17 +389,8 @@ function intercepted(_this, self, cb, fnargs) { |
394 | 389 | return; |
395 | 390 | } |
396 | 391 |
|
397 | | - const args = []; |
398 | | - let ret; |
399 | | - |
400 | 392 | self.enter(); |
401 | | - if (fnargs.length > 1) { |
402 | | - for (let i = 1; i < fnargs.length; i++) |
403 | | - args.push(fnargs[i]); |
404 | | - ret = cb.apply(_this, args); |
405 | | - } else { |
406 | | - ret = cb.call(_this); |
407 | | - } |
| 393 | + const ret = ReflectApply(cb, _this, ArrayPrototypeSlice(fnargs, 1)); |
408 | 394 | self.exit(); |
409 | 395 |
|
410 | 396 | return ret; |
@@ -423,13 +409,8 @@ Domain.prototype.intercept = function(cb) { |
423 | 409 |
|
424 | 410 |
|
425 | 411 | function bound(_this, self, cb, fnargs) { |
426 | | - let ret; |
427 | | - |
428 | 412 | self.enter(); |
429 | | - if (fnargs.length > 0) |
430 | | - ret = cb.apply(_this, fnargs); |
431 | | - else |
432 | | - ret = cb.call(_this); |
| 413 | + const ret = ReflectApply(cb, _this, fnargs); |
433 | 414 | self.exit(); |
434 | 415 |
|
435 | 416 | return ret; |
@@ -468,7 +449,7 @@ EventEmitter.init = function() { |
468 | 449 | this.domain = exports.active; |
469 | 450 | } |
470 | 451 |
|
471 | | - return eventInit.call(this); |
| 452 | + return FunctionPrototypeCall(eventInit, this); |
472 | 453 | }; |
473 | 454 |
|
474 | 455 | const eventEmit = EventEmitter.prototype.emit; |
@@ -506,7 +487,7 @@ EventEmitter.prototype.emit = function(...args) { |
506 | 487 | // handler doesn't run in its own context. This prevents any event emitter |
507 | 488 | // created or any exception thrown in that error handler from recursively |
508 | 489 | // executing that error handler. |
509 | | - const origDomainsStack = stack.slice(); |
| 490 | + const origDomainsStack = ArrayPrototypeSlice(stack); |
510 | 491 | const origActiveDomain = process.domain; |
511 | 492 |
|
512 | 493 | // Travel the domains stack from top to bottom to find the first domain |
@@ -521,7 +502,7 @@ EventEmitter.prototype.emit = function(...args) { |
521 | 502 | if (idx < 0) { |
522 | 503 | stack.length = 0; |
523 | 504 | } else { |
524 | | - stack.splice(idx + 1); |
| 505 | + ArrayPrototypeSplice(stack, idx + 1); |
525 | 506 | } |
526 | 507 |
|
527 | 508 | // Change the current active domain |
|