Skip to content

Commit 6678897

Browse files
aduh95codebytere
authored andcommitted
esm: refactor to use more primordials
PR-URL: #36019 Reviewed-By: Rich Trott <[email protected]>
1 parent 75707f4 commit 6678897

File tree

8 files changed

+50
-26
lines changed

8 files changed

+50
-26
lines changed

‎lib/internal/modules/esm/create_dynamic_module.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const{
55
ArrayPrototypeMap,
66
JSONStringify,
77
ObjectCreate,
8-
Set,
8+
SafeSet,
99
}=primordials;
1010

1111
letdebug=require('internal/util/debuglog').debuglog('esm',(fn)=>{
@@ -38,7 +38,7 @@ import.meta.done();
3838
const{ ModuleWrap, callbackMap }=internalBinding('module_wrap');
3939
constm=newModuleWrap(`${url}`,undefined,source,0,0);
4040

41-
constreadyfns=newSet();
41+
constreadyfns=newSafeSet();
4242
constreflect={
4343
exports: ObjectCreate(null),
4444
onReady: (cb)=>{readyfns.add(cb);},

‎lib/internal/modules/esm/get_format.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
2-
const{ StringPrototypeStartsWith }=primordials;
2+
const{
3+
RegExpPrototypeExec,
4+
StringPrototypeStartsWith,
5+
}=primordials;
36
const{ extname }=require('path');
47
const{ getOptionValue }=require('internal/options');
58

@@ -39,7 +42,10 @@ function defaultGetFormat(url, context, defaultGetFormatUnused){
3942
}
4043
constparsed=newURL(url);
4144
if(parsed.protocol==='data:'){
42-
const[,mime]=/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname)||[null,null,null];
45+
const[,mime]=RegExpPrototypeExec(
46+
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
47+
parsed.pathname,
48+
)||[null,null,null];
4349
constformat=({
4450
'__proto__': null,
4551
'text/javascript': 'module',

‎lib/internal/modules/esm/get_source.js‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use strict';
22

3+
const{
4+
RegExpPrototypeExec,
5+
}=primordials;
36
const{ getOptionValue }=require('internal/options');
47
// Do not eagerly grab .manifest, it may be in TDZ
58
constpolicy=getOptionValue('--experimental-policy') ?
@@ -8,14 +11,13 @@ const policy = getOptionValue('--experimental-policy') ?
811

912
const{ Buffer }=require('buffer');
1013

11-
constfs=require('fs');
12-
const{URL}=require('url');
13-
const{ promisify }=require('internal/util');
14+
constfs=require('internal/fs/promises').exports;
15+
const{URL}=require('internal/url');
1416
const{
1517
ERR_INVALID_URL,
1618
ERR_INVALID_URL_SCHEME,
1719
}=require('internal/errors').codes;
18-
constreadFileAsync=promisify(fs.readFile);
20+
constreadFileAsync=fs.readFile;
1921

2022
constDATA_URL_PATTERN=/^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
2123

@@ -25,7 +27,7 @@ async function defaultGetSource(url,{format } ={}, defaultGetSource){
2527
if(parsed.protocol==='file:'){
2628
source=awaitreadFileAsync(parsed);
2729
}elseif(parsed.protocol==='data:'){
28-
constmatch=DATA_URL_PATTERN.exec(parsed.pathname);
30+
constmatch=RegExpPrototypeExec(DATA_URL_PATTERN,parsed.pathname);
2931
if(!match){
3032
thrownewERR_INVALID_URL(url);
3133
}

‎lib/internal/modules/esm/loader.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const{
77
FunctionPrototypeBind,
88
ObjectSetPrototypeOf,
99
SafeWeakMap,
10+
StringPrototypeStartsWith,
1011
}=primordials;
1112

1213
const{
@@ -126,8 +127,8 @@ class Loader{
126127
}
127128

128129
if(this._resolve===defaultResolve&&
129-
!url.startsWith('file:')&&
130-
!url.startsWith('data:')
130+
!StringPrototypeStartsWith(url,'file:')&&
131+
!StringPrototypeStartsWith(url,'data:')
131132
){
132133
thrownewERR_INVALID_RETURN_PROPERTY(
133134
'file: or data: url','loader resolve','url',url

‎lib/internal/modules/esm/module_job.js‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
const{
44
ArrayPrototypeJoin,
5+
ArrayPrototypeMap,
6+
ArrayPrototypePush,
7+
FunctionPrototype,
58
ObjectSetPrototypeOf,
69
PromiseAll,
10+
PromiseResolve,
11+
PromisePrototypeCatch,
12+
ReflectApply,
713
SafeSet,
8-
SafePromise,
914
StringPrototypeIncludes,
1015
StringPrototypeMatch,
1116
StringPrototypeReplace,
@@ -16,9 +21,9 @@ const{ModuleWrap } = internalBinding('module_wrap');
1621

1722
const{ decorateErrorStack }=require('internal/util');
1823
constassert=require('internal/assert');
19-
constresolvedPromise=SafePromise.resolve();
24+
constresolvedPromise=PromiseResolve();
2025

21-
functionnoop(){}
26+
constnoop=FunctionPrototype;
2227

2328
lethasPausedEntry=false;
2429

@@ -35,7 +40,7 @@ class ModuleJob{
3540
this.module=undefined;
3641
// Expose the promise to the ModuleWrap directly for linking below.
3742
// `this.module` is also filled in below.
38-
this.modulePromise=moduleProvider.call(loader,url,isMain);
43+
this.modulePromise=ReflectApply(moduleProvider,loader,[url,isMain]);
3944

4045
// Wait for the ModuleWrap instance being linked with all dependencies.
4146
constlink=async()=>{
@@ -49,21 +54,21 @@ class ModuleJob{
4954
constdependencyJobs=[];
5055
constpromises=this.module.link(async(specifier)=>{
5156
constjobPromise=this.loader.getModuleJob(specifier,url);
52-
dependencyJobs.push(jobPromise);
57+
ArrayPrototypePush(dependencyJobs,jobPromise);
5358
constjob=awaitjobPromise;
5459
returnjob.modulePromise;
5560
});
5661

5762
if(promises!==undefined)
58-
awaitSafePromise.all(promises);
63+
awaitPromiseAll(promises);
5964

60-
returnSafePromise.all(dependencyJobs);
65+
returnPromiseAll(dependencyJobs);
6166
};
6267
// Promise for the list of all dependencyJobs.
6368
this.linked=link();
6469
// This promise is awaited later anyway, so silence
6570
// 'unhandled rejection' warnings.
66-
this.linked.catch(noop);
71+
PromisePrototypeCatch(this.linked,noop);
6772

6873
// instantiated == deep dependency jobs wrappers are instantiated,
6974
// and module wrapper is instantiated.
@@ -85,7 +90,8 @@ class ModuleJob{
8590
}
8691
jobsInGraph.add(moduleJob);
8792
constdependencyJobs=awaitmoduleJob.linked;
88-
returnPromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
93+
returnPromiseAll(
94+
ArrayPrototypeMap(dependencyJobs,addJobsToDependencyGraph));
8995
};
9096
awaitaddJobsToDependencyGraph(this);
9197

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,8 @@ function defaultResolve(specifier, context ={}, defaultResolveUnused){
880880
[internalFS.realpathCacheKey]: realpathCache
881881
});
882882
constold=url;
883-
url=pathToFileURL(real+(urlPath.endsWith(sep) ? '/' : ''));
883+
url=pathToFileURL(
884+
real+(StringPrototypeEndsWith(urlPath,sep) ? '/' : ''));
884885
url.search=old.search;
885886
url.hash=old.hash;
886887
}

‎lib/internal/modules/esm/translators.js‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* global WebAssembly */
44

55
const{
6+
ArrayPrototypeMap,
67
Boolean,
78
JSONParse,
89
ObjectGetPrototypeOf,
@@ -14,6 +15,7 @@ const{
1415
SafeMap,
1516
SafeSet,
1617
StringPrototypeReplace,
18+
StringPrototypeSlice,
1719
StringPrototypeSplit,
1820
StringPrototypeStartsWith,
1921
SyntaxErrorPrototype,
@@ -277,9 +279,9 @@ function cjsPreparseModuleExports(filename){
277279
translators.set('builtin',asyncfunctionbuiltinStrategy(url){
278280
debug(`Translating BuiltinModule ${url}`);
279281
// Slice 'node:' scheme
280-
constid=url.slice(5);
282+
constid=StringPrototypeSlice(url,5);
281283
constmodule=loadNativeModule(id,url,true);
282-
if(!url.startsWith('node:')||!module){
284+
if(!StringPrototypeStartsWith(url,'node:')||!module){
283285
thrownewERR_UNKNOWN_BUILTIN_MODULE(url);
284286
}
285287
debug(`Loading BuiltinModule ${url}`);
@@ -291,7 +293,8 @@ translators.set('json', async function jsonStrategy(url){
291293
emitExperimentalWarning('Importing JSON modules');
292294
debug(`Translating JSONModule ${url}`);
293295
debug(`Loading JSONModule ${url}`);
294-
constpathname=url.startsWith('file:') ? fileURLToPath(url) : null;
296+
constpathname=StringPrototypeStartsWith(url,'file:') ?
297+
fileURLToPath(url) : null;
295298
letmodulePath;
296299
letmodule;
297300
if(pathname){
@@ -365,8 +368,11 @@ translators.set('wasm', async function(url){
365368
}
366369

367370
constimports=
368-
WebAssembly.Module.imports(compiled).map(({ module })=>module);
369-
constexports=WebAssembly.Module.exports(compiled).map(({ name })=>name);
371+
ArrayPrototypeMap(WebAssembly.Module.imports(compiled),
372+
({ module })=>module);
373+
constexports=
374+
ArrayPrototypeMap(WebAssembly.Module.exports(compiled),
375+
({ name })=>name);
370376

371377
returncreateDynamicModule(imports,exports,url,(reflect)=>{
372378
const{ exports }=newWebAssembly.Instance(compiled,reflect.imports);

‎test/parallel/test-bootstrap-modules.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const expectedModules = new Set([
4949
'NativeModule internal/fixed_queue',
5050
'NativeModule internal/fs/dir',
5151
'NativeModule internal/fs/utils',
52+
'NativeModule internal/fs/promises',
53+
'NativeModule internal/fs/rimraf',
5254
'NativeModule internal/idna',
5355
'NativeModule internal/linkedlist',
5456
'NativeModule internal/modules/run_main',

0 commit comments

Comments
(0)