domain: refactor to use more primordials

PR-URL: https://github.com/nodejs/node/pull/35885
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Antoine du Hamel 2020-10-30 16:03:02 +01:00 committed by Node.js GitHub Bot
parent 8fa90358d7
commit 0ad4f70db5

View File

@ -27,11 +27,17 @@
// unless they address existing, critical bugs. // unless they address existing, critical bugs.
const { const {
Array, ArrayPrototypeEvery,
ArrayPrototypeIndexOf,
ArrayPrototypeLastIndexOf,
ArrayPrototypePush,
ArrayPrototypeSlice,
ArrayPrototypeSplice,
Error, Error,
Map, FunctionPrototypeCall,
ObjectDefineProperty, ObjectDefineProperty,
ReflectApply, ReflectApply,
SafeMap,
Symbol, Symbol,
} = primordials; } = primordials;
@ -61,7 +67,7 @@ ObjectDefineProperty(process, 'domain', {
} }
}); });
const pairing = new Map(); const pairing = new SafeMap();
const asyncHook = createHook({ const asyncHook = createHook({
init(asyncId, type, triggerAsyncId, resource) { init(asyncId, type, triggerAsyncId, resource) {
if (process.domain !== null && process.domain !== undefined) { if (process.domain !== null && process.domain !== undefined) {
@ -149,7 +155,8 @@ exports._stack = stack;
useDomainTrampoline(topLevelDomainCallback); useDomainTrampoline(topLevelDomainCallback);
function updateExceptionCapture() { function updateExceptionCapture() {
if (stack.every((domain) => domain.listenerCount('error') === 0)) { if (ArrayPrototypeEvery(stack,
(domain) => domain.listenerCount('error') === 0)) {
setUncaughtExceptionCaptureCallback(null); setUncaughtExceptionCaptureCallback(null);
} else { } else {
setUncaughtExceptionCaptureCallback(null); setUncaughtExceptionCaptureCallback(null);
@ -296,18 +303,18 @@ Domain.prototype.enter = function() {
// Note that this might be a no-op, but we still need // 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. // to push it onto the stack so that we can pop it later.
exports.active = process.domain = this; exports.active = process.domain = this;
stack.push(this); ArrayPrototypePush(stack, this);
updateExceptionCapture(); updateExceptionCapture();
}; };
Domain.prototype.exit = function() { Domain.prototype.exit = function() {
// Don't do anything if this domain is not on the stack. // 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; if (index === -1) return;
// Exit all domains until this one. // Exit all domains until this one.
stack.splice(index); ArrayPrototypeSplice(stack, index);
exports.active = stack[stack.length - 1]; exports.active = stack[stack.length - 1];
process.domain = exports.active; process.domain = exports.active;
@ -346,33 +353,21 @@ Domain.prototype.add = function(ee) {
value: this, value: this,
writable: true writable: true
}); });
this.members.push(ee); ArrayPrototypePush(this.members, ee);
}; };
Domain.prototype.remove = function(ee) { Domain.prototype.remove = function(ee) {
ee.domain = null; ee.domain = null;
const index = this.members.indexOf(ee); const index = ArrayPrototypeIndexOf(this.members, ee);
if (index !== -1) if (index !== -1)
this.members.splice(index, 1); ArrayPrototypeSplice(this.members, index, 1);
}; };
Domain.prototype.run = function(fn) { Domain.prototype.run = function(fn) {
let ret;
this.enter(); this.enter();
if (arguments.length >= 2) { const ret = ReflectApply(fn, this, ArrayPrototypeSlice(arguments, 1));
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);
}
this.exit(); this.exit();
return ret; return ret;
@ -394,17 +389,8 @@ function intercepted(_this, self, cb, fnargs) {
return; return;
} }
const args = [];
let ret;
self.enter(); self.enter();
if (fnargs.length > 1) { const ret = ReflectApply(cb, _this, ArrayPrototypeSlice(fnargs, 1));
for (let i = 1; i < fnargs.length; i++)
args.push(fnargs[i]);
ret = cb.apply(_this, args);
} else {
ret = cb.call(_this);
}
self.exit(); self.exit();
return ret; return ret;
@ -423,13 +409,8 @@ Domain.prototype.intercept = function(cb) {
function bound(_this, self, cb, fnargs) { function bound(_this, self, cb, fnargs) {
let ret;
self.enter(); self.enter();
if (fnargs.length > 0) const ret = ReflectApply(cb, _this, fnargs);
ret = cb.apply(_this, fnargs);
else
ret = cb.call(_this);
self.exit(); self.exit();
return ret; return ret;
@ -468,7 +449,7 @@ EventEmitter.init = function() {
this.domain = exports.active; this.domain = exports.active;
} }
return eventInit.call(this); return FunctionPrototypeCall(eventInit, this);
}; };
const eventEmit = EventEmitter.prototype.emit; const eventEmit = EventEmitter.prototype.emit;
@ -506,7 +487,7 @@ EventEmitter.prototype.emit = function(...args) {
// handler doesn't run in its own context. This prevents any event emitter // handler doesn't run in its own context. This prevents any event emitter
// created or any exception thrown in that error handler from recursively // created or any exception thrown in that error handler from recursively
// executing that error handler. // executing that error handler.
const origDomainsStack = stack.slice(); const origDomainsStack = ArrayPrototypeSlice(stack);
const origActiveDomain = process.domain; const origActiveDomain = process.domain;
// Travel the domains stack from top to bottom to find the first domain // Travel the domains stack from top to bottom to find the first domain
@ -521,7 +502,7 @@ EventEmitter.prototype.emit = function(...args) {
if (idx < 0) { if (idx < 0) {
stack.length = 0; stack.length = 0;
} else { } else {
stack.splice(idx + 1); ArrayPrototypeSplice(stack, idx + 1);
} }
// Change the current active domain // Change the current active domain