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

‎lib/internal/process/per_thread.js‎

Lines changed: 24 additions & 25 deletions
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
constallowedNodeEnvironmentFlags=[];
257260
for(const[name,info]ofoptions){
258261
if(info.envVarSettings===kAllowedInEnvironment){
259-
allowedNodeEnvironmentFlags.push(name);
262+
ArrayPrototypePush(allowedNodeEnvironmentFlags,name);
260263
}
261264
}
262265

263266
for(const[from,expansion]ofaliases){
264267
letisAccepted=true;
265268
for(consttoofexpansion){
266-
if(!to.startsWith('-')||to==='--')continue;
269+
if(!StringPrototypeStartsWith(to,'-')||to==='--')continue;
267270
constrecursiveExpansion=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
letcanonical=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-
constnodeFlags=ObjectDefineProperties(
295-
newSet(allowedNodeEnvironmentFlags.map(trimLeadingDashes)),
296-
ObjectGetOwnPropertyDescriptors(SetPrototype)
297-
);
298-
299-
classNodeEnvironmentFlagsSetextendsSet{
295+
constnodeFlags=newSafeSet(ArrayPrototypeMap(allowedNodeEnvironmentFlags,
296+
trimLeadingDashes));
297+
298+
classNodeEnvironmentFlagsSetextendsSafeSet{
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-
returnSetPrototypeHas(this,key);
330+
returnsuper.has(key);
332331
}
333-
returnSetPrototypeHas(nodeFlags,key);
332+
returnnodeFlags.has(key);
334333
}
335334
returnfalse;
336335
}

‎lib/internal/process/promises.js‎

Lines changed: 8 additions & 6 deletions
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
constkHasRejectionToWarn=1;
2729

28-
constmaybeUnhandledPromises=newWeakMap();
30+
constmaybeUnhandledPromises=newSafeWeakMap();
2931
constpendingUnhandledRejections=[];
3032
constasyncHandledRejections=[];
3133
letlastPromiseId=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
letmaybeScheduledTicksOrMicrotasks=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
letlen=pendingUnhandledRejections.length;
188190
while(len--){
189-
constpromise=pendingUnhandledRejections.shift();
191+
constpromise=ArrayPrototypeShift(pendingUnhandledRejections);
190192
constpromiseInfo=maybeUnhandledPromises.get(promise);
191193
if(promiseInfo===undefined){
192194
continue;

‎lib/internal/process/signal.js‎

Lines changed: 4 additions & 3 deletions
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
letSignal;
14-
constsignalWraps=newMap();
15+
constsignalWraps=newSafeMap();
1516

1617
functionisSignal(event){
1718
returntypeofevent==='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
constsignum=signals[type];
3233
consterr=wrap.start(signum);

0 commit comments

Comments
(0)