Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-128627: Use __builtin_wasm_test_function_pointer_signature for Emscripten trampoline#137470
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
freakboy3742 merged 18 commits into python:main from hoodmane:__builtin_wasm_test_function_pointer_signatureSep 17, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
18 commits Select commit Hold shift + click to select a range
0ffb76b gh-128627: Use __builtin_wasm_test_function_pointer_signature for Ems…
hoodmane 04e0541 Add forgotten file
hoodmane b633734 Mark global as const
hoodmane 538e563 Add prefix to globals
hoodmane 7ed9118 Add a comment
hoodmane fff815c Update comment
hoodmane 6fd7bfe Merge branch 'main' into __builtin_wasm_test_function_pointer_signature
freakboy3742 e96892e New approach
hoodmane c48298d Merge branch 'main' into __builtin_wasm_test_function_pointer_signature
hoodmane fd20053 Clean up
hoodmane 3379e1c More comments
hoodmane 7d5a013 Cleanup
hoodmane b80911b Reduce diff
hoodmane b4f07d9 Reduce diff
hoodmane 51af9e1 Cleanup
hoodmane a1753d6 typedef => #define
hoodmane 0ca9e4c Fix again
hoodmane 4002452 Add comment
hoodmane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
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,38 @@ | ||
| // This file must be compiled with -mgc to enable the extra wasm-gc | ||
| // instructions. It has to be compiled separately because not enough JS runtimes | ||
| // support wasm-gc yet. If the JS runtime does not support wasm-gc (or has buggy | ||
| // support like iOS), we will use the JS trampoline fallback. | ||
| // We can't import Python.h here because it is compiled/linked with -nostdlib. | ||
| // We don't need to know what's inside PyObject* anyways. We could just call it | ||
| // void* everywhere. There are two reasons to do this: | ||
| // 1. to improve readability | ||
| // 2. eventually when we are comfortable requiring wasm-gc, we can merge this | ||
| // into emscripten_trampoline.c without worrying about it. | ||
| typedef void PyObject; | ||
| typedef PyObject* (*three_arg)(PyObject*, PyObject*, PyObject*); | ||
| typedef PyObject* (*two_arg)(PyObject*, PyObject*); | ||
| typedef PyObject* (*one_arg)(PyObject*); | ||
| typedef PyObject* (*zero_arg)(void); | ||
| #define TRY_RETURN_CALL(ty, args...) \ | ||
| if (__builtin_wasm_test_function_pointer_signature((ty)func)){\ | ||
| return ((ty)func)(args); \ | ||
| } | ||
| __attribute__((export_name("trampoline_call"))) PyObject* | ||
| trampoline_call(int* success, | ||
| void* func, | ||
| PyObject* self, | ||
| PyObject* args, | ||
| PyObject* kw) | ||
| { | ||
| *success = 1; | ||
| TRY_RETURN_CALL(three_arg, self, args, kw); | ||
| TRY_RETURN_CALL(two_arg, self, args); | ||
| TRY_RETURN_CALL(one_arg, self); | ||
| TRY_RETURN_CALL(zero_arg); | ||
| *success = 0; | ||
| return 0; | ||
| } |
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
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,54 @@ | ||
| #!/usr/bin/env python3 | ||
| import argparse | ||
| import sys | ||
| from pathlib import Path | ||
| JS_TEMPLATE = """ | ||
| #include "emscripten.h" | ||
| EM_JS(void,{function_name}, (void),{{ | ||
| return new WebAssembly.Module(hexStringToUTF8Array("{hex_string}")); | ||
| }} | ||
| function hexStringToUTF8Array(hex){{ | ||
| const bytes = []; | ||
| for (let i = 0; i < hex.length; i += 2){{ | ||
| bytes.push(parseInt(hex.substr(i, 2), 16)); | ||
| }} | ||
| return new Uint8Array(bytes); | ||
| }}); | ||
| """ | ||
| def prepare_wasm(input_file, output_file, function_name): | ||
| # Read the compiled WASM as binary and convert to hex | ||
| wasm_bytes = Path(input_file).read_bytes() | ||
| hex_string = "".join(f"{byte:02x}" for byte in wasm_bytes) | ||
| # Generate JavaScript module | ||
| js_content = JS_TEMPLATE.format( | ||
| function_name=function_name, hex_string=hex_string | ||
| ) | ||
| Path(output_file).write_text(js_content) | ||
| print( | ||
| f"Successfully compiled{input_file} and generated{output_file}" | ||
| ) | ||
| return 0 | ||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Compile WebAssembly text files using wasm-as" | ||
| ) | ||
| parser.add_argument("input_file", help="Input .wat file to compile") | ||
| parser.add_argument("output_file", help="Output file name") | ||
| parser.add_argument("function_name", help="Name of the export function") | ||
| args = parser.parse_args() | ||
| return prepare_wasm(args.input_file, args.output_file, args.function_name) | ||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.