Skip to content

feat: add Emscripten support to the WASM generator#26

Merged
owenthcarey merged 2 commits into
mainfrom
feat/wasm-emscripten-support
Jul 7, 2026
Merged

feat: add Emscripten support to the WASM generator#26
owenthcarey merged 2 commits into
mainfrom
feat/wasm-emscripten-support

Conversation

@owenthcarey

@owenthcarey owenthcarey commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds an emscripten flag to the WASM generator config. When set, loadWeaveffiWasm accepts a pre-initialized Emscripten Module (or the promise returned by its MODULARIZE factory) instead of a .wasm URL. The loader prologue binds the module's underscore-prefixed exports to the symbol names the glue calls (quoted bracket access for Closure Compiler safety), with a live get memory() getter over HEAPU8 so memory growth is handled. Everything after the prologue is byte-identical to the standard loader; no standard-mode .js snapshot changed.
  • Cherry-picks the cabi.rs visibility change from the fork: {PREFIX}_API now expands to __attribute__((used, visibility("default"))) under __EMSCRIPTEN__ (the EMSCRIPTEN_KEEPALIVE expansion), so exports survive dead-code elimination without an -sEXPORTED_FUNCTIONS list.
  • Closes the allocator ABI gap instead of substituting _malloc/_free: the generated C header now declares {prefix}_alloc / {prefix}_dealloc alongside the other runtime helpers, and the {prefix}.c scaffold provides malloc/free-backed defaults.
  • Fixes the .d.ts byte type from the Node-only Buffer to Uint8Array.
  • Async functions in Emscripten mode become explicit throwing stubs and are omitted from the .d.ts (the same treatment listeners get); documented in the generated README and the book.

Credit

Based on the design validation by @ariankordi, who proved out Emscripten support end to end in the emscripten-support fork and reported it in #22. This PR implements the loader through a config flag plus a load-time export adapter rather than per-call-site branching, but the cabi.rs visibility fix and the overall validation come from that work.

Closes #22.

Test plan

  • 7 new unit tests in weaveffi-gen-wasm: adapter loader shape, prefixed runtime bindings, byte-identical shared helpers between modes, async stubs, .d.ts loader signature and async omission, Uint8Array mapping
  • New CLI end-to-end test for inline generators.wasm.emscripten: true
  • cargo test --workspace passes; the 32 changed snapshots (C/C++ headers, weaveffi.c, two .d.ts) reviewed and accepted
  • cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings clean
  • Generated Emscripten-mode JS parses and imports under Node; generated weaveffi.c compiles with -Wall -Wextra -Werror

@owenthcarey owenthcarey merged commit d05349f into main Jul 7, 2026
21 checks passed
@owenthcarey owenthcarey deleted the feat/wasm-emscripten-support branch July 7, 2026 04:55
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.

feat: Emscripten support for WASM

1 participant