From c1cb25f7d39cf7fa9b1959f7a04e5b2b67c1caa6 Mon Sep 17 00:00:00 2001 From: ctw Date: Thu, 24 Mar 2022 17:04:20 +0800 Subject: [PATCH] fix losing function name of function expression Signed-off-by: ctw Change-Id: I63125789ef48d98d067965f795ad228e56dcd25d Signed-off-by: ctw --- ts2panda/src/addVariable2Scope.ts | 8 +++++++- ts2panda/src/compiler.ts | 17 +++++++++++++++++ ts2panda/src/recorder.ts | 14 +++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/ts2panda/src/addVariable2Scope.ts b/ts2panda/src/addVariable2Scope.ts index 8d65f686c1..692d9a1032 100644 --- a/ts2panda/src/addVariable2Scope.ts +++ b/ts2panda/src/addVariable2Scope.ts @@ -135,7 +135,13 @@ export function addVariableToScope(recorder: Recorder, enableTypeRecord: boolean } else if (decl instanceof ConstDecl) { v = scope.add(decl.name, VarDeclarationKind.CONST, InitStatus.UNINITIALIZED); } else if (decl instanceof FuncDecl) { - v = scope.add(decl.name, VarDeclarationKind.FUNCTION); + let funcNode = decl.node; + if (ts.isFunctionDeclaration(funcNode)) { + v = scope.add(decl.name, VarDeclarationKind.FUNCTION); + } else { + let functionScope = recorder.getScopeOfNode(funcNode); + v = functionScope.add(decl.name, VarDeclarationKind.FUNCTION); + } } else if (decl instanceof CatchParameter) { v = scope.add(decl.name, VarDeclarationKind.LET); } else if (decl instanceof ClassDecl) { diff --git a/ts2panda/src/compiler.ts b/ts2panda/src/compiler.ts index 26268be68d..11bfe99630 100644 --- a/ts2panda/src/compiler.ts +++ b/ts2panda/src/compiler.ts @@ -157,6 +157,7 @@ export class Compiler { } compile() { + this.storeFuncObj2LexEnvIfNeeded(); this.compileLexicalBindingForArrowFunction(); if (this.rootNode.kind == ts.SyntaxKind.SourceFile) { @@ -230,6 +231,22 @@ export class Compiler { } } + private storeFuncObj2LexEnvIfNeeded() { + let rootNode = this.rootNode; + if (!ts.isFunctionExpression(rootNode) || !ts.isMethodDeclaration(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) { + this.pandaGen.loadAccumulator(rootNode, getVregisterCache(this.pandaGen, CacheList.FUNC)); + this.pandaGen.storeAccToLexEnv(rootNode, v.scope, v.level, v.v, true); + } + } + } + private compileLexicalBindingForArrowFunction() { let rootNode = this.rootNode; diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index 75bca228e8..2eb92ab545 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -128,7 +128,7 @@ export class Recorder { case ts.SyntaxKind.SetAccessor: case ts.SyntaxKind.ArrowFunction: { let functionScope = this.buildVariableScope(scope, childNode); - this.recordFuncInfo(childNode); + this.recordOtherFunc(childNode, functionScope); this.recordInfo(childNode, functionScope); break; } @@ -459,6 +459,18 @@ export class Recorder { } } + private recordOtherFunc(node: ts.FunctionLikeDeclaration, scope: Scope) { // functionlikedecalration except function declaration + this.recordFuncInfo(node); + if (!ts.isFunctionExpression(node) && !ts.isMethodDeclaration(node)) { + return; + } + if (node.name && ts.isIdentifier(node.name)) { + let funcName = jshelpers.getTextOfIdentifierOrLiteral(node.name); + let funcDecl = new FuncDecl(funcName, node); + scope.setDecls(funcDecl); + } + } + private recordFuncInfo(node: ts.FunctionLikeDeclaration) { this.recordFunctionParameters(node); this.recordFuncName(node); -- Gitee