Skip to content

WASI runtime#1831

Open
vouillon wants to merge 23 commits into
masterfrom
wasi
Open

WASI runtime#1831
vouillon wants to merge 23 commits into
masterfrom
wasi

Conversation

@vouillon
Copy link
Copy Markdown
Member

@vouillon vouillon commented Feb 6, 2025

You can produce a WASI binary by running wasm_of_ocaml with the --enable wasi flag:

wasm_of_ocaml --enable wasi foo.byte -o foo.js

This produces some Wasm code that can be directly executed by the Wizard engine:

wizeng.x86-64-linux -ext:stack-switching -ext:legacy-eh foo.assets/code.wasm

wasmtime does not support the legacy exception handling instructions. You can produce a binary that will work with wasmtime as well by adding the --enable exnref flag:

wasm_of_ocaml --enable wasi --enable exnref foo.byte -o foo.js

You can then execute it with the following command:

wasmtime -W=all-proposals=y foo.assets/code.wasm

Note that we keep the same output convention, at least for now: a JavaScript file foo.js and a directory foo.assets containing the Wasm code. The JavaScript file can be use to run the WASI binary with node.

Blocked by:

@vouillon vouillon force-pushed the wasi branch 5 times, most recently from 28a41a0 to 7a4d3ff Compare February 13, 2025 16:42
@vouillon vouillon added the wasm label Feb 14, 2025
@vouillon vouillon force-pushed the wasi branch 12 times, most recently from c1fc50d to c9f7566 Compare February 20, 2025 12:55
@vouillon vouillon force-pushed the wasi branch 3 times, most recently from 85a9891 to 25a5e68 Compare February 26, 2025 17:14
@vouillon vouillon force-pushed the wasi branch 2 times, most recently from 391b19a to f3efbd6 Compare February 28, 2025 13:18
@vouillon vouillon force-pushed the wasi branch 7 times, most recently from 31610af to 2356554 Compare April 10, 2025 17:04
Comment thread compiler/bin-wasm_of_ocaml/link_wasm.ml Outdated
with
| Some "native" -> `Native
| Some "cps" -> `Cps
| _ -> `Jspi);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waybe we should not silently ignore unknown values

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is similar diff in #2189, is there a lot of intersection between the two ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is on top of #2189.

vouillon added 23 commits May 22, 2026 19:12
Use a compact encoding for the [--build-config] output: a key at its
default is omitted; all other keys still emit [name=value]. Restructure
[set_values] to iterate over the known keys rather than the input
entries, so that an omitted bool is reset to its default (instead of
silently keeping whatever value the caller left it at) and a missing
enum is reported as an error.

For the typical wasm builds this reduces e.g.
  effects=jspi+toplevel=false+use-js-string=true  (45 chars)
to
  effects=jspi                                    (12 chars),
which keeps the build-config directory that dune derives from this
output well within Windows' MAX_PATH limit.
Dune 3.23 forces sandboxing on user rules, so gen.exe runs from
_build/.sandbox/<hash>/default/<dir> instead of the project tree.
The previous prefix walker (stop when parent ends with _build) then
yielded <hash>/default/<dir>/, which both shows up in the generated
;; comment and shifts the Hashtbl.hash-based name suffix, making the
runtest diff against the checked-in dune.inc fail.

Take the project-relative directory as argv.(1) (replacing the
previously unused library-name argument) so the prefix is stable
regardless of where dune runs the action.
The current release of ocaml-dune-lint is not compatible with dune 3.23.
Under dune 3.23 sandboxing, rules that run a *.bc.wasm.js file also
need to depend on the companion *.bc.wasm.assets/ directory so that
the .wasm files are copied into the sandbox.
Under dune 3.23 sandboxing, rules invoking js_of_ocaml or wasm_of_ocaml
with --toplevel need their .cmi files brought into the sandbox.

For locally built cmis, declare them as deps. For library cmis, add
a cmi_include_dirs.txt rule (mirroring the existing one for toplevel.bc)
that uses ocamlfind to produce -I flags, and pass the result via
read-strings.
Under dune 3.23 sandboxing, wc.ml needs to be brought into the
sandbox. Inline the dep with %{dep:wc.ml} on the command line.
It uses `js_of_ocaml --build-config` to manage config details.
oxcaml-dune-patches pins dune <= 3.21, but the project requires
dune >= 3.23. opam re-checks dependency constraints on every install,
so a one-shot --ignore-constraints-on=dune flag isn't enough.

Re-pin the package with the dune version constraint stripped, so
subsequent installs let dune resolve to 3.23.x normally.
Wasmtime only supports these instructions and not the legacy ones. So,
emit them when WASI support is requested, since Wasmtime is the
primary target for WASI binaries.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants