From 8d4cb6faf6e023b50877dd0b4ba65cc550f509c7 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Thu, 1 Sep 2022 15:41:09 +0800 Subject: [PATCH] Fix lexical variable load in for-in head Issue:I5QW8A Signed-off-by: ctw-ian Change-Id: I2af342753e87629d65d67c27f44bd67e59394a04 --- ts2panda/src/recorder.ts | 7 ++----- ts2panda/src/statement/forOfStatement.ts | 10 ++++++++++ ts2panda/src/statement/loopStatement.ts | 22 ++++++++++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index e3ba10ca90..fa4ac23701 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -51,11 +51,9 @@ import { extractCtorOfClass } from "./statement/classStatement"; import { checkSyntaxError } from "./syntaxChecker"; -import { isGlobalIdentifier } from "./syntaxCheckHelper"; +import { isGlobalIdentifier, isFunctionLikeDeclaration } from "./syntaxCheckHelper"; import { TypeChecker } from "./typeChecker"; import { VarDeclarationKind } from "./variable"; -import { TypeRecorder } from "./typeRecorder"; -import { PandaGen } from "./pandagen"; import path from "path"; export class Recorder { @@ -626,8 +624,7 @@ export class Recorder { this.collectHoistDecls(node, hoistScope, funcDecl); } else if (scope instanceof LocalScope) { hoistScope = scope.getNearestVariableScope(); - let expectHoistScope = this.getScopeOfNode(node.parent.parent); - if ((hoistScope == expectHoistScope) && (hoistScope instanceof FunctionScope)) { + if ((hoistScope instanceof FunctionScope) && isFunctionLikeDeclaration(node.parent.parent)) { need2AddDecls = this.collectHoistDecls(node, hoistScope, funcDecl); } } else { diff --git a/ts2panda/src/statement/forOfStatement.ts b/ts2panda/src/statement/forOfStatement.ts index ca3fb6a40c..32458918c2 100644 --- a/ts2panda/src/statement/forOfStatement.ts +++ b/ts2panda/src/statement/forOfStatement.ts @@ -70,9 +70,19 @@ export function compileForOfStatement(stmt: ts.ForOfStatement, compiler: Compile // for now Async is not handled. let type: IteratorType = IteratorType.Normal; + if (needCreateLoopEnv) { + pandaGen.createLexEnv(stmt, loopEnv, loopScope); + compiler.pushEnv(loopEnv); + } + compiler.compileExpression(stmt.expression); let iterator: IteratorRecord = getIteratorRecord(pandaGen, stmt, method, object, type); + if (needCreateLoopEnv) { + pandaGen.popLexicalEnv(stmt); + compiler.popEnv(); + } + pandaGen.loadAccumulator(stmt, getVregisterCache(pandaGen, CacheList.False)); pandaGen.storeAccumulator(stmt, doneReg); diff --git a/ts2panda/src/statement/loopStatement.ts b/ts2panda/src/statement/loopStatement.ts index f7e1bd0d8f..4af9a4a576 100644 --- a/ts2panda/src/statement/loopStatement.ts +++ b/ts2panda/src/statement/loopStatement.ts @@ -199,6 +199,12 @@ export function compileForStatement(stmt: ts.ForStatement, compiler: Compiler) { compiler.popEnv(); pandaGen.freeTemps(...tmpVregs); } else { // compile for in fast mode + // createLoopEnv if needed + if (needCreateLoopEnv) { + pandaGen.createLexEnv(stmt, loopEnv, loopScope); + compiler.pushEnv(loopEnv); + } + if (stmt.initializer) { if (ts.isVariableDeclarationList(stmt.initializer)) { let declList = stmt.initializer; @@ -211,12 +217,6 @@ export function compileForStatement(stmt: ts.ForStatement, compiler: Compiler) { // loopCondition pandaGen.label(stmt, loopStartLabel); - // createLoopEnv if needed - if (needCreateLoopEnv) { - pandaGen.createLexEnv(stmt, loopEnv, loopScope); - compiler.pushEnv(loopEnv); - } - if (stmt.condition) { compiler.compileCondition(stmt.condition, loopEndLabel); } @@ -268,11 +268,21 @@ export function compileForInStatement(stmt: ts.ForInStatement, compiler: Compile let iterReg = pandaGen.getTemp(); let propName = pandaGen.getTemp(); + if (needCreateLexEnv) { + pandaGen.createLexEnv(stmt, loopEnv, loopScope); + compiler.pushEnv(loopEnv); + } + // create enumerator compiler.compileExpression(stmt.expression); pandaGen.getPropIterator(stmt); pandaGen.storeAccumulator(stmt, iterReg); + if (needCreateLexEnv) { + pandaGen.popLexicalEnv(stmt); + compiler.popEnv(); + } + pandaGen.label(stmt, loopStartLabel); if (needCreateLexEnv) { pandaGen.createLexEnv(stmt, loopEnv, loopScope); -- Gitee