|
| 1 | +# Compiling for WebAssembly |
| 2 | + |
| 3 | +Lua 5.1-5.5 and Luau can be compiled for WebAssembly. The WASI |
| 4 | +builds use [WASI SDK](https://github.com/WebAssembly/wasi-sdk) to compile and link |
| 5 | +the C/C++ code, and [Wasmtime](https://github.com/bytecodealliance/wasmtime) to run it. |
| 6 | + |
| 7 | +| Rust target | Rust toolchain | Output | |
| 8 | +| --- | --- | --- | |
| 9 | +| `wasm32-wasip1` | stable | WASI Preview 1 module | |
| 10 | +| `wasm32-wasip2` | stable | WASI Preview 2 component | |
| 11 | +| `wasm32-wasip3` | nightly | WASI Preview 3 component | |
| 12 | +| `wasm32-unknown-unknown` | stable | Module or component, linked with a WASI Preview 1 or 2 C runtime | |
| 13 | + |
| 14 | +LuaJIT and Luau's JIT backend are not supported. |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +Run the commands below from the mlua repository root. |
| 19 | + |
| 20 | +Install WASI SDK, for example on Apple Silicon: |
| 21 | + |
| 22 | +```bash |
| 23 | +WASI_SDK_VERSION=34 |
| 24 | +WASI_SDK_ARCHIVE="wasi-sdk-${WASI_SDK_VERSION}.0-arm64-macos" |
| 25 | +mkdir -p "$HOME/.local" |
| 26 | +curl -fL "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION}/${WASI_SDK_ARCHIVE}.tar.gz" \ |
| 27 | + | tar -xz -C "$HOME/.local" |
| 28 | +export WASI_SDK_PATH="$HOME/.local/$WASI_SDK_ARCHIVE" |
| 29 | +``` |
| 30 | + |
| 31 | +Install Wasmtime: |
| 32 | + |
| 33 | +```bash |
| 34 | +brew install wasmtime |
| 35 | +``` |
| 36 | + |
| 37 | +Install the Rust target(s): |
| 38 | + |
| 39 | +```bash |
| 40 | +rustup target add wasm32-wasip1 wasm32-wasip2 wasm32-unknown-unknown |
| 41 | +``` |
| 42 | + |
| 43 | +The [setup script](../tests/wasm/setup.sh) selects the SDK compilers, |
| 44 | +libraries, linker flags and Cargo runner. Source it in the same shell as the |
| 45 | +Cargo commands. It also enables Wasm exceptions, which the Lua runtimes need |
| 46 | +for error handling. |
| 47 | + |
| 48 | +## WASI Preview 1 and 2 |
| 49 | + |
| 50 | +To compile and run the basic tests with Lua 5.5 on Preview 2: |
| 51 | + |
| 52 | +```bash |
| 53 | +source tests/wasm/setup.sh wasm32-wasip2 |
| 54 | +cargo test --manifest-path tests/wasm/Cargo.toml \ |
| 55 | + --target wasm32-wasip2 --lib --features lua55 |
| 56 | +``` |
| 57 | + |
| 58 | +The test crate accepts `lua55`, `lua54`, `lua53`, `lua52`, `lua51` or `luau`. |
| 59 | +It enables vendored Lua automatically. |
| 60 | + |
| 61 | +For your own application, enable `vendored` for Lua. |
| 62 | +Source the setup script using the path to your mlua checkout, |
| 63 | +then build or run your application: |
| 64 | + |
| 65 | +```bash |
| 66 | +source /path/to/mlua/tests/wasm/setup.sh wasm32-wasip2 |
| 67 | +cargo build --target wasm32-wasip2 |
| 68 | +cargo run --target wasm32-wasip2 |
| 69 | +``` |
| 70 | + |
| 71 | +Cargo uses Wasmtime as its runner. To run the output directly: |
| 72 | + |
| 73 | +```bash |
| 74 | +wasmtime -W exceptions target/wasm32-wasip2/debug/your-app.wasm |
| 75 | +``` |
| 76 | + |
| 77 | +## wasm32-unknown-unknown |
| 78 | + |
| 79 | +This build uses Rust's `wasm32-unknown-unknown` target with a WASI C runtime. |
| 80 | +The host must provide the matching WASI imports and support Wasm exceptions. |
| 81 | +Loading the output in a browser requires those facilities too. |
| 82 | + |
| 83 | +The [test crate](../tests/wasm/src/lib.rs) is a working example. Run it with |
| 84 | +either C runtime: |
| 85 | + |
| 86 | +```bash |
| 87 | +./tests/wasm/run.sh lua55 wasm32-wasip1 |
| 88 | +./tests/wasm/run.sh lua55 wasm32-wasip2 |
| 89 | +``` |
| 90 | + |
| 91 | +Append `--release` to build and run with optimizations, or `--debug` for the default debug build. |
| 92 | + |
| 93 | +With Preview 1, it builds a `cdylib` and invokes the exported `run_tests` function. |
| 94 | +With Preview 2, it builds a Rust `staticlib`, then links it with a small |
| 95 | +[C entry point](../tests/wasm/main.c) into a command component. |
| 96 | + |
| 97 | +To adapt this to your project: |
| 98 | + |
| 99 | +1. Use the `allocator` module from the test crate as your global allocator. Rust |
| 100 | + and WASI libc must share an allocator so they do not independently manage the |
| 101 | + same linear memory. |
| 102 | +2. Export your entry point as an `extern "C"` function with `#[unsafe(no_mangle)]`. |
| 103 | +3. Follow [run.sh](../tests/wasm/run.sh) for the build and link commands, replacing |
| 104 | + the crate path, output name and entry point with your own. Its call to |
| 105 | + `setup.sh` selects the C runtime separately from the Rust target. |
0 commit comments