Skip to content

Latest commit

 

History

History

wasm

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Sparkle WASM packaging for xeus-lean

This directory is the publish-side of the xeus-lean EXTRA_WASM_DIRS extension contract. Running build-wasm.sh produces a staging directory that xlean's CMake reads at build time to whole-archive Sparkle's externs and oleans into xlean.wasm.

What's in the staging dir

<staging>/
├── Sparkle.olean                       # umbrella olean
├── Sparkle/...                         # per-module olean tree
├── lib/
│   ├── libsparkle_wasm.a               # whole-archived into xlean.wasm
│   └── sparkle_exports.txt             # one symbol per line for emcc
├── xeus-lean-extra.json                # manifest xlean's CMake reads
└── .xeus-auto-imports                  # pre-import Sparkle on REPL cell 1

libsparkle_wasm.a bundles:

  • c_src/sparkle_barrier.c — pure C, WASM-safe. Implements the Signal evaluator's memoization barriers (sparkle_cache_get / sparkle_eval_at).
  • c_src/sparkle_jit_wasm_stub.c — replaces sparkle_jit.c for WASM. The native JIT loads compiled .sos via dlopen, which the xeus-lean WASM sandbox doesn't expose. The stub satisfies every @[extern "sparkle_jit_*"] at link time and throws a clear IO.userError at run time so notebook users see a helpful message instead of an opaque "missing symbol" failure.
  • Lake-generated .c wrappers under .lake/build/ir/Sparkle/**.c. Lean's interpreter resolves the boxed entry points (lp_…___boxed) via dlsym; the wrappers must live in the same archive as the C externs.

What's NOT in here

  • IP.* (RV32, BitNet, YOLOv8, Video, …). Out-of-scope for the tutorials and would inflate the JupyterLite first-boot download by ~50 MB. If you want them, copy this script and add lake build IP.<lib> plus copy steps for the per-IP olean trees.
  • Tools.SVParser. Useful for write-your-own-SoC work but tutorials don't import it.
  • Anything from Examples/ or Tests/.

Running

You need an emscripten-enabled shell — the easiest is the one xeus-lean's docs CI uses:

# Inside an xeus-lean checkout (peer to sparkle/):
cd /path/to/xeus-lean
pixi run -e wasm-build bash /path/to/sparkle/scripts/wasm/build-wasm.sh \
  /tmp/sparkle-staging

# Then feed the staging dir to xlean's WASM build:
pixi run -e wasm-build emcmake cmake -S . -B wasm-build \
  -DEXTRA_WASM_DIRS="/tmp/sparkle-staging"
pixi run -e wasm-build emmake make -C wasm-build -j$(nproc) xlean

The script bails out early if emcc, emar, or lake aren't on PATH, so you don't waste a 30-minute WASM relink just to find out one tool was missing.

When Sparkle's extern surface changes

If you add or remove an @[extern "sparkle_*"] declaration anywhere under Sparkle/:

  • The Lake-generated wrappers under .lake/build/ir/Sparkle/**.c pick up the change automatically — build-wasm.sh re-archives whatever is on disk.
  • The hand-written exports list in sparkle_exports.txt is regenerated by scanning LEAN_EXPORT lines in those .c files, so it tracks the C side automatically too.
  • For new hand-written externs (something other than barrier or JIT), add the implementation under c_src/ and add its .o to step 2 of build-wasm.sh.

The native JIT externs (everything in c_src/sparkle_jit.c) are WASM-incompatible by construction; if you ever add a new one, also add a stub return in c_src/sparkle_jit_wasm_stub.c so the WASM link doesn't break.

Why this lives here and not in xeus-lean

xeus-lean PR #13 moved every third-party Lean library out of the kernel repo. xeus-lean now owns:

  • the kernel binary (xlean.wasm/js),
  • the JupyterLite glue,
  • a vendor-agnostic mechanism (EXTRA_WASM_DIRS + xeus-lean-extra.json)

and nothing else about any particular downstream library. This directory is Sparkle's matching plug for that mechanism.