From 78c40e3f6c63fd1e891481e5aa6b7bd0b06ba09b Mon Sep 17 00:00:00 2001 From: Dmitry Vasilev Date: Sat, 31 Dec 2022 20:40:59 +0800 Subject: [PATCH] fix bug --- src/eval.js | 21 ++++++++++----------- test/test.js | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/eval.js b/src/eval.js index e1a962b..ee69921 100644 --- a/src/eval.js +++ b/src/eval.js @@ -72,8 +72,8 @@ const codegen_function_expr = (node, cxt) => { const call = (node.is_async ? 'async ' : '') + `(${args}) => ` + ( (node.body.type == 'do') - ? '{' + do_codegen(node.body) + '}' - : '(' + do_codegen(node.body) + ')' + ? '{ let __obj, __fn; ' + do_codegen(node.body) + '}' + : '{ let __obj, __fn; return ' + do_codegen(node.body) + '}' ) const argscount = node @@ -103,18 +103,17 @@ const codegen_function_call = (node, cxt) => { let call if(node.fn.type == 'member_access') { - // Wrap to IIFE to create scope to calculate obj. - // We cant do `codegen(obj)[prop].bind(codegen(obj))` because codegen(obj) - // can be expr we dont want to eval twice - const op = node.fn.is_optional_chaining ? '?.' : '' // TODO gensym __obj, __fn - return `((() => { - const __obj = ${do_codegen(node.fn.object)}; - const __fn = __obj${op}[${do_codegen(node.fn.property)}] - return trace_call(__fn, __obj, ${args}) - })())` + // We cant do `codegen(obj)[prop].bind(codegen(obj))` because codegen(obj) + // can be expr we dont want to eval twice. Use comma operator to perform + // assignments in expression context + return `( + __obj = ${do_codegen(node.fn.object)}, + __fn = __obj${op}[${do_codegen(node.fn.property)}], + trace_call(__fn, __obj, ${args}) + )` } else { return `trace_call(${do_codegen(node.fn)}, null, ${args})` } diff --git a/test/test.js b/test/test.js index a5e6c35..314978f 100644 --- a/test/test.js +++ b/test/test.js @@ -2912,5 +2912,22 @@ const y = x()` const {state: after_move} = await COMMANDS.move_cursor(i, code.indexOf('1')) assert_equal(after_move.active_calltree_node.fn.name, 'f') }), + + test('async/await await argument bug', async () => { + await assert_code_evals_to_async( + ` + Object.assign({}, await {foo: 1}) + `, + {foo: 1} + ) + }), + + /* + test('async/await move_cursor bug', async () => { + const i = await test_initial_state_async(` + await new Promise(resolve => globalThis.setTimeout(resolve, 1)) + `) + }), + */ ]