Skip to content

Commit a8a04ef

Browse files
targosBethGriggs
authored andcommitted
lib: enforce use of Promise from primordials
PR-URL: #30936 Refs: #30697 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent fbd9ea8 commit a8a04ef

File tree

15 files changed

+59
-8
lines changed

15 files changed

+59
-8
lines changed

‎lib/.eslintrc.yaml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ rules:
2323
message: "Use `const{Number } = primordials;` instead of the global."
2424
- name: Object
2525
message: "Use `const{Object } = primordials;` instead of the global."
26+
- name: Promise
27+
message: "Use `const{Promise } = primordials;` instead of the global."
2628
- name: Reflect
2729
message: "Use `const{Reflect } = primordials;` instead of the global."
2830
- name: Symbol

‎lib/child_process.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const{
2727
ObjectAssign,
2828
ObjectDefineProperty,
2929
ObjectPrototypeHasOwnProperty,
30+
Promise,
3031
}=primordials;
3132

3233
const{

‎lib/events.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const{
2929
ObjectDefineProperty,
3030
ObjectGetPrototypeOf,
3131
ObjectKeys,
32+
Promise,
3233
ReflectApply,
3334
ReflectOwnKeys,
3435
}=primordials;

‎lib/fs.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const{
3030
ObjectCreate,
3131
ObjectDefineProperties,
3232
ObjectDefineProperty,
33+
Promise,
3334
}=primordials;
3435

3536
const{fs: constants}=internalBinding('constants');

‎lib/internal/dns/promises.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const{
44
ObjectCreate,
55
ObjectDefineProperty,
6+
Promise,
67
}=primordials;
78

89
const{

‎lib/internal/fs/rimraf.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// - Bring your own custom fs module is not currently supported.
66
// - Some basic code cleanup.
77
'use strict';
8+
9+
const{
10+
Promise,
11+
}=primordials;
12+
813
const{
914
chmod,
1015
chmodSync,

‎lib/internal/http2/core.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const{
1010
ObjectCreate,
1111
ObjectDefineProperty,
1212
ObjectPrototypeHasOwnProperty,
13+
Promise,
1314
ReflectGetPrototypeOf,
1415
Symbol,
1516
}=primordials;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const{
44
ObjectSetPrototypeOf,
5+
PromiseAll,
56
SafeSet,
67
SafePromise,
78
}=primordials;
@@ -79,7 +80,7 @@ class ModuleJob{
7980
}
8081
jobsInGraph.add(moduleJob);
8182
constdependencyJobs=awaitmoduleJob.linked;
82-
returnPromise.all(dependencyJobs.map(addJobsToDependencyGraph));
83+
returnPromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
8384
};
8485
awaitaddJobsToDependencyGraph(this);
8586
try{

‎lib/internal/per_context/primordials.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ function copyPropsRenamed(src, dest, prefix){
4747
}
4848
}
4949

50+
functioncopyPropsRenamedBound(src,dest,prefix){
51+
for(constkeyofReflect.ownKeys(src)){
52+
if(typeofkey==='string'){
53+
constdesc=Reflect.getOwnPropertyDescriptor(src,key);
54+
if(typeofdesc.value==='function'){
55+
desc.value=desc.value.bind(src);
56+
}
57+
Reflect.defineProperty(
58+
dest,
59+
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
60+
desc
61+
);
62+
}
63+
}
64+
}
65+
5066
functioncopyPrototype(src,dest,prefix){
5167
for(constkeyofReflect.ownKeys(src)){
5268
if(typeofkey==='string'){
@@ -135,5 +151,17 @@ primordials.SafePromise = makeSafe(
135151
copyPrototype(original.prototype,primordials,`${name}Prototype`);
136152
});
137153

154+
// Create copies of intrinsic objects that require a valid `this` to call
155+
// static methods.
156+
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
157+
[
158+
'Promise',
159+
].forEach((name)=>{
160+
constoriginal=global[name];
161+
primordials[name]=original;
162+
copyPropsRenamedBound(original,primordials,name);
163+
copyPrototype(original.prototype,primordials,`${name}Prototype`);
164+
});
165+
138166
Object.setPrototypeOf(primordials,null);
139167
Object.freeze(primordials);

‎lib/internal/process/execution.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const{
44
JSONStringify,
5+
PromiseResolve,
56
}=primordials;
67

78
constpath=require('path');
@@ -41,7 +42,7 @@ function evalModule(source, print){
4142
const{ log, error }=require('internal/console/global');
4243
const{ decorateErrorStack }=require('internal/util');
4344
constasyncESM=require('internal/process/esm_loader');
44-
Promise.resolve(asyncESM.ESMLoader).then(async(loader)=>{
45+
PromiseResolve(asyncESM.ESMLoader).then(async(loader)=>{
4546
const{ result }=awaitloader.eval(source);
4647
if(print){
4748
log(result);

0 commit comments

Comments
(0)