mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
process: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36212 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Pranshu Srivastava <rexagod@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
69a8f05488
commit
39d0ceda48
3 files changed
+35
-33
No files matched your search
@@ -6,18 +6,21 @@
|
||||
|
||||
const {
|
||||
ArrayIsArray,
|
||||
ArrayPrototypeMap,
|
||||
ArrayPrototypePush,
|
||||
ArrayPrototypeSplice,
|
||||
BigUint64Array,
|
||||
Float64Array,
|
||||
NumberMAX_SAFE_INTEGER,
|
||||
ObjectDefineProperties,
|
||||
ObjectDefineProperty,
|
||||
ObjectFreeze,
|
||||
ObjectGetOwnPropertyDescriptors,
|
||||
ReflectApply,
|
||||
RegExpPrototypeTest,
|
||||
Set,
|
||||
SetPrototype,
|
||||
SetPrototypeHas,
|
||||
SafeSet,
|
||||
StringPrototypeEndsWith,
|
||||
StringPrototypeReplace,
|
||||
StringPrototypeSlice,
|
||||
StringPrototypeStartsWith,
|
||||
Uint32Array,
|
||||
} = primordials;
|
||||
|
||||
@@ -94,7 +97,7 @@ function wrapProcessMethods(binding) {
|
||||
} = binding;
|
||||
|
||||
function _rawDebug(...args) {
|
||||
binding._rawDebug(format.apply(null, args));
|
||||
binding._rawDebug(ReflectApply(format, null, args));
|
||||
}
|
||||
|
||||
// Create the argument array that will be passed to the native function.
|
||||
@@ -256,19 +259,19 @@ function buildAllowedFlags() {
|
||||
const allowedNodeEnvironmentFlags = [];
|
||||
for (const [name, info] of options) {
|
||||
if (info.envVarSettings === kAllowedInEnvironment) {
|
||||
allowedNodeEnvironmentFlags.push(name);
|
||||
ArrayPrototypePush(allowedNodeEnvironmentFlags, name);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [ from, expansion ] of aliases) {
|
||||
let isAccepted = true;
|
||||
for (const to of expansion) {
|
||||
if (!to.startsWith('-') || to === '--') continue;
|
||||
if (!StringPrototypeStartsWith(to, '-') || to === '--') continue;
|
||||
const recursiveExpansion = aliases.get(to);
|
||||
if (recursiveExpansion) {
|
||||
if (recursiveExpansion[0] === to)
|
||||
recursiveExpansion.splice(0, 1);
|
||||
expansion.push(...recursiveExpansion);
|
||||
ArrayPrototypeSplice(recursiveExpansion, 0, 1);
|
||||
ArrayPrototypePush(expansion, ...recursiveExpansion);
|
||||
continue;
|
||||
}
|
||||
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
|
||||
@@ -276,11 +279,11 @@ function buildAllowedFlags() {
|
||||
}
|
||||
if (isAccepted) {
|
||||
let canonical = from;
|
||||
if (canonical.endsWith('='))
|
||||
canonical = canonical.substr(0, canonical.length - 1);
|
||||
if (canonical.endsWith(' <arg>'))
|
||||
canonical = canonical.substr(0, canonical.length - 4);
|
||||
allowedNodeEnvironmentFlags.push(canonical);
|
||||
if (StringPrototypeEndsWith(canonical, '='))
|
||||
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1);
|
||||
if (StringPrototypeEndsWith(canonical, ' <arg>'))
|
||||
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 4);
|
||||
ArrayPrototypePush(allowedNodeEnvironmentFlags, canonical);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,14 +292,10 @@ function buildAllowedFlags() {
|
||||
|
||||
// Save these for comparison against flags provided to
|
||||
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
|
||||
// Avoid interference w/ user code by flattening `Set.prototype` into
|
||||
// each object.
|
||||
const nodeFlags = ObjectDefineProperties(
|
||||
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
|
||||
ObjectGetOwnPropertyDescriptors(SetPrototype)
|
||||
);
|
||||
const nodeFlags = new SafeSet(ArrayPrototypeMap(allowedNodeEnvironmentFlags,
|
||||
trimLeadingDashes));
|
||||
|
||||
class NodeEnvironmentFlagsSet extends Set {
|
||||
class NodeEnvironmentFlagsSet extends SafeSet {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
|
||||
@@ -328,9 +327,9 @@ function buildAllowedFlags() {
|
||||
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
|
||||
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
|
||||
key = StringPrototypeReplace(key, trailingValuesRegex, '');
|
||||
return SetPrototypeHas(this, key);
|
||||
return super.has(key);
|
||||
}
|
||||
return SetPrototypeHas(nodeFlags, key);
|
||||
return nodeFlags.has(key);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypePush,
|
||||
ArrayPrototypeShift,
|
||||
Error,
|
||||
ObjectDefineProperty,
|
||||
WeakMap,
|
||||
SafeWeakMap,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -25,7 +27,7 @@ const {
|
||||
// *Must* match Environment::TickInfo::Fields in src/env.h.
|
||||
const kHasRejectionToWarn = 1;
|
||||
|
||||
const maybeUnhandledPromises = new WeakMap();
|
||||
const maybeUnhandledPromises = new SafeWeakMap();
|
||||
const pendingUnhandledRejections = [];
|
||||
const asyncHandledRejections = [];
|
||||
let lastPromiseId = 0;
|
||||
@@ -121,7 +123,7 @@ function unhandledRejection(promise, reason) {
|
||||
domain: process.domain
|
||||
});
|
||||
// This causes the promise to be referenced at least for one tick.
|
||||
pendingUnhandledRejections.push(promise);
|
||||
ArrayPrototypePush(pendingUnhandledRejections, promise);
|
||||
setHasRejectionToWarn(true);
|
||||
}
|
||||
|
||||
@@ -137,7 +139,7 @@ function handledRejection(promise) {
|
||||
`asynchronously (rejection id: ${uid})`);
|
||||
warning.name = 'PromiseRejectionHandledWarning';
|
||||
warning.id = uid;
|
||||
asyncHandledRejections.push({ promise, warning });
|
||||
ArrayPrototypePush(asyncHandledRejections, { promise, warning });
|
||||
setHasRejectionToWarn(true);
|
||||
return;
|
||||
}
|
||||
@@ -178,7 +180,7 @@ function processPromiseRejections() {
|
||||
let maybeScheduledTicksOrMicrotasks = asyncHandledRejections.length > 0;
|
||||
|
||||
while (asyncHandledRejections.length > 0) {
|
||||
const { promise, warning } = asyncHandledRejections.shift();
|
||||
const { promise, warning } = ArrayPrototypeShift(asyncHandledRejections);
|
||||
if (!process.emit('rejectionHandled', promise)) {
|
||||
process.emitWarning(warning);
|
||||
}
|
||||
@@ -186,7 +188,7 @@ function processPromiseRejections() {
|
||||
|
||||
let len = pendingUnhandledRejections.length;
|
||||
while (len--) {
|
||||
const promise = pendingUnhandledRejections.shift();
|
||||
const promise = ArrayPrototypeShift(pendingUnhandledRejections);
|
||||
const promiseInfo = maybeUnhandledPromises.get(promise);
|
||||
if (promiseInfo === undefined) {
|
||||
continue;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
Map,
|
||||
FunctionPrototypeBind,
|
||||
SafeMap,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -11,7 +12,7 @@ const {
|
||||
const { signals } = internalBinding('constants').os;
|
||||
|
||||
let Signal;
|
||||
const signalWraps = new Map();
|
||||
const signalWraps = new SafeMap();
|
||||
|
||||
function isSignal(event) {
|
||||
return typeof event === 'string' && signals[event] !== undefined;
|
||||
@@ -26,7 +27,7 @@ function startListeningIfSignal(type) {
|
||||
|
||||
wrap.unref();
|
||||
|
||||
wrap.onsignal = process.emit.bind(process, type, type);
|
||||
wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type);
|
||||
|
||||
const signum = signals[type];
|
||||
const err = wrap.start(signum);
|
||||
|
||||
Reference in new issue
Block a user