From 517db278aaceade1d95eab2dc3ef5b6bed2a9189 Mon Sep 17 00:00:00 2001 From: hufeng Date: Thu, 16 Sep 2021 19:05:21 +0800 Subject: [PATCH] fixed d8d1e08 from https://gitee.com/hufeng20/ark_ts2abc/pulls/25 fix function's name setting Signed-off-by: hufeng Change-Id: Ia4e1e219c4f8180ad0363164561ad6a76bb4ae40 --- ts2panda/src/base/bcGenUtil.ts | 10 ++++--- .../src/expression/objectLiteralExpression.ts | 26 ++++++++++++++++--- ts2panda/src/pandagen.ts | 21 +++++++-------- ts2panda/src/statement/classStatement.ts | 2 +- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/ts2panda/src/base/bcGenUtil.ts b/ts2panda/src/base/bcGenUtil.ts index cf7c3534be..4f19537799 100755 --- a/ts2panda/src/base/bcGenUtil.ts +++ b/ts2panda/src/base/bcGenUtil.ts @@ -71,6 +71,8 @@ import { EcmaStownbyindex, EcmaStownbyname, EcmaStownbyvalue, + EcmaStownbynamewithnameset, + EcmaStownbyvaluewithnameset, EcmaStsuperbyname, EcmaStsuperbyvalue, EcmaSupercall, @@ -218,16 +220,16 @@ export function storeObjByValue(obj: VReg, prop: VReg): IRNode { return new EcmaStobjbyvalue(obj, prop); } -export function storeOwnByName(obj: VReg, key: string): IRNode { - return new EcmaStownbyname(key, obj); +export function storeOwnByName(obj: VReg, key: string, nameSetting: boolean): IRNode { + return nameSetting ? new EcmaStownbynamewithnameset(key, obj) : new EcmaStownbyname(key, obj); } export function storeOwnByIndex(obj: VReg, index: number) { return new EcmaStownbyindex(obj, new Imm(ResultType.Int, index)); } -export function storeOwnByValue(obj: VReg, value: VReg) { - return new EcmaStownbyvalue(obj, value); +export function storeOwnByValue(obj: VReg, value: VReg, nameSetting: boolean) { + return nameSetting ? new EcmaStownbyvaluewithnameset(obj, value) : new EcmaStownbyvalue(obj, value); } export function throwIfSuperNotCorrectCall(num: number) { diff --git a/ts2panda/src/expression/objectLiteralExpression.ts b/ts2panda/src/expression/objectLiteralExpression.ts index 574b8d9a6d..b7f34f646d 100644 --- a/ts2panda/src/expression/objectLiteralExpression.ts +++ b/ts2panda/src/expression/objectLiteralExpression.ts @@ -19,6 +19,7 @@ import * as jshelpers from "../jshelpers"; import { getParamLengthOfFunc } from "../base/util"; import { CacheList, getVregisterCache } from "../base/vregisterCache"; import { isInteger } from "./numericLiteral"; +import { findInnerExprOfParenthesis } from "./parenthesizedExpression"; import { PandaGen } from "../pandagen"; import { VReg } from "../irnodes"; import { PropertyKind, Property, generatePropertyFromExpr } from "../base/properties"; @@ -205,12 +206,13 @@ function compileComputedProperty(compiler: Compiler, prop: Property, objReg: VRe switch (prop.getValue().kind) { case ts.SyntaxKind.PropertyAssignment: { compiler.compileExpression((prop.getValue()).initializer); - pandaGen.storeOwnProperty(prop.getValue(), objReg, keyReg); + let nameSetting: boolean = needSettingName((prop.getValue()).initializer); + pandaGen.storeOwnProperty(prop.getValue(), objReg, keyReg, nameSetting); break; } case ts.SyntaxKind.MethodDeclaration: { createMethodOrAccessor(pandaGen, compiler, objReg, prop.getValue()); - pandaGen.storeOwnProperty(prop.getValue(), objReg, keyReg); + pandaGen.storeOwnProperty(prop.getValue(), objReg, keyReg, true); break; } case ts.SyntaxKind.GetAccessor: { @@ -265,12 +267,15 @@ function setUncompiledProperties(compiler: Compiler, pandaGen: PandaGen, propert } case PropertyKind.Constant: case PropertyKind.Variable: { + let nameSetting: boolean = false; if (ts.isMethodDeclaration(prop.getValue())) { createMethodOrAccessor(pandaGen, compiler, objReg, prop.getValue()); } else { compiler.compileExpression(prop.getValue()); + nameSetting = needSettingName(prop.getValue()) + && ((prop.getName())).toString().lastIndexOf('.') != -1; } - pandaGen.storeOwnProperty(prop.getValue().parent, objReg, (prop.getName())); + pandaGen.storeOwnProperty(prop.getValue().parent, objReg, (prop.getName()), nameSetting); break; } case PropertyKind.Prototype: { @@ -294,4 +299,19 @@ export function createMethodOrAccessor(pandaGen: PandaGen, compiler: Compiler, o } else { pandaGen.defineMethod(func, internalName, objReg, env); } +} + +function needSettingName(node: ts.Node): boolean { + let tempNode: ts.Node = node; + if (ts.isParenthesizedExpression(node)) { + tempNode = findInnerExprOfParenthesis(node); + } + + if (ts.isFunctionLike(tempNode) || ts.isClassLike(tempNode)) { + let funcOrClassNode = tempNode; + if (!funcOrClassNode.name) { + return true; + } + } + return false; } \ No newline at end of file diff --git a/ts2panda/src/pandagen.ts b/ts2panda/src/pandagen.ts index 7f638f4a17..e823a72174 100644 --- a/ts2panda/src/pandagen.ts +++ b/ts2panda/src/pandagen.ts @@ -480,7 +480,7 @@ export class PandaGen { } } - storeOwnProperty(node: ts.Node | NodeKind, obj: VReg, prop: VReg | string | number) { + storeOwnProperty(node: ts.Node | NodeKind, obj: VReg, prop: VReg | string | number, nameSetting: boolean = false) { switch (typeof prop) { case "number": { if (isInteger(prop)) { @@ -495,16 +495,16 @@ export class PandaGen { storeAccumulator(propReg), loadAccumulator(valueReg) ); - this.stOwnByValue(node, obj, propReg); + this.stOwnByValue(node, obj, propReg, nameSetting); this.freeTemps(valueReg, propReg); } break; } case "string": - this.stOwnByName(node, obj, prop); + this.stOwnByName(node, obj, prop, nameSetting); break; default: - this.stOwnByValue(node, obj, prop); + this.stOwnByValue(node, obj, prop, nameSetting); } } @@ -551,19 +551,16 @@ export class PandaGen { ) } - private stOwnByName(node: ts.Node | NodeKind, obj: VReg, string_id: string) { - this.add(node, storeOwnByName(obj, string_id)); + private stOwnByName(node: ts.Node | NodeKind, obj: VReg, string_id: string, nameSetting: boolean) { + this.add(node, storeOwnByName(obj, string_id, nameSetting)); } private stOwnByIndex(node: ts.Node | NodeKind, obj: VReg, index: number) { - this.add( - node, - storeOwnByIndex(obj, index) - ) + this.add(node, storeOwnByIndex(obj, index)); } - private stOwnByValue(node: ts.Node | NodeKind, obj: VReg, value: VReg) { - this.add(node, storeOwnByValue(obj, value)); + private stOwnByValue(node: ts.Node | NodeKind, obj: VReg, value: VReg, nameSetting: boolean) { + this.add(node, storeOwnByValue(obj, value, nameSetting)); } // eg. print diff --git a/ts2panda/src/statement/classStatement.ts b/ts2panda/src/statement/classStatement.ts index 1eaffd17b6..1163c9e0b8 100644 --- a/ts2panda/src/statement/classStatement.ts +++ b/ts2panda/src/statement/classStatement.ts @@ -561,7 +561,7 @@ function compileComputedProperty(compiler: Compiler, prop: Property, classReg: V let protoReg = pandaGen.getTemp(); let tmpReg = pandaGen.getTemp(); let flag = createClassMethodOrAccessor(compiler, classReg, protoReg, tmpReg, prop.getValue()); - pandaGen.storeOwnProperty(prop.getValue(), flag ? protoReg : classReg, keyReg); + pandaGen.storeOwnProperty(prop.getValue(), flag ? protoReg : classReg, keyReg, true); pandaGen.freeTemps(protoReg, tmpReg); break; } -- Gitee