-
Notifications
You must be signed in to change notification settings - Fork 20
feat: optionally use WASM function names for compiled methods #168
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
Merged
andreaTP
merged 6 commits into
bytecodealliance:main
from
andreas-karlsson:named-methods
Sep 3, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3173a98
feat: optionally use WASM function names for compiled methods
andreas-karlsson 46eca27
update compiler docs
andreas-karlsson f824711
method prefix proposal
andreas-karlsson d8a64ed
Fix the docs snippet and resolve func ids by exact lookup
andreaTP b4fb7e1
Trim method name javadoc
andreaTP 401b8b4
Scan the method names instead of indexing them in reverse
andreaTP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
compiler/src/main/java/run/endive/compiler/MethodPrefixer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package run.endive.compiler; | ||
|
|
||
| import run.endive.wasm.WasmModule; | ||
|
|
||
| /** | ||
| * Supplies the human readable prefix used when naming the JVM method compiled for a WASM function. | ||
| * | ||
| * <p>The compiler derives every method name as {@code <sanitized prefix>_<funcId>}. The prefixer | ||
| * only controls the prefix; the compiler owns the rest of the name. That split keeps two | ||
| * invariants that the rest of the compiler and any external tooling can rely on, regardless of | ||
| * what a prefixer returns: | ||
| * | ||
| * <ul> | ||
| * <li>method names are unique, because the function id is unique | ||
| * <li>the function id can always be recovered by parsing the {@code _<funcId>} suffix | ||
| * </ul> | ||
| * | ||
| * <p>Characters that are illegal in a JVM method name ({@code . ; [ / < >}, see | ||
| * <a href="https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.2.2">JVM Spec | ||
| * §4.2.2</a>) are replaced with {@code _}. A prefixer that needs to preserve the original name | ||
| * exactly can avoid the substitution by encoding those characters itself, for example by | ||
| * percent-encoding them. | ||
| * | ||
| * <p>The prefix is a hint for humans reading a thread dump or a profile. Tooling should never | ||
| * parse it; it should use the function id instead. | ||
| */ | ||
| @FunctionalInterface | ||
| public interface MethodPrefixer { | ||
|
|
||
| /** | ||
| * The prefix used when no prefixer is configured, and the fallback whenever a prefixer returns | ||
| * {@code null}, an empty string, or a string that sanitizes to nothing. | ||
| */ | ||
| String DEFAULT_PREFIX = "func"; | ||
|
|
||
| /** | ||
| * Returns the prefix for the method compiled for {@code funcId}, or {@code null} to use | ||
| * {@link #DEFAULT_PREFIX}. | ||
| * | ||
| * @param funcId the WASM function index, covering imported and defined functions | ||
| * @param module the module being compiled | ||
| */ | ||
| String getMethodPrefix(int funcId, WasmModule module); | ||
|
|
||
| /** Returns the default prefixer, naming every method {@value #DEFAULT_PREFIX}. */ | ||
| static MethodPrefixer defaultPrefixer() { | ||
| return (funcId, module) -> DEFAULT_PREFIX; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a prefixer that uses the function name from the module's name custom section, falling | ||
| * back to {@link #DEFAULT_PREFIX} for functions without one. | ||
| */ | ||
| static MethodPrefixer fromNameSection() { | ||
| return (funcId, module) -> { | ||
| var nameSection = module.nameSection(); | ||
| return nameSection == null ? null : nameSection.nameOfFunction(funcId); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe more correct to accept empty prefix?