Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement generic functions #16

Merged
merged 1 commit into from
Oct 8, 2023

Conversation

Shanks0224
Copy link
Contributor

No description provided.

@Shanks0224
Copy link
Contributor Author

PTAL @xujuntwt95329

@@ -521,6 +531,7 @@ export class Scope {
scope.localIndex = this.localIndex;
scope.mangledName = this.mangledName;
scope.modifiers = this.modifiers;
if (this.genericOwner) scope.setGenericOwner(this.genericOwner);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy and clone, which is used for generic specialization?
According to the function name, I would expect the result scope is the same as the original one (e.g. the genericOwner field should be the same), but this line breaks this assumption.
I would suggest to use specialize as the method name used to process generic specialization.

Copy link
Contributor Author

@Shanks0224 Shanks0224 Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
The name of method was changed to specialize.

src/scope.ts Outdated

constructor(parent: Scope) {
constructor(parent: Scope | null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it be possible to have a null parent scope?

Copy link
Contributor Author

@Shanks0224 Shanks0224 Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
This does seem redundant and wrong.

@@ -921,6 +923,14 @@ function buildArrayLiteralExpression(

// return new NewLiteralArrayValue(array_type!, init_values);
const elem_type = (array_type as ArrayType).element;
// Workaround: solve the case that the return value of a generic function is a generic array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this only work for return value of a generic function? Seems it should also work for assignment statement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
In buildArrayLiteralExpression, I made common logic to process generic arrays.It does not only work for return value of a generic function.

@@ -1276,6 +1286,14 @@ export function newCastValue(
return value;
}

// Workaround: solve the case that the return value of a generic function is a generic array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
This code has been deleted

newParam.setIsLocalVar(v.isLocalVar());
newParam.needReBinding = v.needReBinding;
newParam.tsNode = v.tsNode;
f.addParameter(newParam);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the specialized function scope is created in frontend, and the parameters are added in semantic tree, and it is same as class specialization too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,the same goes for generic member functions in generic class.

src/utils.ts Outdated
* @returns a new specialized ClassScope
*/
export function createClassScopeByClassType(
originalClassScope: ClassScope,
parent: Scope,
parent: Scope | null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which situation, parent will be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.
This does seem redundant and wrong.

Copy link
Contributor

@yviansu yviansu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@xujuntwt95329 xujuntwt95329 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xujuntwt95329
Copy link
Contributor

@Shanks0224 Thanks for the great work!

@xujuntwt95329 xujuntwt95329 merged commit 16dfea5 into web-devkits:main Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants