| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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; | ||
| } |
| 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()) |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.