Skip to content

Implement instance methods#429

Closed
b-temirov wants to merge 11 commits intohkust-taco:hkmc2from
b-temirov:vmethods
Closed

Implement instance methods#429
b-temirov wants to merge 11 commits intohkust-taco:hkmc2from
b-temirov:vmethods

Conversation

@b-temirov
Copy link
Copy Markdown
Contributor

This PR adds class-method support for non-inheriting classes by lowering instance methods to standalone direct-call wasm functions with an explicit this parameter, and a prescan that predeclares constructors and methods so self recursion, mutual recursion, later-method calls, and constructor calls to same or later classes work by construction.

Comment thread hkmc2/shared/src/test/mlscript/wasm/ScopedLocals.mls Outdated
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls Outdated
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls Outdated
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls Outdated
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls Outdated
Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala Outdated
@LPTK
Copy link
Copy Markdown
Contributor

LPTK commented Apr 1, 2026

Is this ready for review/merge?

@b-temirov
Copy link
Copy Markdown
Contributor Author

Is this ready for review/merge?

Not quite. Inheritance coming soon on top. Or should I raise inheritance separately?

@LPTK
Copy link
Copy Markdown
Contributor

LPTK commented Apr 1, 2026

Yes, better keep the PRs small and merge them regularly.

@b-temirov b-temirov marked this pull request as ready for review April 1, 2026 10:58
Copilot AI review requested due to automatic review settings April 1, 2026 10:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds WebAssembly backend support for instance/class methods on (non-inheriting) classes by lowering methods into standalone direct-call Wasm functions with an explicit receiver (this) parameter, plus a prescan that predeclares constructors/methods to support recursion and forward references.

Changes:

  • Add prescan metadata registration for constructors and methods, and emit lowered method functions.
  • Route constructor/method calls through direct call to the lowered functions (instead of indirect call_ref) where applicable.
  • Add/adjust Wasm golden tests for methods and for constructor calls in matching output.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
hkmc2/shared/src/test/mlscript/wasm/Methods.mls New golden tests covering getters, nullary/callable methods, recursion, forward references, chaining, mutation, and error cases.
hkmc2/shared/src/test/mlscript/wasm/Matching.mls Update golden WAT output to reflect direct constructor calls (call) rather than call_ref.
hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala Implement method/ctor metadata prescan, emit lowered method funcs, and add direct-call lowering for constructors/methods and related selection/assignment handling.
hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Ctx.scala Add MethodInfo/ConstructorInfo metadata storage and accessors in the Wasm codegen context.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala Outdated
Copy link
Copy Markdown
Contributor

@LPTK LPTK left a comment

Choose a reason for hiding this comment

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

The logic seems quite complicated, for what this achieves. But let's try to merge this soon anyway, as soon as you have fixed what I point out.

Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala Outdated
Comment thread hkmc2/shared/src/test/mlscript/wasm/Methods.mls
@LPTK LPTK requested a review from Derppening April 1, 2026 14:00
Copy link
Copy Markdown
Contributor

@Derppening Derppening left a comment

Choose a reason for hiding this comment

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

Just a few preliminary comments for now. I will need to look into this in more detail.

Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Ctx.scala
Comment thread hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/Ctx.scala
This was referenced Apr 8, 2026
@b-temirov
Copy link
Copy Markdown
Contributor Author

This PR has been superseded by #456.

@b-temirov b-temirov closed this Apr 11, 2026
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.

4 participants