diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index e3ba10ca90838821142f396a2929c62704838865..fa4ac237017c534ea18f747bbad0dfa82add4d50 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 ca3fb6a40c00363f97f0935b2960ae995bbd5beb..32458918c289e43d3c8301af630f42277196f93e 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 f7e1bd0d8fa5e74caf7f899ead52cc6f577b81fb..4af9a4a5766df9df14ea1490c8629f7359689ce2 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);