Skip to content

Commit 9d26c4d

Browse files
aduh95danielleadams
authored andcommitted
domain: refactor to use more primordials
PR-URL: #35885 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 8d76db8 commit 9d26c4d

File tree

1 file changed

+23
-42
lines changed

1 file changed

+23
-42
lines changed

lib/domain.js

+23-42
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@
2727
// unless they address existing, critical bugs.
2828

2929
const {
30-
Array,
30+
ArrayPrototypeEvery,
31+
ArrayPrototypeIndexOf,
32+
ArrayPrototypeLastIndexOf,
33+
ArrayPrototypePush,
34+
ArrayPrototypeSlice,
35+
ArrayPrototypeSplice,
3136
Error,
32-
Map,
37+
FunctionPrototypeCall,
3338
ObjectDefineProperty,
3439
ReflectApply,
40+
SafeMap,
3541
Symbol,
3642
} = primordials;
3743

@@ -61,7 +67,7 @@ ObjectDefineProperty(process, 'domain', {
6167
}
6268
});
6369

64-
const pairing = new Map();
70+
const pairing = new SafeMap();
6571
const asyncHook = createHook({
6672
init(asyncId, type, triggerAsyncId, resource) {
6773
if (process.domain !== null && process.domain !== undefined) {
@@ -149,7 +155,8 @@ exports._stack = stack;
149155
useDomainTrampoline(topLevelDomainCallback);
150156

151157
function updateExceptionCapture() {
152-
if (stack.every((domain) => domain.listenerCount('error') === 0)) {
158+
if (ArrayPrototypeEvery(stack,
159+
(domain) => domain.listenerCount('error') === 0)) {
153160
setUncaughtExceptionCaptureCallback(null);
154161
} else {
155162
setUncaughtExceptionCaptureCallback(null);
@@ -296,18 +303,18 @@ Domain.prototype.enter = function() {
296303
// Note that this might be a no-op, but we still need
297304
// to push it onto the stack so that we can pop it later.
298305
exports.active = process.domain = this;
299-
stack.push(this);
306+
ArrayPrototypePush(stack, this);
300307
updateExceptionCapture();
301308
};
302309

303310

304311
Domain.prototype.exit = function() {
305312
// Don't do anything if this domain is not on the stack.
306-
const index = stack.lastIndexOf(this);
313+
const index = ArrayPrototypeLastIndexOf(stack, this);
307314
if (index === -1) return;
308315

309316
// Exit all domains until this one.
310-
stack.splice(index);
317+
ArrayPrototypeSplice(stack, index);
311318

312319
exports.active = stack[stack.length - 1];
313320
process.domain = exports.active;
@@ -346,33 +353,21 @@ Domain.prototype.add = function(ee) {
346353
value: this,
347354
writable: true
348355
});
349-
this.members.push(ee);
356+
ArrayPrototypePush(this.members, ee);
350357
};
351358

352359

353360
Domain.prototype.remove = function(ee) {
354361
ee.domain = null;
355-
const index = this.members.indexOf(ee);
362+
const index = ArrayPrototypeIndexOf(this.members, ee);
356363
if (index !== -1)
357-
this.members.splice(index, 1);
364+
ArrayPrototypeSplice(this.members, index, 1);
358365
};
359366

360367

361368
Domain.prototype.run = function(fn) {
362-
let ret;
363-
364369
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));
376371
this.exit();
377372

378373
return ret;
@@ -394,17 +389,8 @@ function intercepted(_this, self, cb, fnargs) {
394389
return;
395390
}
396391

397-
const args = [];
398-
let ret;
399-
400392
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));
408394
self.exit();
409395

410396
return ret;
@@ -423,13 +409,8 @@ Domain.prototype.intercept = function(cb) {
423409

424410

425411
function bound(_this, self, cb, fnargs) {
426-
let ret;
427-
428412
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);
433414
self.exit();
434415

435416
return ret;
@@ -468,7 +449,7 @@ EventEmitter.init = function() {
468449
this.domain = exports.active;
469450
}
470451

471-
return eventInit.call(this);
452+
return FunctionPrototypeCall(eventInit, this);
472453
};
473454

474455
const eventEmit = EventEmitter.prototype.emit;
@@ -506,7 +487,7 @@ EventEmitter.prototype.emit = function(...args) {
506487
// handler doesn't run in its own context. This prevents any event emitter
507488
// created or any exception thrown in that error handler from recursively
508489
// executing that error handler.
509-
const origDomainsStack = stack.slice();
490+
const origDomainsStack = ArrayPrototypeSlice(stack);
510491
const origActiveDomain = process.domain;
511492

512493
// Travel the domains stack from top to bottom to find the first domain
@@ -521,7 +502,7 @@ EventEmitter.prototype.emit = function(...args) {
521502
if (idx < 0) {
522503
stack.length = 0;
523504
} else {
524-
stack.splice(idx + 1);
505+
ArrayPrototypeSplice(stack, idx + 1);
525506
}
526507

527508
// Change the current active domain

0 commit comments

Comments
 (0)