diff --git a/lib/internal/repl/eval.js b/lib/internal/repl/eval.js index f988dcc13230..6b88b5a9840d 100644 --- a/lib/internal/repl/eval.js +++ b/lib/internal/repl/eval.js @@ -150,8 +150,6 @@ function createReplEval(repl) { silent: false, returnByValue: false, generatePreview: false, - // Await in a second inspector command after retaining the promise's - // objectId. See ReplInspectorChannel.evaluate(). awaitPromise: false, replMode: true, throwOnSideEffect: false, diff --git a/lib/internal/repl/inspector.js b/lib/internal/repl/inspector.js index 371255eac28d..cf3b25b145e0 100644 --- a/lib/internal/repl/inspector.js +++ b/lib/internal/repl/inspector.js @@ -172,21 +172,8 @@ class ReplInspectorChannel { } // Performs a `Runtime.evaluate`. - async evaluate(params, breakOnSigint) { - const response = await this.postInterruptible( - 'Runtime.evaluate', params, breakOnSigint); - if (response.exceptionDetails || response.result?.subtype !== 'promise') { - return response; - } - - // Hold the promise open until it resolves or rejects, - // so we can return the final value. - return this.postInterruptible('Runtime.awaitPromise', { - __proto__: null, - promiseObjectId: response.result.objectId, - returnByValue: params.returnByValue, - generatePreview: params.generatePreview, - }, breakOnSigint); + evaluate(params, breakOnSigint) { + return this.postInterruptible('Runtime.evaluate', params, breakOnSigint); } // Turns a remote object returned by the inspector into the real, live value diff --git a/lib/internal/repl/utils.js b/lib/internal/repl/utils.js index 750867d6ba4f..7dcbb0dbe56e 100644 --- a/lib/internal/repl/utils.js +++ b/lib/internal/repl/utils.js @@ -132,7 +132,12 @@ function isRecoverableError(e, code) { // Try to parse the code with acorn. If the parse fails, ignore the acorn // error and return the recoverable status. try { - RecoverableParser.parse(code, { ecmaVersion: 'latest' }); + RecoverableParser.parse(code, { + ecmaVersion: 'latest', + // Top-level await is supported, so `await` must be parsed as a keyword + // for incomplete input such as `await new Promise((resolve) => {`. + allowAwaitOutsideFunction: true, + }); // Odd case: the underlying JS engine (V8, Chakra) rejected this input // but Acorn detected no issue. Presume that additional text won't diff --git a/test/parallel/test-repl-promise-completion-value.js b/test/parallel/test-repl-promise-completion-value.js new file mode 100644 index 000000000000..fe528db9ae73 --- /dev/null +++ b/test/parallel/test-repl-promise-completion-value.js @@ -0,0 +1,35 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { startNewREPLServer } = require('../common/repl'); + +const testCases = [ + ['await 2', '2'], + ['Promise.resolve(2)', 'Promise { 2 }'], + ['await 1; Promise.resolve(2);', 'Promise { 2 }'], + ['new Promise(() => {})', 'Promise { }'], + ['await Promise.resolve(123), Promise.resolve(456)', 'Promise { 456 }'], + ['await Promise.resolve(123), await Promise.resolve(456)', '456'], + ['await (Promise.resolve(123), Promise.resolve(456))', '456'], + ['(async () => 7)()', 'Promise { 7 }'], + ['await (async () => 7)()', '7'], + ['const p = Promise.resolve(8)', 'undefined'], + ['p', 'Promise { 8 }'], + ['await p', '8'], +]; + +(async () => { + const { output, replServer, run } = startNewREPLServer({ + terminal: false, + useColors: false, + }); + + for (const [input, expected] of testCases) { + output.accumulator = ''; + await run(`${input}\n`); + assert.strictEqual(output.accumulator, `${expected}\n`, input); + } + + replServer.close(); +})().then(common.mustCall()); diff --git a/test/parallel/test-repl-promise-gc.mjs b/test/parallel/test-repl-promise-gc.mjs index 94fa28bbddb4..efa1c323118c 100644 --- a/test/parallel/test-repl-promise-gc.mjs +++ b/test/parallel/test-repl-promise-gc.mjs @@ -11,7 +11,7 @@ const { output, replServer, run } = startNewREPLServer({ // The promise is not retained by user code. Force a collection while the REPL // is awaiting it to verify that the inspector keeps it alive until settlement. -await run(`new Promise((resolve) => setImmediate(() => { +await run(`await new Promise((resolve) => setImmediate(() => { globalThis.gc(); resolve(42); }))\n`);