mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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:
parent
8fa90358d7
commit
0ad4f70db5
@ -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;
|
||||
|
||||
@ -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) {
|
||||
@ -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);
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -468,7 +449,7 @@ EventEmitter.init = function() {
|
||||
this.domain = exports.active;
|
||||
}
|
||||
|
||||
return eventInit.call(this);
|
||||
return FunctionPrototypeCall(eventInit, this);
|
||||
};
|
||||
|
||||
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
|
||||
// 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
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user