Browser WASM API

The browser wrapper crate is crates/rukalang_wasm.

It currently exposes these wasm-bindgen APIs:

  • compile_for_browser_json(source_name, source_text)
  • analyze_for_browser_json(source_name, source_text)
  • lex_for_browser_json(source_text)

Success payload fields:

  • ast_graph
  • hir_graph
  • mir_graph
  • rust_source
  • wat_source
  • wasm_bytes (optional u8 array; present only when binary emission succeeds)
  • wasm_diagnostics (non-fatal backend diagnostics)

Build prerequisite for direct WASM emission:

  • run ./scripts/build-runtime-wasm.sh before Rust or web WASM builds/tests that consume browser artifacts

Error payload shape:

  • object with diagnostics array
  • each diagnostic includes phase and message
  • syntax diagnostics may include line and column
  • phase values currently include module, syntax, meta, check, mir_lower, and codegen

compile_for_browser_json behavior notes:

  • Rust/AST/HIR/MIR artifacts are emitted when frontend + MIR + Rust codegen pass.
  • AST/HIR/MIR graph payloads are browser-friendly Cytoscape data rather than DOT text.
  • WASM backend diagnostics are reported in wasm_diagnostics without failing the whole compile payload.
  • wasm_bytes is omitted when the current source uses unsupported WASM backend features.
  • wat_source is generated from emitted wasm_bytes via wasmprinter, with synthesized names enabled to improve readability; when WASM emission fails, wat_source is empty.
  • Emitted wasm_bytes are validated with wasmparser before they are returned.
  • Emitted browser WASM exports run_main, which calls runtime assert_no_leaks by default after invoking user main.

analyze_for_browser_json payload shape:

  • ok: boolean (true when no diagnostics were produced)
  • diagnostics: same diagnostic entry shape as compile errors
  • highlight_spans: lexical/semantic token spans used by the editor

Validate the wrapper crate:

cargo test --manifest-path crates/rukalang_wasm/Cargo.toml