diff --git a/src/scope.ts b/src/scope.ts index 0ece0df8..f545a68c 100644 --- a/src/scope.ts +++ b/src/scope.ts @@ -1213,12 +1213,17 @@ export class ScopeScanner { } else { if (ts.isPropertyAssignment(node.parent)) { functionName = node.parent.name.getText(); + functionScope.envParamLen++; + const thisVar = new Variable('this', new Type()); + thisVar.setVarIsClosure(); + functionScope.addVariable(thisVar); + functionScope.setClassName(node.parent.parent.getText()); } else { functionName = '@anonymous' + this.anonymousIndex++; } } /* function context struct placeholder */ - functionScope.envParamLen = 1; + functionScope.envParamLen++; this.nodeScopeMap.set(node, functionScope); if (functionScope.isDeclare()) { diff --git a/src/type.ts b/src/type.ts index db0e11ee..dd673c63 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1590,23 +1590,13 @@ export class TypeResolver { const propType = this.typechecker!.getTypeAtLocation(valueDecl); const tsType = this.tsTypeToType(propType); - if (tsType instanceof TSFunction && tsType.isMethod) { - if (tsType.envParamLen == 1) { - tsClass.addMethod({ - name: propName, - type: tsType as TSFunction, - optional: (valueDecl as any).questionToken - ? true - : false, - }); - } else { - this.setMethod( - valueDecl as ts.MethodDeclaration, - null, - tsClass, - FunctionKind.METHOD, - ); - } + if (tsType instanceof TSFunction && tsType.envParamLen == 2) { + this.setMethod( + valueDecl as ts.MethodDeclaration, + null, + tsClass, + FunctionKind.METHOD, + ); } else { tsClass.addMemberField({ name: propName, @@ -1667,6 +1657,10 @@ export class TypeResolver { ts.isAccessor(decl); /* get env type length: @context & @this */ + const funcScope = this.nodeScopeMap.get(decl); + if (funcScope) { + tsFunction.envParamLen = (funcScope as FunctionScope).envParamLen; + } if (tsFunction.envParamLen === 0) { tsFunction.envParamLen = tsFunction.isMethod && !tsFunction.isStatic ? 2 : 1; diff --git a/tests/samples/obj_literal.ts b/tests/samples/obj_literal.ts index 4fcb0ee0..f860f901 100644 --- a/tests/samples/obj_literal.ts +++ b/tests/samples/obj_literal.ts @@ -103,7 +103,7 @@ export function useThisInLiteralObj() { console.log(this.name); return n; }, - say2(){ + say2() { console.log(this.name); } @@ -123,4 +123,17 @@ export function useThisInLiteralObj() { } console.log(b.say(1)); b.say2(); + + const a2 = { + name: "a2", + say(n: number) { + console.log(this.name); + return n; + }, + say2: function() { + console.log(this.name); + } + } + console.log(a2.say(1)); + a2.say2(); } \ No newline at end of file diff --git a/tools/validate/wamr/validation.json b/tools/validate/wamr/validation.json index 68a50e0d..dad8bc55 100644 --- a/tools/validate/wamr/validation.json +++ b/tools/validate/wamr/validation.json @@ -2915,7 +2915,7 @@ { "name": "useThisInLiteralObj", "args": [], - "result": "A\n1\nA\nB\n1\nB" + "result": "A\n1\nA\nB\n1\nB\na2\n1\na2" } ] },