Skip to content

Commit 39d0ced

Browse files
aduh95danielleadams
authored andcommitted
process: refactor to use more primordials
PR-URL: #36212 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Pranshu Srivastava <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 69a8f05 commit 39d0ced

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

Diff for: lib/internal/process/per_thread.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66

77
const {
88
ArrayIsArray,
9+
ArrayPrototypeMap,
10+
ArrayPrototypePush,
11+
ArrayPrototypeSplice,
912
BigUint64Array,
1013
Float64Array,
1114
NumberMAX_SAFE_INTEGER,
12-
ObjectDefineProperties,
1315
ObjectDefineProperty,
1416
ObjectFreeze,
15-
ObjectGetOwnPropertyDescriptors,
17+
ReflectApply,
1618
RegExpPrototypeTest,
17-
Set,
18-
SetPrototype,
19-
SetPrototypeHas,
19+
SafeSet,
20+
StringPrototypeEndsWith,
2021
StringPrototypeReplace,
22+
StringPrototypeSlice,
23+
StringPrototypeStartsWith,
2124
Uint32Array,
2225
} = primordials;
2326

@@ -94,7 +97,7 @@ function wrapProcessMethods(binding) {
9497
} = binding;
9598

9699
function _rawDebug(...args) {
97-
binding._rawDebug(format.apply(null, args));
100+
binding._rawDebug(ReflectApply(format, null, args));
98101
}
99102

100103
// Create the argument array that will be passed to the native function.
@@ -256,31 +259,31 @@ function buildAllowedFlags() {
256259
const allowedNodeEnvironmentFlags = [];
257260
for (const [name, info] of options) {
258261
if (info.envVarSettings === kAllowedInEnvironment) {
259-
allowedNodeEnvironmentFlags.push(name);
262+
ArrayPrototypePush(allowedNodeEnvironmentFlags, name);
260263
}
261264
}
262265

263266
for (const [ from, expansion ] of aliases) {
264267
let isAccepted = true;
265268
for (const to of expansion) {
266-
if (!to.startsWith('-') || to === '--') continue;
269+
if (!StringPrototypeStartsWith(to, '-') || to === '--') continue;
267270
const recursiveExpansion = aliases.get(to);
268271
if (recursiveExpansion) {
269272
if (recursiveExpansion[0] === to)
270-
recursiveExpansion.splice(0, 1);
271-
expansion.push(...recursiveExpansion);
273+
ArrayPrototypeSplice(recursiveExpansion, 0, 1);
274+
ArrayPrototypePush(expansion, ...recursiveExpansion);
272275
continue;
273276
}
274277
isAccepted = options.get(to).envVarSettings === kAllowedInEnvironment;
275278
if (!isAccepted) break;
276279
}
277280
if (isAccepted) {
278281
let canonical = from;
279-
if (canonical.endsWith('='))
280-
canonical = canonical.substr(0, canonical.length - 1);
281-
if (canonical.endsWith(' <arg>'))
282-
canonical = canonical.substr(0, canonical.length - 4);
283-
allowedNodeEnvironmentFlags.push(canonical);
282+
if (StringPrototypeEndsWith(canonical, '='))
283+
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 1);
284+
if (StringPrototypeEndsWith(canonical, ' <arg>'))
285+
canonical = StringPrototypeSlice(canonical, 0, canonical.length - 4);
286+
ArrayPrototypePush(allowedNodeEnvironmentFlags, canonical);
284287
}
285288
}
286289

@@ -289,14 +292,10 @@ function buildAllowedFlags() {
289292

290293
// Save these for comparison against flags provided to
291294
// process.allowedNodeEnvironmentFlags.has() which lack leading dashes.
292-
// Avoid interference w/ user code by flattening `Set.prototype` into
293-
// each object.
294-
const nodeFlags = ObjectDefineProperties(
295-
new Set(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
296-
ObjectGetOwnPropertyDescriptors(SetPrototype)
297-
);
298-
299-
class NodeEnvironmentFlagsSet extends Set {
295+
const nodeFlags = new SafeSet(ArrayPrototypeMap(allowedNodeEnvironmentFlags,
296+
trimLeadingDashes));
297+
298+
class NodeEnvironmentFlagsSet extends SafeSet {
300299
constructor(...args) {
301300
super(...args);
302301

@@ -328,9 +327,9 @@ function buildAllowedFlags() {
328327
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
329328
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
330329
key = StringPrototypeReplace(key, trailingValuesRegex, '');
331-
return SetPrototypeHas(this, key);
330+
return super.has(key);
332331
}
333-
return SetPrototypeHas(nodeFlags, key);
332+
return nodeFlags.has(key);
334333
}
335334
return false;
336335
}

Diff for: lib/internal/process/promises.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypePush,
5+
ArrayPrototypeShift,
46
Error,
57
ObjectDefineProperty,
6-
WeakMap,
8+
SafeWeakMap,
79
} = primordials;
810

911
const {
@@ -25,7 +27,7 @@ const {
2527
// *Must* match Environment::TickInfo::Fields in src/env.h.
2628
const kHasRejectionToWarn = 1;
2729

28-
const maybeUnhandledPromises = new WeakMap();
30+
const maybeUnhandledPromises = new SafeWeakMap();
2931
const pendingUnhandledRejections = [];
3032
const asyncHandledRejections = [];
3133
let lastPromiseId = 0;
@@ -121,7 +123,7 @@ function unhandledRejection(promise, reason) {
121123
domain: process.domain
122124
});
123125
// This causes the promise to be referenced at least for one tick.
124-
pendingUnhandledRejections.push(promise);
126+
ArrayPrototypePush(pendingUnhandledRejections, promise);
125127
setHasRejectionToWarn(true);
126128
}
127129

@@ -137,7 +139,7 @@ function handledRejection(promise) {
137139
`asynchronously (rejection id: ${uid})`);
138140
warning.name = 'PromiseRejectionHandledWarning';
139141
warning.id = uid;
140-
asyncHandledRejections.push({ promise, warning });
142+
ArrayPrototypePush(asyncHandledRejections, { promise, warning });
141143
setHasRejectionToWarn(true);
142144
return;
143145
}
@@ -178,15 +180,15 @@ function processPromiseRejections() {
178180
let maybeScheduledTicksOrMicrotasks = asyncHandledRejections.length > 0;
179181

180182
while (asyncHandledRejections.length > 0) {
181-
const { promise, warning } = asyncHandledRejections.shift();
183+
const { promise, warning } = ArrayPrototypeShift(asyncHandledRejections);
182184
if (!process.emit('rejectionHandled', promise)) {
183185
process.emitWarning(warning);
184186
}
185187
}
186188

187189
let len = pendingUnhandledRejections.length;
188190
while (len--) {
189-
const promise = pendingUnhandledRejections.shift();
191+
const promise = ArrayPrototypeShift(pendingUnhandledRejections);
190192
const promiseInfo = maybeUnhandledPromises.get(promise);
191193
if (promiseInfo === undefined) {
192194
continue;

Diff for: lib/internal/process/signal.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
const {
4-
Map,
4+
FunctionPrototypeBind,
5+
SafeMap,
56
} = primordials;
67

78
const {
@@ -11,7 +12,7 @@ const {
1112
const { signals } = internalBinding('constants').os;
1213

1314
let Signal;
14-
const signalWraps = new Map();
15+
const signalWraps = new SafeMap();
1516

1617
function isSignal(event) {
1718
return typeof event === 'string' && signals[event] !== undefined;
@@ -26,7 +27,7 @@ function startListeningIfSignal(type) {
2627

2728
wrap.unref();
2829

29-
wrap.onsignal = process.emit.bind(process, type, type);
30+
wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type);
3031

3132
const signum = signals[type];
3233
const err = wrap.start(signum);

0 commit comments

Comments
 (0)