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
Xuan Huang (黄玄) committed
Oct 5, 2022 at 13:23 UTC
83f3999a2e87248412ff3e5ad0ebec51b3d0b045
1 file changed
+19
-7
compiler/forget/src/BackEnd/JS.ts
+19
-7
@@ -247,14 +247,26 @@ export class Func {
247
* let output1, output2;
248
*/
249
emitReactiveBlockOutputDecls(block: LIR.ReactiveBlock) {
250
- if (block.outputDecls.size > 0) {
250
+ const vars: t.VariableDeclarator[] = [];
251
+ const lets: t.VariableDeclarator[] = [];
252
+
253
+ for (const decl of block.outputDecls) {
254
+ if (decl.binding.kind === "var") {
255
+ vars.push(t.variableDeclarator(decl.binding.identifier));
256
+ } else {
257
+ lets.push(t.variableDeclarator(decl.binding.identifier));
258
+ }
259
+ }
260
+
261
+ if (vars.length > 0) {
262
this.code.push(
252
- t.variableDeclaration(
253
- /*kind*/ "let",
254
- /*declarators*/ [...block.outputDecls.values()].map((output) =>
255
- t.variableDeclarator(/*id*/ output.binding.identifier)
256
- )
257
- )
263
+ t.variableDeclaration(/*kind*/ "var", /*declarators*/ vars)
264
+ );
265
+ }
266
+
267
+ if (lets.length > 0) {
268
+ this.code.push(
269
+ t.variableDeclaration(/*kind*/ "let", /*declarators*/ lets)
270
);
271
}
272
}