mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
repl: fix promise regression
Signed-off-by: avivkeller <me@aviv.sh> PR-URL: https://github.com/nodejs/node/pull/67721 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
345e2b2fd7
commit
b663e55882
5 files changed
+44
-19
No files matched your search
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 { <pending> }'],
|
||||
['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());
|
||||
@@ -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`);
|
||||
|
||||
Reference in new issue
Block a user