Skip to content

Commit

Permalink
lib: enforce use of Promise from primordials
Browse files Browse the repository at this point in the history
PR-URL: #30936
Refs: #30697
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
targos authored and MylesBorins committed Dec 17, 2019
1 parent 2b0e2c2 commit 19f05ca
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 8 deletions.
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
28 changes: 28 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 @@ -135,5 +151,17 @@ primordials.SafePromise = makeSafe(
copyPrototype(original.prototype, primordials, `${name}Prototype`);
});

// Create copies of intrinsic objects that require a valid `this` to call
// static methods.
// Refs: https://www.ecma-international.org/ecma-262/#sec-promise.all
[
'Promise',
].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 @@ -53,6 +53,8 @@ const {
ObjectGetPrototypeOf,
ObjectKeys,
ObjectSetPrototypeOf,
Promise,
PromiseRace,
Symbol,
} = primordials;

Expand Down Expand Up @@ -460,7 +462,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

0 comments on commit 19f05ca

Please sign in to comment.