Skip to content

Commit

Permalink
support of using this in propertyassignment function
Browse files Browse the repository at this point in the history
Signed-off-by: wenlingyun1 <[email protected]>
  • Loading branch information
WenLY1 committed Oct 10, 2023
1 parent be2cb6f commit 6213f31
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
28 changes: 11 additions & 17 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion tests/samples/obj_literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function useThisInLiteralObj() {
console.log(this.name);
return n;
},
say2(){
say2() {
console.log(this.name);
}

Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion tools/validate/wamr/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@
{
"name": "useThisInLiteralObj",
"args": [],
"result": "A\n1\nA\nB\n1\nB"
"result": "A\n1\nA\nB\n1\nB\na2\n1\na2"
}
]
},
Expand Down

0 comments on commit 6213f31

Please sign in to comment.