Skip to content

cstruct: ship a Wasm runtime (cstruct.wat), the analog of cstruct.js#320

Draft
samoht wants to merge 2 commits into
mirage:mainfrom
samoht:wasm-runtime
Draft

cstruct: ship a Wasm runtime (cstruct.wat), the analog of cstruct.js#320
samoht wants to merge 2 commits into
mirage:mainfrom
samoht:wasm-runtime

Conversation

@samoht

@samoht samoht commented Jul 7, 2026

Copy link
Copy Markdown
Member

wasm_of_ocaml resolves a library's C primitives from Wasm modules registered with (wasm_of_ocaml (wasm_files ...)), importing runtime helpers from the env namespace. cstruct.wat mirrors cstruct.js one to one: the three bigstring blits alias the runtime's caml_bigstring_blit_* exports, fill and compare loop over caml_ba_set_1 / caml_ba_get_1 + caml_int_compare, and check_alignment is always true. Without it a program that links cstruct cannot run as wasm (the blit primitives get dummy stubs that throw).

@samoht

samoht commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

/cc @vouillon - is this the right way to support wasm_of_ocaml ?

@vouillon

vouillon commented Jul 7, 2026

Copy link
Copy Markdown

Looks good. The functions already exist under another name. So you can also just reexport them.
https://github.com/ocaml-wasm/ocaml-cstruct/blob/cc77042b9ce9b27f5f4ed2d18a705e1143da4951/lib/cstruct.wat

@samoht
samoht force-pushed the wasm-runtime branch 4 times, most recently from 941ef74 to a530cf9 Compare July 10, 2026 05:46
samoht added 2 commits July 10, 2026 14:43
The wasm_of_ocaml (wasm_files ...) stanza needs dune language 3.17, so
bump dune-project and the dune lower bound in every package -- otherwise
opam may resolve an older dune (e.g. 2.3) that cannot parse the project.

Lang 3.17 also makes the dev profile treat warning 32 (unused value) as
an error, so drop the unused pp_cstruct in the fuzzer and use the
compile-test binding in the lwt ppx test.
wasm_of_ocaml already provides the bigstring blit, memset and memcmp
operations, so cstruct.wat re-exports them under cstruct's stub names;
only caml_check_alignment_bigstring is cstruct-specific.
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.

2 participants