Skip to content

lib: enforce use of Promise from primordials #30936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ rules:
message: "Use `const { Number } = primordials;` instead of the global."
- name: Object
message: "Use `const { Object } = primordials;` instead of the global."
- name: Promise
message: "Use `const { Promise } = primordials;` instead of the global."
- name: Reflect
message: "Use `const { Reflect } = primordials;` instead of the global."
- name: Symbol
Expand Down
1 change: 1 addition & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
ObjectAssign,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Promise,
} = primordials;

const {
Expand Down
1 change: 1 addition & 0 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
ObjectDefineProperty,
ObjectGetPrototypeOf,
ObjectKeys,
Promise,
ReflectApply,
ReflectOwnKeys,
Symbol,
Expand Down
1 change: 1 addition & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
Promise,
} = primordials;

const { fs: constants } = internalBinding('constants');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ObjectCreate,
ObjectDefineProperty,
Promise,
} = primordials;

const {
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
// - Bring your own custom fs module is not currently supported.
// - Some basic code cleanup.
'use strict';

const {
Promise,
} = primordials;

const { Buffer } = require('buffer');
const {
chmod,
Expand Down
1 change: 1 addition & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ObjectCreate,
ObjectDefineProperty,
ObjectPrototypeHasOwnProperty,
Promise,
ReflectGetPrototypeOf,
Symbol,
} = primordials;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
ObjectSetPrototypeOf,
PromiseAll,
SafeSet,
SafePromise,
} = primordials;
Expand Down Expand Up @@ -79,7 +80,7 @@ class ModuleJob {
}
jobsInGraph.add(moduleJob);
const dependencyJobs = await moduleJob.linked;
return Promise.all(dependencyJobs.map(addJobsToDependencyGraph));
return PromiseAll(dependencyJobs.map(addJobsToDependencyGraph));
};
await addJobsToDependencyGraph(this);
try {
Expand Down
27 changes: 27 additions & 0 deletions lib/internal/per_context/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ function copyPropsRenamed(src, dest, prefix) {
}
}

function copyPropsRenamedBound(src, dest, prefix) {
for (const key of Reflect.ownKeys(src)) {
if (typeof key === 'string') {
const desc = Reflect.getOwnPropertyDescriptor(src, key);
if (typeof desc.value === 'function') {
desc.value = desc.value.bind(src);
}
Reflect.defineProperty(
dest,
`${prefix}${key[0].toUpperCase()}${key.slice(1)}`,
desc
);
}
}
}

function copyPrototype(src, dest, prefix) {
for (const key of Reflect.ownKeys(src)) {
if (typeof key === 'string') {
Expand Down Expand Up @@ -124,5 +140,16 @@ primordials.SafePromise = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

// Create copies of intrinsic objects that require a valid `this` to call
// static methods.
[
'Promise',
Copy link
Member

@joyeecheung joyeecheung Dec 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be more informative to reference https://www.ecma-international.org/ecma-262/#sec-promise.all here which has this note:

The all function requires its this value to be a constructor function that supports the parameter conventions of the Promise constructor.

(and so are other Promise "static" methods since Promise is a class, not just a namespace)

].forEach((name) => {
const original = global[name];
primordials[name] = original;
copyPropsRenamedBound(original, primordials, name);
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

Object.setPrototypeOf(primordials, null);
Object.freeze(primordials);
3 changes: 2 additions & 1 deletion lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
JSONStringify,
PromiseResolve,
} = primordials;

const path = require('path');
Expand Down Expand Up @@ -41,7 +42,7 @@ function evalModule(source, print) {
const { log, error } = require('internal/console/global');
const { decorateErrorStack } = require('internal/util');
const asyncESM = require('internal/process/esm_loader');
Promise.resolve(asyncESM.ESMLoader).then(async (loader) => {
PromiseResolve(asyncESM.ESMLoader).then(async (loader) => {
const { result } = await loader.eval(source);
if (print) {
log(result);
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/streams/async_iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const {
ObjectCreate,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Promise,
PromiseReject,
PromiseResolve,
Symbol,
} = primordials;

Expand Down Expand Up @@ -67,11 +70,11 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
// reject straight away.
const error = this[kError];
if (error !== null) {
return Promise.reject(error);
return PromiseReject(error);
}

if (this[kEnded]) {
return Promise.resolve(createIterResult(undefined, true));
return PromiseResolve(createIterResult(undefined, true));
}

if (this[kStream].destroyed) {
Expand Down Expand Up @@ -103,7 +106,7 @@ const ReadableStreamAsyncIteratorPrototype = ObjectSetPrototypeOf({
// without triggering the next() queue.
const data = this[kStream].read();
if (data !== null) {
return Promise.resolve(createIterResult(data, false));
return PromiseResolve(createIterResult(data, false));
}

promise = new Promise(this[kHandlePromise]);
Expand Down
1 change: 1 addition & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ObjectGetOwnPropertyDescriptors,
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
Promise,
ReflectConstruct,
Symbol,
SymbolFor,
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
MathMax,
ObjectCreate,
ObjectEntries,
Promise,
PromiseResolve,
Symbol,
SymbolFor,
} = primordials;
Expand Down Expand Up @@ -259,11 +261,11 @@ class Worker extends EventEmitter {
'Passing a callback to worker.terminate() is deprecated. ' +
'It returns a Promise instead.',
'DeprecationWarning', 'DEP0132');
if (this[kHandle] === null) return Promise.resolve();
if (this[kHandle] === null) return PromiseResolve();
this.once('exit', (exitCode) => callback(null, exitCode));
}

if (this[kHandle] === null) return Promise.resolve();
if (this[kHandle] === null) return PromiseResolve();

this[kHandle].stopThread();

Expand Down
4 changes: 3 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectSetPrototypeOf,
Promise,
PromiseRace,
Symbol,
} = primordials;

Expand Down Expand Up @@ -459,7 +461,7 @@ function REPLServer(prompt,
};
prioritizedSigintQueue.add(sigintListener);
});
promise = Promise.race([promise, interrupt]);
promise = PromiseRace([promise, interrupt]);
}

promise.then((result) => {
Expand Down
1 change: 1 addition & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const {
MathTrunc,
Promise,
} = primordials;

const {
Expand Down