From 3f801169760a1817876648e6d22288ebd8ce97b8 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 11 May 2021 20:11:01 +0200 Subject: compiler: fix local for-loop initializer variable declarations In a loop statement like `for (let x = 1, y = 2; ...)` the initialization statement was incorrectly interpreted as `let x = 1; y = 2` instead of the correct `let ..., y = 2`, triggering reference error exceptions in strict mode. Solve the issue by continue parsing the rest of the comma expression seqence as declaration list expression when the initializer is compiled in local mode. Signed-off-by: Jo-Philipp Wich --- compiler.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'compiler.c') diff --git a/compiler.c b/compiler.c index b691b5e..889a8dd 100644 --- a/compiler.c +++ b/compiler.c @@ -1869,7 +1869,7 @@ uc_compiler_declare_internal(uc_compiler *compiler, size_t srcpos, const char *n } static void -uc_compiler_compile_local(uc_compiler *compiler) +uc_compiler_compile_declexpr(uc_compiler *compiler) { ssize_t slot; @@ -1899,7 +1899,12 @@ uc_compiler_compile_local(uc_compiler *compiler) } } while (uc_compiler_parse_match(compiler, TK_COMMA)); +} +static void +uc_compiler_compile_local(uc_compiler *compiler) +{ + uc_compiler_compile_declexpr(compiler); uc_compiler_parse_consume(compiler, TK_SCOL); } @@ -2218,8 +2223,15 @@ uc_compiler_compile_for_count(uc_compiler *compiler, bool local, uc_token *var) /* If followed by a comma, continue parsing expression */ if (uc_compiler_parse_match(compiler, TK_COMMA)) { - uc_compiler_compile_expression(compiler); - uc_compiler_emit_insn(compiler, 0, I_POP); + /* Is a continuation of a declaration list... */ + if (local) { + uc_compiler_compile_declexpr(compiler); + } + /* ... otherwise an unrelated expression */ + else { + uc_compiler_compile_expression(compiler); + uc_compiler_emit_insn(compiler, 0, I_POP); + } } } /* ... otherwise try parsing an entire expression (which might be absent) */ -- cgit v1.2.3