Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aduh95 authored and BethGriggs committed Dec 15, 2020
1 parent 002005f commit 0477e00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
19 changes: 12 additions & 7 deletions lib/internal/bootstrap/loaders.js
Expand Up @@ -44,15 +44,18 @@
/* global process, getLinkedBinding, getInternalBinding, primordials */

const {
ArrayPrototypeMap,
ArrayPrototypePush,
Error,
Map,
ObjectCreate,
ObjectDefineProperty,
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ReflectGet,
SafeMap,
SafeSet,
String,
StringPrototypeStartsWith,
TypeError,
} = primordials;

Expand Down Expand Up @@ -135,7 +138,7 @@ let internalBinding;
let mod = bindingObj[module];
if (typeof mod !== 'object') {
mod = bindingObj[module] = getInternalBinding(module);
moduleLoadList.push(`Internal Binding ${module}`);
ArrayPrototypePush(moduleLoadList, `Internal Binding ${module}`);
}
return mod;
};
Expand Down Expand Up @@ -163,12 +166,14 @@ class NativeModule {
* A map from the module IDs to the module instances.
* @type {Map<string, NativeModule>}
*/
static map = new Map(moduleIds.map((id) => [id, new NativeModule(id)]));
static map = new SafeMap(
ArrayPrototypeMap(moduleIds, (id) => [id, new NativeModule(id)])
);

constructor(id) {
this.filename = `${id}.js`;
this.id = id;
this.canBeRequiredByUsers = !id.startsWith('internal/');
this.canBeRequiredByUsers = !StringPrototypeStartsWith(id, 'internal/');

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

try {
const requireFn = this.id.startsWith('internal/deps/') ?
const requireFn = StringPrototypeStartsWith(this.id, 'internal/deps/') ?
requireWithFallbackInDeps : nativeModuleRequire;

const fn = compileFunction(id);
Expand All @@ -282,7 +287,7 @@ class NativeModule {
this.loading = false;
}

moduleLoadList.push(`NativeModule ${id}`);
ArrayPrototypePush(moduleLoadList, `NativeModule ${id}`);
return this.exports;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/node.js
Expand Up @@ -39,6 +39,7 @@
setupPrepareStackTrace();

const {
FunctionPrototypeCall,
JSONParse,
ObjectDefineProperty,
ObjectGetPrototypeOf,
Expand Down Expand Up @@ -271,7 +272,7 @@ function setupProcessObject() {
const EventEmitter = require('events');
const origProcProto = ObjectGetPrototypeOf(process);
ObjectSetPrototypeOf(origProcProto, EventEmitter.prototype);
EventEmitter.call(process);
FunctionPrototypeCall(EventEmitter, process);
ObjectDefineProperty(process, SymbolToStringTag, {
enumerable: false,
writable: true,
Expand Down
10 changes: 6 additions & 4 deletions lib/internal/bootstrap/pre_execution.js
@@ -1,10 +1,11 @@
'use strict';

const {
Map,
NumberParseInt,
ObjectDefineProperty,
SafeMap,
SafeWeakMap,
StringPrototypeStartsWith,
} = primordials;

const {
Expand Down Expand Up @@ -90,7 +91,8 @@ function patchProcessObject(expandArgv1) {
});
process.argv[0] = process.execPath;

if (expandArgv1 && process.argv[1] && !process.argv[1].startsWith('-')) {
if (expandArgv1 && process.argv[1] &&
!StringPrototypeStartsWith(process.argv[1], '-')) {
// Expand process.argv[1] into a full path.
const path = require('path');
try {
Expand Down Expand Up @@ -351,7 +353,7 @@ function initializePolicy() {
if (experimentalPolicy) {
process.emitWarning('Policies are experimental.',
'ExperimentalWarning');
const { pathToFileURL, URL } = require('url');
const { pathToFileURL, URL } = require('internal/url');
// URL here as it is slightly different parsing
// no bare specifiers for now
let manifestURL;
Expand All @@ -368,7 +370,7 @@ function initializePolicy() {
if (experimentalPolicyIntegrity) {
const SRI = require('internal/policy/sri');
const { createHash, timingSafeEqual } = require('crypto');
const realIntegrities = new Map();
const realIntegrities = new SafeMap();
const integrityEntries = SRI.parse(experimentalPolicyIntegrity);
let foundMatch = false;
for (let i = 0; i < integrityEntries.length; i++) {
Expand Down

0 comments on commit 0477e00

Please sign in to comment.