Skip to content

Comments

Dynlink / toplevel support#2187

Open
vouillon wants to merge 17 commits intomasterfrom
dynlink
Open

Dynlink / toplevel support#2187
vouillon wants to merge 17 commits intomasterfrom
dynlink

Conversation

@vouillon
Copy link
Member

No description provided.

@vouillon vouillon added the wasm label Feb 18, 2026
@vouillon vouillon force-pushed the dynlink branch 10 times, most recently from 84eb0f3 to 69f1f24 Compare February 19, 2026 12:03
…l_refill

Allow overriding channel I/O with custom callbacks, matching the
JavaScript runtime behavior.
Add a virtual filesystem for embedding files (e.g. CMIs for the
toplevel) into the wasm output. Files specified via --file are
base64-encoded in the runtime arguments and decoded at startup.
@vouillon vouillon force-pushed the dynlink branch 7 times, most recently from 3795d67 to 80eab5a Compare February 19, 2026 21:32
--dynlink links all primitives and compilation units, skipping dead code elimination.
Add a dynlink library (wasm_of_ocaml-compiler.dynlink) that can
dynamically load pre-compiled .wasmo/.wasma files at runtime. The
wasmo/wasma ZIP archives now include a link_order entry to ensure
compilation units are initialized in the correct order.
Add Generate.compile and Generate.from_string to compile OCaml
bytecode to wasm binary in memory, without writing to disk. Add
Wasm_output.to_string for in-memory wasm serialization. This enables
runtime compilation of plugins from bytecode to loadable .wasmo files.
caml_global_data is a $block whose index 0 is the tag. Generated
code accesses it via Field(gdata, i) which compiles to
array.get gdata (i+1), so the runtime must store at i+1 as well.
Adjust caml_register_global, caml_realloc_global, and all
predefined exception indices in fail.wat accordingly.
Implement Dynlink.loadfile for wasm_of_ocaml. Dynamically loaded
modules are compiled from bytecode to Wasm and instantiated at
runtime. Cross-module references go through caml_global_data by
index, while statically linked modules continue to use named Wasm
globals.
--toplevel compiles for toplevel/REPL use, embedding CMI files
and implying --linkall. --no-cmis disables CMI embedding.
--export restricts the set of exported units and their CRCs.
When building a .wasma library with --linkall, set force_link on
all compilation units so they are included when the library is
linked into an executable.
…set_raw_field

The toplevel accesses a closure's code pointer field for tracing purposes.
Since Wasm closures are not represented as blocks, the cast would trap.
Add a type check and return a dummy value for non-block inputs.
…efaults

These primitives are needed by the toplevel/dynlink but were missing
from the Wasm runtime. caml_exn_with_js_backtrace returns the exception
unchanged (backtrace attachment is not supported). caml_xdg_defaults
returns an empty list.
Move JsooTop, JsooTopPpx, and JsooTopError into a new
js_of_ocaml-toplevel.common sublibrary that depends on
js_of_ocaml-compiler (without .dynlink), making it usable by both
JS and Wasm backends. The existing js_of_ocaml-toplevel becomes a
thin wrapper that re-exports the common code and pulls in the JS
dynlink dependency for backward compatibility.
Implement all Graphics primitives for the Wasm backend by reusing the existing JS canvas
code. JS helper functions (suffixed _for_wasm) take plain JS values and perform the canvas
operations; WAT wrappers handle OCaml value conversion, state checking, and exception
raising.
Refactor the Lwt toplevel example to support both JS and Wasm backends.
Extract the shared UI logic (history, syntax highlighting, examples,
pseudo-fs, toplevel setup) into a lwt_toplevel_lib library. The JS and
Wasm entry points are now thin wrappers that call Toplevel_main.run.

The HTML page auto-detects Wasm support (via WebAssembly.JSTag) and
loads the appropriate script; a #js fragment parameter forces the JS
backend.
@hhugo
Copy link
Member

hhugo commented Feb 22, 2026

Are there tests or examples that can run in wasm now that we support virtual filesystem ?

@vouillon
Copy link
Member Author

Are there tests or examples that can run in wasm now that we support virtual filesystem ?

compiler/tests-toplevel, and toplevel/examples/lwt_toplevel, for instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants