Skip to content

Commit

Permalink
fixup! esm,loaders: tidy ESMLoader internals
Browse files Browse the repository at this point in the history
ObjectCreate(null) → {__proto__:null}
  • Loading branch information
JakobJingleheimer committed Sep 17, 2022
1 parent 687def8 commit 8cdb4ba
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 29 deletions.
4 changes: 1 addition & 3 deletions lib/internal/modules/esm/assert.js
Expand Up @@ -3,7 +3,6 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ObjectCreate,
ObjectValues,
ObjectPrototypeHasOwnProperty,
} = primordials;
Expand Down Expand Up @@ -52,8 +51,7 @@ const supportedAssertionTypes = ArrayPrototypeFilter(
* @returns {true}
* @throws {TypeError} If the format and assertion type are incompatible.
*/
function validateAssertions(url, format,
importAssertions = ObjectCreate(null)) {
function validateAssertions(url, format, importAssertions = { __proto__: null }) {
const validType = formatTypeMap[format];

switch (validType) {
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/modules/esm/create_dynamic_module.js
Expand Up @@ -4,7 +4,6 @@ const {
ArrayPrototypeJoin,
ArrayPrototypeMap,
JSONStringify,
ObjectCreate,
SafeSet,
} = primordials;

Expand Down Expand Up @@ -40,12 +39,12 @@ import.meta.done();

const readyfns = new SafeSet();
const reflect = {
exports: ObjectCreate(null),
exports: { __proto__: null },
onReady: (cb) => { readyfns.add(cb); },
};

if (imports.length)
reflect.imports = ObjectCreate(null);
reflect.imports = { __proto__: null };

callbackMap.set(m, {
initializeImportMeta: (meta, wrap) => {
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/modules/esm/get_format.js
@@ -1,8 +1,6 @@
'use strict';
const {
RegExpPrototypeExec,
ObjectAssign,
ObjectCreate,
ObjectPrototypeHasOwnProperty,
PromisePrototypeThen,
PromiseResolve,
Expand All @@ -25,13 +23,14 @@ const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/
const { URL, fileURLToPath } = require('internal/url');
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;

const protocolHandlers = ObjectAssign(ObjectCreate(null), {
const protocolHandlers = {
'__proto__': null,
'data:': getDataProtocolModuleFormat,
'file:': getFileProtocolModuleFormat,
'http:': getHttpProtocolModuleFormat,
'https:': getHttpProtocolModuleFormat,
'node:'() { return 'builtin'; },
});
};

/**
* @param {URL} parsed
Expand Down
17 changes: 8 additions & 9 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -10,7 +10,6 @@ const {
ArrayPrototypePush,
FunctionPrototypeCall,
ObjectAssign,
ObjectCreate,
ObjectDefineProperty,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
Expand Down Expand Up @@ -269,7 +268,7 @@ class ESMLoader {
transformSource,
}) {
const obsoleteHooks = [];
const acceptedHooks = ObjectCreate(null);
const acceptedHooks = { __proto__: null };

if (getGlobalPreloadCode) {
globalPreload ??= getGlobalPreloadCode;
Expand Down Expand Up @@ -414,7 +413,7 @@ class ESMLoader {
// We can skip cloning if there are no user-provided loaders because
// the Node.js default resolve hook does not use import assertions.
importAssertionsForResolve = ObjectAssign(
ObjectCreate(null),
{ __proto__: null },
importAssertions,
);
}
Expand Down Expand Up @@ -531,11 +530,11 @@ class ESMLoader {
if (!wasArr) { return namespaces[0]; } // We can skip the pairing below

for (let i = 0; i < count; i++) {
const namespace = ObjectCreate(null);
namespace.url = specifiers[i];
namespace.exports = namespaces[i];

namespaces[i] = namespace;
namespaces[i] = {
__proto__: null,
url: specifiers[i],
exports: namespaces[i],
};
}

return namespaces;
Expand Down Expand Up @@ -787,7 +786,7 @@ class ESMLoader {
async resolve(
originalSpecifier,
parentURL,
importAssertions = ObjectCreate(null)
importAssertions = { __proto__: null }
) {
const isMain = parentURL === undefined;

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/esm/module_job.js
Expand Up @@ -5,7 +5,6 @@ const {
ArrayPrototypePush,
ArrayPrototypeSome,
FunctionPrototype,
ObjectCreate,
ObjectSetPrototypeOf,
PromiseResolve,
PromisePrototypeThen,
Expand Down Expand Up @@ -50,7 +49,7 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
class ModuleJob {
// `loader` is the Loader instance used for loading dependencies.
// `moduleProvider` is a function
constructor(loader, url, importAssertions = ObjectCreate(null),
constructor(loader, url, importAssertions = { __proto: null },
moduleProvider, isMain, inspectBrk) {
this.loader = loader;
this.importAssertions = importAssertions;
Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/run_main.js
@@ -1,7 +1,6 @@
'use strict';

const {
ObjectCreate,
StringPrototypeEndsWith,
} = primordials;
const CJSLoader = require('internal/modules/cjs/loader');
Expand Down Expand Up @@ -60,7 +59,7 @@ function runMainESM(mainPath) {
handleMainPromise(loadESM((esmLoader) => {
const main = path.isAbsolute(mainPath) ?
pathToFileURL(mainPath).href : mainPath;
return esmLoader.import(main, undefined, ObjectCreate(null));
return esmLoader.import(main, undefined, { __proto__: null });
}));
}

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/process/esm_loader.js
Expand Up @@ -2,7 +2,6 @@

const {
ArrayIsArray,
ObjectCreate,
} = primordials;

const {
Expand Down Expand Up @@ -92,7 +91,7 @@ function loadModulesInIsolation(specifiers, loaders = []) {
return internalEsmLoader.import(
specifiers,
pathToFileURL(cwd).href,
ObjectCreate(null),
{ __proto__: null },
);
}

Expand Down
8 changes: 4 additions & 4 deletions test/es-module/test-esm-loader-hooks.mjs
Expand Up @@ -12,10 +12,10 @@ const { ESMLoader } = esmLoaderModule;
const esmLoader = new ESMLoader();

const originalSpecifier = 'foo/bar';
const importAssertions = Object.assign(
Object.create(null),
{ type: 'json' },
);
const importAssertions = {
__proto__: null,
type: 'json',
};
const parentURL = 'file:///entrypoint.js';
const resolvedURL = 'file:///foo/bar.js';
const suggestedFormat = 'test';
Expand Down

0 comments on commit 8cdb4ba

Please sign in to comment.