From 499fa612152ed92cc603063d56a4624f2f093201 Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 8 Jun 2022 11:31:50 +0800 Subject: [PATCH] fixed 4a10a65 from https://gitee.com/hufeng20/ark_ts2abc/pulls/295 Fix loading func obj wrongly in methodBody Signed-off-by: hufeng Change-Id: I80d7b058e14dd4fd741ec155ca883d91be78d78b --- ts2panda/src/compiler.ts | 4 ++-- ts2panda/src/scope.ts | 7 +++++++ ts2panda/ts2abc/main.cpp | 3 ++- ts2panda/ts2abc/ts2abc.cpp | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index 59cd1d776e..94b970de43 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -232,14 +232,14 @@ export class Compiler { private storeFuncObj2LexEnvIfNeeded() { let rootNode = this.rootNode; - if (!ts.isFunctionExpression(rootNode) && !ts.isMethodDeclaration(rootNode)) { + if (!ts.isFunctionExpression(rootNode)) { return; } let functionScope = this.recorder.getScopeOfNode(rootNode); if ((rootNode).name) { let funcName = jshelpers.getTextOfIdentifierOrLiteral((rootNode).name); let v = functionScope.find(funcName); - if (v.scope == functionScope) { + if (v.scope == functionScope && v.v.declKind === VarDeclarationKind.FUNCTION) { this.pandaGen.loadAccumulator(NodeKind.FirstNodeOfFunction, getVregisterCache(this.pandaGen, CacheList.FUNC)); this.pandaGen.storeAccToLexEnv(NodeKind.FirstNodeOfFunction, v.scope, v.level, v.v, true); } diff --git a/ts2panda/src/scope.ts b/ts2panda/src/scope.ts index 24e7ebb468..8589769867 100644 --- a/ts2panda/src/scope.ts +++ b/ts2panda/src/scope.ts @@ -495,6 +495,13 @@ export class FunctionScope extends VariableScope { v = topLevelScope.add(name, declKind); } else if (declKind == VarDeclarationKind.VAR || declKind == VarDeclarationKind.FUNCTION) { v = new LocalVariable(declKind, name); + if (declKind === VarDeclarationKind.FUNCTION && ts.isFunctionExpression((decl).node)) { + for (let para of this.parameters) { + if (para.getName() === name) { + return v; + } + } + } this.name2variable.set(name, v); } else { v = new LocalVariable(declKind, name, status); diff --git a/ts2panda/ts2abc/main.cpp b/ts2panda/ts2abc/main.cpp index 3d69fada83..68456831d4 100644 --- a/ts2panda/ts2abc/main.cpp +++ b/ts2panda/ts2abc/main.cpp @@ -91,8 +91,9 @@ int main(int argc, const char *argv[]) return RETURN_FAILED; } + std::string optLogLevel = options.GetOptLogLevelArg(); if (!GenerateProgram(data, output, options.GetCompileByPipeArg(), - options.GetOptLevelArg(), options.GetOptLogLevelArg())) { + options.GetOptLevelArg(), optLogLevel)) { std::cerr << "call GenerateProgram fail" << std::endl; return RETURN_FAILED; } diff --git a/ts2panda/ts2abc/ts2abc.cpp b/ts2panda/ts2abc/ts2abc.cpp index bd84aaa274..402703e594 100644 --- a/ts2panda/ts2abc/ts2abc.cpp +++ b/ts2panda/ts2abc/ts2abc.cpp @@ -1277,8 +1277,8 @@ static bool ReadFromPipe(panda::pandasm::Program &prog) return true; } -bool GenerateProgram([[maybe_unused]] const std::string &data, std::string output, bool isParsingFromPipe, - int optLevel, std::string optLogLevel) +bool GenerateProgram([[maybe_unused]] const std::string &data, std::string &output, bool isParsingFromPipe, + int optLevel, std::string &optLogLevel) { panda::pandasm::Program prog = panda::pandasm::Program(); prog.lang = panda::pandasm::extensions::Language::ECMASCRIPT; -- Gitee