Skip to content

Commit

Permalink
domain: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #35885
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Nov 9, 2020
1 parent 8d76db8 commit 9d26c4d
Showing 1 changed file with 23 additions and 42 deletions.
65 changes: 23 additions & 42 deletions lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@
// unless they address existing, critical bugs.

const {
Array,
ArrayPrototypeEvery,
ArrayPrototypeIndexOf,
ArrayPrototypeLastIndexOf,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
Error,
Map,
FunctionPrototypeCall,
ObjectDefineProperty,
ReflectApply,
SafeMap,
Symbol,
} = primordials;

Expand Down Expand Up @@ -61,7 +67,7 @@ ObjectDefineProperty(process, 'domain', {
}
});

const pairing = new Map();
const pairing = new SafeMap();
const asyncHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (process.domain !== null && process.domain !== undefined) {
Expand Down Expand Up @@ -149,7 +155,8 @@ exports._stack = stack;
useDomainTrampoline(topLevelDomainCallback);

function updateExceptionCapture() {
if (stack.every((domain) => domain.listenerCount('error') === 0)) {
if (ArrayPrototypeEvery(stack,
(domain) => domain.listenerCount('error') === 0)) {
setUncaughtExceptionCaptureCallback(null);
} else {
setUncaughtExceptionCaptureCallback(null);
Expand Down Expand Up @@ -296,18 +303,18 @@ Domain.prototype.enter = function() {
// Note that this might be a no-op, but we still need
// to push it onto the stack so that we can pop it later.
exports.active = process.domain = this;
stack.push(this);
ArrayPrototypePush(stack, this);
updateExceptionCapture();
};


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

// Exit all domains until this one.
stack.splice(index);
ArrayPrototypeSplice(stack, index);

exports.active = stack[stack.length - 1];
process.domain = exports.active;
Expand Down Expand Up @@ -346,33 +353,21 @@ Domain.prototype.add = function(ee) {
value: this,
writable: true
});
this.members.push(ee);
ArrayPrototypePush(this.members, ee);
};


Domain.prototype.remove = function(ee) {
ee.domain = null;
const index = this.members.indexOf(ee);
const index = ArrayPrototypeIndexOf(this.members, ee);
if (index !== -1)
this.members.splice(index, 1);
ArrayPrototypeSplice(this.members, index, 1);
};


Domain.prototype.run = function(fn) {
let ret;

this.enter();
if (arguments.length >= 2) {
const len = arguments.length;
const args = new Array(len - 1);

for (let i = 1; i < len; i++)
args[i - 1] = arguments[i];

ret = fn.apply(this, args);
} else {
ret = fn.call(this);
}
const ret = ReflectApply(fn, this, ArrayPrototypeSlice(arguments, 1));
this.exit();

return ret;
Expand All @@ -394,17 +389,8 @@ function intercepted(_this, self, cb, fnargs) {
return;
}

const args = [];
let ret;

self.enter();
if (fnargs.length > 1) {
for (let i = 1; i < fnargs.length; i++)
args.push(fnargs[i]);
ret = cb.apply(_this, args);
} else {
ret = cb.call(_this);
}
const ret = ReflectApply(cb, _this, ArrayPrototypeSlice(fnargs, 1));
self.exit();

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


function bound(_this, self, cb, fnargs) {
let ret;

self.enter();
if (fnargs.length > 0)
ret = cb.apply(_this, fnargs);
else
ret = cb.call(_this);
const ret = ReflectApply(cb, _this, fnargs);
self.exit();

return ret;
Expand Down Expand Up @@ -468,7 +449,7 @@ EventEmitter.init = function() {
this.domain = exports.active;
}

return eventInit.call(this);
return FunctionPrototypeCall(eventInit, this);
};

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

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

// Change the current active domain
Expand Down

0 comments on commit 9d26c4d

Please sign in to comment.