Skip to content

Commit 292915a

Browse files
aduh95danielleadams
authored andcommitted
bootstrap: refactor to use more primordials
PR-URL: #35999 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe>
1 parent f73b8d8 commit 292915a

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

lib/internal/bootstrap/loaders.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,18 @@
4444
/* global process, getLinkedBinding, getInternalBinding, primordials */
4545

4646
const {
47+
ArrayPrototypeMap,
48+
ArrayPrototypePush,
4749
Error,
48-
Map,
4950
ObjectCreate,
5051
ObjectDefineProperty,
5152
ObjectKeys,
5253
ObjectPrototypeHasOwnProperty,
5354
ReflectGet,
55+
SafeMap,
5456
SafeSet,
5557
String,
58+
StringPrototypeStartsWith,
5659
TypeError,
5760
} = primordials;
5861

@@ -135,7 +138,7 @@ let internalBinding;
135138
let mod = bindingObj[module];
136139
if (typeof mod !== 'object') {
137140
mod = bindingObj[module] = getInternalBinding(module);
138-
moduleLoadList.push(`Internal Binding ${module}`);
141+
ArrayPrototypePush(moduleLoadList, `Internal Binding ${module}`);
139142
}
140143
return mod;
141144
};
@@ -163,12 +166,14 @@ class NativeModule {
163166
* A map from the module IDs to the module instances.
164167
* @type {Map<string, NativeModule>}
165168
*/
166-
static map = new Map(moduleIds.map((id) => [id, new NativeModule(id)]));
169+
static map = new SafeMap(
170+
ArrayPrototypeMap(moduleIds, (id) => [id, new NativeModule(id)])
171+
);
167172

168173
constructor(id) {
169174
this.filename = `${id}.js`;
170175
this.id = id;
171-
this.canBeRequiredByUsers = !id.startsWith('internal/');
176+
this.canBeRequiredByUsers = !StringPrototypeStartsWith(id, 'internal/');
172177

173178
// The CJS exports object of the module.
174179
this.exports = {};
@@ -221,7 +226,7 @@ class NativeModule {
221226
if (!this.exportKeys) {
222227
// When using --expose-internals, we do not want to reflect the named
223228
// exports from core modules as this can trigger unnecessary getters.
224-
const internal = this.id.startsWith('internal/');
229+
const internal = StringPrototypeStartsWith(this.id, 'internal/');
225230
this.exportKeys = internal ? [] : ObjectKeys(this.exports);
226231
}
227232
this.getESMFacade();
@@ -271,7 +276,7 @@ class NativeModule {
271276
this.loading = true;
272277

273278
try {
274-
const requireFn = this.id.startsWith('internal/deps/') ?
279+
const requireFn = StringPrototypeStartsWith(this.id, 'internal/deps/') ?
275280
requireWithFallbackInDeps : nativeModuleRequire;
276281

277282
const fn = compileFunction(id);
@@ -282,7 +287,7 @@ class NativeModule {
282287
this.loading = false;
283288
}
284289

285-
moduleLoadList.push(`NativeModule ${id}`);
290+
ArrayPrototypePush(moduleLoadList, `NativeModule ${id}`);
286291
return this.exports;
287292
}
288293
}

lib/internal/bootstrap/node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
setupPrepareStackTrace();
4040

4141
const {
42+
FunctionPrototypeCall,
4243
JSONParse,
4344
ObjectDefineProperty,
4445
ObjectGetPrototypeOf,
@@ -299,7 +300,7 @@ function setupProcessObject() {
299300
const EventEmitter = require('events');
300301
const origProcProto = ObjectGetPrototypeOf(process);
301302
ObjectSetPrototypeOf(origProcProto, EventEmitter.prototype);
302-
EventEmitter.call(process);
303+
FunctionPrototypeCall(EventEmitter, process);
303304
ObjectDefineProperty(process, SymbolToStringTag, {
304305
enumerable: false,
305306
writable: true,

lib/internal/bootstrap/pre_execution.js

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

33
const {
4-
Map,
54
NumberParseInt,
65
ObjectDefineProperty,
6+
SafeMap,
77
SafeWeakMap,
8+
StringPrototypeStartsWith,
89
} = primordials;
910

1011
const {
@@ -96,7 +97,8 @@ function patchProcessObject(expandArgv1) {
9697
});
9798
process.argv[0] = process.execPath;
9899

99-
if (expandArgv1 && process.argv[1] && !process.argv[1].startsWith('-')) {
100+
if (expandArgv1 && process.argv[1] &&
101+
!StringPrototypeStartsWith(process.argv[1], '-')) {
100102
// Expand process.argv[1] into a full path.
101103
const path = require('path');
102104
try {
@@ -357,7 +359,7 @@ function initializePolicy() {
357359
if (experimentalPolicy) {
358360
process.emitWarning('Policies are experimental.',
359361
'ExperimentalWarning');
360-
const { pathToFileURL, URL } = require('url');
362+
const { pathToFileURL, URL } = require('internal/url');
361363
// URL here as it is slightly different parsing
362364
// no bare specifiers for now
363365
let manifestURL;
@@ -374,7 +376,7 @@ function initializePolicy() {
374376
if (experimentalPolicyIntegrity) {
375377
const SRI = require('internal/policy/sri');
376378
const { createHash, timingSafeEqual } = require('crypto');
377-
const realIntegrities = new Map();
379+
const realIntegrities = new SafeMap();
378380
const integrityEntries = SRI.parse(experimentalPolicyIntegrity);
379381
let foundMatch = false;
380382
for (let i = 0; i < integrityEntries.length; i++) {

0 commit comments

Comments
 (0)