11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { processTopLevelAwait } = require('internal/repl/await');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Flags: --expose-internals
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// This test was created based on
101cb0ef41Sopenharmony_ci// https://cs.chromium.org/chromium/src/third_party/WebKit/LayoutTests/http/tests/inspector-unit/preprocess-top-level-awaits.js?rcl=358caaba5e763e71c4abb9ada2d9cd8b1188cac9
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst surrogate = (
131cb0ef41Sopenharmony_ci  '"\u{1F601}\u{1f468}\u200d\u{1f469}\u200d\u{1f467}\u200d\u{1f466}"'
141cb0ef41Sopenharmony_ci);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst testCases = [
171cb0ef41Sopenharmony_ci  [ '0',
181cb0ef41Sopenharmony_ci    null ],
191cb0ef41Sopenharmony_ci  [ 'await 0',
201cb0ef41Sopenharmony_ci    '(async () => { return { value: (await 0) } })()' ],
211cb0ef41Sopenharmony_ci  [ `await ${surrogate}`,
221cb0ef41Sopenharmony_ci    `(async () => { return { value: (await ${surrogate}) } })()` ],
231cb0ef41Sopenharmony_ci  [ 'await 0;',
241cb0ef41Sopenharmony_ci    '(async () => { return { value: (await 0) }; })()' ],
251cb0ef41Sopenharmony_ci  [ 'await 0;;;',
261cb0ef41Sopenharmony_ci    '(async () => { return { value: (await 0) };;; })()' ],
271cb0ef41Sopenharmony_ci  [ `await ${surrogate};`,
281cb0ef41Sopenharmony_ci    `(async () => { return { value: (await ${surrogate}) }; })()` ],
291cb0ef41Sopenharmony_ci  [ `await ${surrogate};`,
301cb0ef41Sopenharmony_ci    `(async () => { return { value: (await ${surrogate}) }; })()` ],
311cb0ef41Sopenharmony_ci  [ '(await 0)',
321cb0ef41Sopenharmony_ci    '(async () => { return ({ value: (await 0) }) })()' ],
331cb0ef41Sopenharmony_ci  [ `(await ${surrogate})`,
341cb0ef41Sopenharmony_ci    `(async () => { return ({ value: (await ${surrogate}) }) })()` ],
351cb0ef41Sopenharmony_ci  [ '(await 0);',
361cb0ef41Sopenharmony_ci    '(async () => { return ({ value: (await 0) }); })()' ],
371cb0ef41Sopenharmony_ci  [ `(await ${surrogate});`,
381cb0ef41Sopenharmony_ci    `(async () => { return ({ value: (await ${surrogate}) }); })()` ],
391cb0ef41Sopenharmony_ci  [ 'async function foo() { await 0; }',
401cb0ef41Sopenharmony_ci    null ],
411cb0ef41Sopenharmony_ci  [ 'async () => await 0',
421cb0ef41Sopenharmony_ci    null ],
431cb0ef41Sopenharmony_ci  [ 'class A { async method() { await 0 } }',
441cb0ef41Sopenharmony_ci    null ],
451cb0ef41Sopenharmony_ci  [ 'await 0; return 0;',
461cb0ef41Sopenharmony_ci    null ],
471cb0ef41Sopenharmony_ci  [ `await ${surrogate}; await ${surrogate};`,
481cb0ef41Sopenharmony_ci    `(async () => { await ${surrogate}; return { value: (await ${surrogate}) }; })()` ],
491cb0ef41Sopenharmony_ci  [ 'var a = await 1',
501cb0ef41Sopenharmony_ci    'var a; (async () => { void (a = await 1) })()' ],
511cb0ef41Sopenharmony_ci  [ `var a = await ${surrogate}`,
521cb0ef41Sopenharmony_ci    `var a; (async () => { void (a = await ${surrogate}) })()` ],
531cb0ef41Sopenharmony_ci  [ 'let a = await 1',
541cb0ef41Sopenharmony_ci    'let a; (async () => { void (a = await 1) })()' ],
551cb0ef41Sopenharmony_ci  [ 'const a = await 1',
561cb0ef41Sopenharmony_ci    'let a; (async () => { void (a = await 1) })()' ],
571cb0ef41Sopenharmony_ci  [ 'for (var i = 0; i < 1; ++i) { await i }',
581cb0ef41Sopenharmony_ci    'var i; (async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
591cb0ef41Sopenharmony_ci  [ 'for (let i = 0; i < 1; ++i) { await i }',
601cb0ef41Sopenharmony_ci    '(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ],
611cb0ef41Sopenharmony_ci  [ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}',
621cb0ef41Sopenharmony_ci    'var a, b, d; (async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
631cb0ef41Sopenharmony_ci        '({c:{d}} = {c:{d: await 1}})) })()' ],
641cb0ef41Sopenharmony_ci  [ 'let [a, b, c] = await ([1, 2, 3])',
651cb0ef41Sopenharmony_ci    'let a, b, c; (async () => { void ([a, b, c] = await ([1, 2, 3])) })()'],
661cb0ef41Sopenharmony_ci  [ 'let {a,b,c} = await ({a: 1, b: 2, c: 3})',
671cb0ef41Sopenharmony_ci    'let a, b, c; (async () => { void ({a,b,c} = ' +
681cb0ef41Sopenharmony_ci        'await ({a: 1, b: 2, c: 3})) })()'],
691cb0ef41Sopenharmony_ci  [ 'let {a: [b]} = {a: [await 1]}, [{d}] = [{d: 3}]',
701cb0ef41Sopenharmony_ci    'let b, d; (async () => { void ( ({a: [b]} = {a: [await 1]}),' +
711cb0ef41Sopenharmony_ci        ' ([{d}] = [{d: 3}])) })()'],
721cb0ef41Sopenharmony_ci  /* eslint-disable no-template-curly-in-string */
731cb0ef41Sopenharmony_ci  [ 'console.log(`${(await { a: 1 }).a}`)',
741cb0ef41Sopenharmony_ci    '(async () => { return { value: (console.log(`${(await { a: 1 }).a}`)) } })()' ],
751cb0ef41Sopenharmony_ci  /* eslint-enable no-template-curly-in-string */
761cb0ef41Sopenharmony_ci  [ 'await 0; function foo() {}',
771cb0ef41Sopenharmony_ci    'var foo; (async () => { await 0; this.foo = foo; function foo() {} })()' ],
781cb0ef41Sopenharmony_ci  [ 'await 0; class Foo {}',
791cb0ef41Sopenharmony_ci    'let Foo; (async () => { await 0; Foo=class Foo {} })()' ],
801cb0ef41Sopenharmony_ci  [ 'if (await true) { function foo() {} }',
811cb0ef41Sopenharmony_ci    'var foo; (async () => { ' +
821cb0ef41Sopenharmony_ci      'if (await true) { this.foo = foo; function foo() {} } })()' ],
831cb0ef41Sopenharmony_ci  [ 'if (await true) { class Foo{} }',
841cb0ef41Sopenharmony_ci    '(async () => { if (await true) { class Foo{} } })()' ],
851cb0ef41Sopenharmony_ci  [ 'if (await true) { var a = 1; }',
861cb0ef41Sopenharmony_ci    'var a; (async () => { if (await true) { void (a = 1); } })()' ],
871cb0ef41Sopenharmony_ci  [ 'if (await true) { let a = 1; }',
881cb0ef41Sopenharmony_ci    '(async () => { if (await true) { let a = 1; } })()' ],
891cb0ef41Sopenharmony_ci  [ 'var a = await 1; let b = 2; const c = 3;',
901cb0ef41Sopenharmony_ci    'var a; let b; let c; (async () => { void (a = await 1); void (b = 2);' +
911cb0ef41Sopenharmony_ci        ' void (c = 3); })()' ],
921cb0ef41Sopenharmony_ci  [ 'let o = await 1, p',
931cb0ef41Sopenharmony_ci    'let o, p; (async () => { void ( (o = await 1), (p=undefined)) })()' ],
941cb0ef41Sopenharmony_ci  [ 'await (async () => { let p = await 1; return p; })()',
951cb0ef41Sopenharmony_ci    '(async () => { return { value: (await (async () => ' +
961cb0ef41Sopenharmony_ci      '{ let p = await 1; return p; })()) } })()' ],
971cb0ef41Sopenharmony_ci  [ '{ let p = await 1; }',
981cb0ef41Sopenharmony_ci    '(async () => { { let p = await 1; } })()' ],
991cb0ef41Sopenharmony_ci  [ 'var p = await 1',
1001cb0ef41Sopenharmony_ci    'var p; (async () => { void (p = await 1) })()' ],
1011cb0ef41Sopenharmony_ci  [ 'await (async () => { var p = await 1; return p; })()',
1021cb0ef41Sopenharmony_ci    '(async () => { return { value: (await (async () => ' +
1031cb0ef41Sopenharmony_ci      '{ var p = await 1; return p; })()) } })()' ],
1041cb0ef41Sopenharmony_ci  [ '{ var p = await 1; }',
1051cb0ef41Sopenharmony_ci    'var p; (async () => { { void (p = await 1); } })()' ],
1061cb0ef41Sopenharmony_ci  [ 'for await (var i of asyncIterable) { i; }',
1071cb0ef41Sopenharmony_ci    'var i; (async () => { for await (i of asyncIterable) { i; } })()'],
1081cb0ef41Sopenharmony_ci  [ 'for await (var [i] of asyncIterable) { i; }',
1091cb0ef41Sopenharmony_ci    'var i; (async () => { for await ([i] of asyncIterable) { i; } })()'],
1101cb0ef41Sopenharmony_ci  [ 'for await (var {i} of asyncIterable) { i; }',
1111cb0ef41Sopenharmony_ci    'var i; (async () => { for await ({i} of asyncIterable) { i; } })()'],
1121cb0ef41Sopenharmony_ci  [ 'for await (var [{i}, [j]] of asyncIterable) { i; }',
1131cb0ef41Sopenharmony_ci    'var i, j; (async () => { for await ([{i}, [j]] of asyncIterable)' +
1141cb0ef41Sopenharmony_ci      ' { i; } })()'],
1151cb0ef41Sopenharmony_ci  [ 'for await (let i of asyncIterable) { i; }',
1161cb0ef41Sopenharmony_ci    '(async () => { for await (let i of asyncIterable) { i; } })()'],
1171cb0ef41Sopenharmony_ci  [ 'for await (const i of asyncIterable) { i; }',
1181cb0ef41Sopenharmony_ci    '(async () => { for await (const i of asyncIterable) { i; } })()'],
1191cb0ef41Sopenharmony_ci  [ 'for (var i of [1,2,3]) { await 1; }',
1201cb0ef41Sopenharmony_ci    'var i; (async () => { for (i of [1,2,3]) { await 1; } })()'],
1211cb0ef41Sopenharmony_ci  [ 'for (var [i] of [[1], [2]]) { await 1; }',
1221cb0ef41Sopenharmony_ci    'var i; (async () => { for ([i] of [[1], [2]]) { await 1; } })()'],
1231cb0ef41Sopenharmony_ci  [ 'for (var {i} of [{i: 1}, {i: 2}]) { await 1; }',
1241cb0ef41Sopenharmony_ci    'var i; (async () => { for ({i} of [{i: 1}, {i: 2}]) { await 1; } })()'],
1251cb0ef41Sopenharmony_ci  [ 'for (var [{i}, [j]] of [[{i: 1}, [2]]]) { await 1; }',
1261cb0ef41Sopenharmony_ci    'var i, j; (async () => { for ([{i}, [j]] of [[{i: 1}, [2]]])' +
1271cb0ef41Sopenharmony_ci      ' { await 1; } })()'],
1281cb0ef41Sopenharmony_ci  [ 'for (let i of [1,2,3]) { await 1; }',
1291cb0ef41Sopenharmony_ci    '(async () => { for (let i of [1,2,3]) { await 1; } })()'],
1301cb0ef41Sopenharmony_ci  [ 'for (const i of [1,2,3]) { await 1; }',
1311cb0ef41Sopenharmony_ci    '(async () => { for (const i of [1,2,3]) { await 1; } })()'],
1321cb0ef41Sopenharmony_ci  [ 'for (var i in {x:1}) { await 1 }',
1331cb0ef41Sopenharmony_ci    'var i; (async () => { for (i in {x:1}) { await 1 } })()'],
1341cb0ef41Sopenharmony_ci  [ 'for (var [a,b] in {xy:1}) { await 1 }',
1351cb0ef41Sopenharmony_ci    'var a, b; (async () => { for ([a,b] in {xy:1}) { await 1 } })()'],
1361cb0ef41Sopenharmony_ci  [ 'for (let i in {x:1}) { await 1 }',
1371cb0ef41Sopenharmony_ci    '(async () => { for (let i in {x:1}) { await 1 } })()'],
1381cb0ef41Sopenharmony_ci  [ 'for (const i in {x:1}) { await 1 }',
1391cb0ef41Sopenharmony_ci    '(async () => { for (const i in {x:1}) { await 1 } })()'],
1401cb0ef41Sopenharmony_ci  [ 'var x = await foo(); async function foo() { return Promise.resolve(1);}',
1411cb0ef41Sopenharmony_ci    'var x; var foo; (async () => { void (x = await foo()); this.foo = foo; ' +
1421cb0ef41Sopenharmony_ci      'async function foo() { return Promise.resolve(1);} })()'],
1431cb0ef41Sopenharmony_ci  [ '(await x).y',
1441cb0ef41Sopenharmony_ci    '(async () => { return { value: ((await x).y) } })()'],
1451cb0ef41Sopenharmony_ci  [ 'await (await x).y',
1461cb0ef41Sopenharmony_ci    '(async () => { return { value: (await (await x).y) } })()'],
1471cb0ef41Sopenharmony_ci];
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_cifor (const [input, expected] of testCases) {
1501cb0ef41Sopenharmony_ci  assert.strictEqual(processTopLevelAwait(input), expected);
1511cb0ef41Sopenharmony_ci}
152