From 9817e405eefd1e119cde826fdb80d0cd57442298 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sat, 19 May 2018 00:25:07 +0200 Subject: [PATCH] lib,src: replace all C++ promises with JS promises C++ promises can not be properly optimized by V8. They also behave a tiny bit different than "regular" promises. PR-URL: https://github.com/nodejs/node/pull/20830 Reviewed-By: Anna Henningsen Reviewed-By: Gus Caplan Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Benedikt Meurer Reviewed-By: Benjamin Gruenbaum Backport-PR-URL: https://github.com/nodejs/node/pull/21879 Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum --- lib/child_process.js | 23 +++++------ lib/fs.js | 5 +-- lib/internal/http2/core.js | 9 ++-- lib/internal/util.js | 20 ++++----- lib/timers.js | 17 +++----- src/node_util.cc | 35 ---------------- .../test-promise-internal-creation.js | 41 ------------------- 7 files changed, 27 insertions(+), 123 deletions(-) delete mode 100644 test/parallel/test-promise-internal-creation.js diff --git a/lib/child_process.js b/lib/child_process.js index b2835b0a8f58be..d816bbf5f091a5 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -26,8 +26,6 @@ const { deprecate, convertToValidSignal, getSystemErrorName } = require('internal/util'); const { isUint8Array } = require('internal/util/types'); -const { createPromise, - promiseResolve, promiseReject } = process.binding('util'); const debug = util.debuglog('child_process'); const { Buffer } = require('buffer'); const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap'); @@ -152,18 +150,17 @@ exports.exec = function exec(command /* , options, callback */) { const customPromiseExecFunction = (orig) => { return (...args) => { - const promise = createPromise(); - - orig(...args, (err, stdout, stderr) => { - if (err !== null) { - err.stdout = stdout; - err.stderr = stderr; - promiseReject(promise, err); - } else { - promiseResolve(promise, { stdout, stderr }); - } + return new Promise((resolve, reject) => { + orig(...args, (err, stdout, stderr) => { + if (err !== null) { + err.stdout = stdout; + err.stderr = stderr; + reject(err); + } else { + resolve({ stdout, stderr }); + } + }); }); - return promise; }; }; diff --git a/lib/fs.js b/lib/fs.js index 0cfd175d7b3d99..e0781161dfccbb 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -207,10 +207,7 @@ function exists(path, callback) { Object.defineProperty(exists, internalUtil.promisify.custom, { value: (path) => { - const { createPromise, promiseResolve } = process.binding('util'); - const promise = createPromise(); - fs.exists(path, (exists) => promiseResolve(promise, exists)); - return promise; + return new Promise((resolve) => fs.exists(path, resolve)); } }); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index db251caf5f73ef..be4545ecc3d70b 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -113,7 +113,6 @@ const { isArrayBufferView } = require('internal/util/types'); const { FileHandle } = process.binding('fs'); const binding = process.binding('http2'); const { ShutdownWrap } = process.binding('stream_wrap'); -const { createPromise, promiseResolve } = process.binding('util'); const { UV_EOF } = process.binding('uv'); const { StreamPipe } = internalBinding('stream_pipe'); @@ -2747,11 +2746,9 @@ function connect(authority, options, listener) { // Support util.promisify Object.defineProperty(connect, promisify.custom, { value: (authority, options) => { - const promise = createPromise(); - const server = connect(authority, - options, - () => promiseResolve(promise, server)); - return promise; + return new Promise((resolve) => { + const server = connect(authority, options, () => resolve(server)); + }); } }); diff --git a/lib/internal/util.js b/lib/internal/util.js index bc513e7fedf79e..49dad0c6c35692 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -8,10 +8,7 @@ const { const { signals } = process.binding('constants').os; const { - createPromise, getHiddenValue, - promiseResolve, - promiseReject, setHiddenValue, arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex, decorated_private_symbol: kDecoratedPrivateSymbolIndex @@ -276,24 +273,21 @@ function promisify(original) { const argumentNames = original[kCustomPromisifyArgsSymbol]; function fn(...args) { - const promise = createPromise(); - try { + return new Promise((resolve, reject) => { original.call(this, ...args, (err, ...values) => { if (err) { - promiseReject(promise, err); - } else if (argumentNames !== undefined && values.length > 1) { + return reject(err); + } + if (argumentNames !== undefined && values.length > 1) { const obj = {}; for (var i = 0; i < argumentNames.length; i++) obj[argumentNames[i]] = values[i]; - promiseResolve(promise, obj); + resolve(obj); } else { - promiseResolve(promise, values[0]); + resolve(values[0]); } }); - } catch (err) { - promiseReject(promise, err); - } - return promise; + }); } Object.setPrototypeOf(fn, Object.getPrototypeOf(original)); diff --git a/lib/timers.js b/lib/timers.js index ff85a9f4b1d1e0..2459242a79f6cd 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -34,7 +34,6 @@ const { validateTimerDuration } = require('internal/timers'); const internalUtil = require('internal/util'); -const { createPromise, promiseResolve } = process.binding('util'); const assert = require('assert'); const util = require('util'); const { ERR_INVALID_CALLBACK } = require('internal/errors').codes; @@ -407,11 +406,9 @@ function setTimeout(callback, after, arg1, arg2, arg3) { } setTimeout[internalUtil.promisify.custom] = function(after, value) { - const promise = createPromise(); - const timeout = new Timeout(promise, after, [value], false, false); - active(timeout); - - return promise; + return new Promise((resolve) => { + active(new Timeout(resolve, after, [value], false, false)); + }); }; exports.setTimeout = setTimeout; @@ -420,7 +417,7 @@ exports.setTimeout = setTimeout; function ontimeout(timer, start) { const args = timer._timerArgs; if (typeof timer._onTimeout !== 'function') - return promiseResolve(timer._onTimeout, args[0]); + return Promise.resolve(timer._onTimeout, args[0]); if (start === undefined && timer._repeat) start = TimerWrap.now(); if (!args) @@ -691,7 +688,7 @@ function tryOnImmediate(immediate, oldTail, count, refCount) { function runCallback(timer) { const argv = timer._argv; if (typeof timer._onImmediate !== 'function') - return promiseResolve(timer._onImmediate, argv[0]); + return Promise.resolve(timer._onImmediate, argv[0]); if (!argv) return timer._onImmediate(); Reflect.apply(timer._onImmediate, timer, argv); @@ -766,9 +763,7 @@ function setImmediate(callback, arg1, arg2, arg3) { } setImmediate[internalUtil.promisify.custom] = function(value) { - const promise = createPromise(); - new Immediate(promise, [value]); - return promise; + return new Promise((resolve) => new Immediate(resolve, [value])); }; exports.setImmediate = setImmediate; diff --git a/src/node_util.cc b/src/node_util.cc index ef7dc8a818bf11..9f31786b32557f 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -9,7 +9,6 @@ using v8::Context; using v8::FunctionCallbackInfo; using v8::Integer; using v8::Local; -using v8::Maybe; using v8::Object; using v8::Private; using v8::Promise; @@ -127,36 +126,6 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(ret); } - -void CreatePromise(const FunctionCallbackInfo& args) { - Local context = args.GetIsolate()->GetCurrentContext(); - auto maybe_resolver = Promise::Resolver::New(context); - if (!maybe_resolver.IsEmpty()) - args.GetReturnValue().Set(maybe_resolver.ToLocalChecked()); -} - - -void PromiseResolve(const FunctionCallbackInfo& args) { - Local context = args.GetIsolate()->GetCurrentContext(); - Local promise = args[0]; - CHECK(promise->IsPromise()); - if (promise.As()->State() != Promise::kPending) return; - Local resolver = promise.As(); // sic - Maybe ret = resolver->Resolve(context, args[1]); - args.GetReturnValue().Set(ret.FromMaybe(false)); -} - - -void PromiseReject(const FunctionCallbackInfo& args) { - Local context = args.GetIsolate()->GetCurrentContext(); - Local promise = args[0]; - CHECK(promise->IsPromise()); - if (promise.As()->State() != Promise::kPending) return; - Local resolver = promise.As(); // sic - Maybe ret = resolver->Reject(context, args[1]); - args.GetReturnValue().Set(ret.FromMaybe(false)); -} - void SafeGetenv(const FunctionCallbackInfo& args) { CHECK(args[0]->IsString()); Utf8Value strenvtag(args.GetIsolate(), args[0]); @@ -211,10 +180,6 @@ void Initialize(Local target, env->SetMethodNoSideEffect(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint); - env->SetMethodNoSideEffect(target, "createPromise", CreatePromise); - env->SetMethod(target, "promiseResolve", PromiseResolve); - env->SetMethod(target, "promiseReject", PromiseReject); - env->SetMethod(target, "safeGetenv", SafeGetenv); } diff --git a/test/parallel/test-promise-internal-creation.js b/test/parallel/test-promise-internal-creation.js deleted file mode 100644 index 7142c872d9452e..00000000000000 --- a/test/parallel/test-promise-internal-creation.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const { - createPromise, promiseResolve, promiseReject -} = process.binding('util'); -const { inspect } = require('util'); - -common.crashOnUnhandledRejection(); - -{ - const promise = createPromise(); - assert.strictEqual(inspect(promise), 'Promise { }'); - promiseResolve(promise, 42); - assert.strictEqual(inspect(promise), 'Promise { 42 }'); - promise.then(common.mustCall((value) => { - assert.strictEqual(value, 42); - })); -} - -{ - const promise = createPromise(); - const error = new Error('foobar'); - promiseReject(promise, error); - assert(inspect(promise).includes(' Error: foobar')); - promise.catch(common.mustCall((value) => { - assert.strictEqual(value, error); - })); -} - -{ - const promise = createPromise(); - const error = new Error('foobar'); - promiseReject(promise, error); - assert(inspect(promise).includes(' Error: foobar')); - promiseResolve(promise, 42); - assert(inspect(promise).includes(' Error: foobar')); - promise.catch(common.mustCall((value) => { - assert.strictEqual(value, error); - })); -}