From 83f3999a2e87248412ff3e5ad0ebec51b3d0b045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xuan=20Huang=20=28=E9=BB=84=E7=8E=84=29?= Date: Wed, 5 Oct 2022 13:23:37 -0400 Subject: [PATCH] Support var declaration hoisting Var declarations were treated identical as other declarations which cause code relying on them getting hoisted now triggers runtime exception on TDZ. This diff fixed that by generating `var` for `var` so they can be hoisted as usual. commit-id:00ab02f6 --- compiler/forget/src/BackEnd/JS.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/compiler/forget/src/BackEnd/JS.ts b/compiler/forget/src/BackEnd/JS.ts index 4baebb3d10..d8c800077e 100644 --- a/compiler/forget/src/BackEnd/JS.ts +++ b/compiler/forget/src/BackEnd/JS.ts @@ -247,14 +247,26 @@ export class Func { * let output1, output2; */ emitReactiveBlockOutputDecls(block: LIR.ReactiveBlock) { - if (block.outputDecls.size > 0) { + const vars: t.VariableDeclarator[] = []; + const lets: t.VariableDeclarator[] = []; + + for (const decl of block.outputDecls) { + if (decl.binding.kind === "var") { + vars.push(t.variableDeclarator(decl.binding.identifier)); + } else { + lets.push(t.variableDeclarator(decl.binding.identifier)); + } + } + + if (vars.length > 0) { this.code.push( - t.variableDeclaration( - /*kind*/ "let", - /*declarators*/ [...block.outputDecls.values()].map((output) => - t.variableDeclarator(/*id*/ output.binding.identifier) - ) - ) + t.variableDeclaration(/*kind*/ "var", /*declarators*/ vars) + ); + } + + if (lets.length > 0) { + this.code.push( + t.variableDeclaration(/*kind*/ "let", /*declarators*/ lets) ); } }