Skip to content

Conversation

alexcrichton
Copy link
Contributor

This commit is an attempt to work towards mlua-rs/mlua#366 and support WASI targets in the mlua crate. This requires that the Lua source code can be compiled to WASI targets such as wasm32-wasip{1,2}. The C toolchain used for this is wasi-sdk and does not support all that Lua requires out-of-the-box. This necessitates some edits to Lua sources to exclude exposing unsupported functions to scripts.

This additionally updates CI to test the wasm32-wasip2 target inside of Wasmtime. This currently requires a dev release of Wasmtime (builds from main) but the Wasmtime 37.0.0 release next week will suffice once it's available due to WebAssembly exception-handling support. I've also dropped the Emscripten tests here as CI wasn't actually testing emscripten (it forgot --target) and the tests are otherwise broken if re-enabled. Installation of dependencies on CI has additionally been refactored to a shared "composite" action between the build/test workflows to avoid duplication between them.

Copy link
Contributor Author

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

I'm not entirely sure what the desired method of managing the patches/diff for lua are so I haven't done anything here other than include them. I'm happy to refactor things and/or place something in a README/diff files or such, and just wanted to have this as a starting point.

println!("cargo:rustc-link-search=native={}", self.lib_dir.display());
for lib in self.libs.iter() {
println!("cargo:rustc-link-lib=static={lib}");
println!("cargo:rustc-link-lib=static:-bundle={lib}");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll note that this was needed as otherwise rustc needs to know about a -L path to the C sysroot, where with -bundle that isn't required and rustc effectively delegates to Clang to find libraries like libsetjmp.a.

- name: Run ${{ matrix.lua }} tests
run: |
export CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the command mentioned in the PR description that's missing --target so this was actually testing a native build instead of Emscripten. The Emscripten failures look like this and I'm not knowledgable enough about Emscripten to know what the error there is or how to fix it.

Copy link
Member

Choose a reason for hiding this comment

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

indeed, good catch, I'll take a look to this

Copy link
Member

@khvzak khvzak left a comment

Choose a reason for hiding this comment

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

Looks good to me!

I'm thinking maybe more useful would be raising an exception in case when a library function (e.g. os.execute) is not supported by wasi rather than undefining it.
At the moment user scripts will get a nil exception (attempt to call a nil value error) when trying to use disabled functions, without any hints why.
Raising an error seems is more consistent with Lua behaviour (see this)

- name: Run ${{ matrix.lua }} tests
run: |
export CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }}
Copy link
Member

Choose a reason for hiding this comment

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

indeed, good catch, I'll take a look to this

This commit is an attempt to work towards mlua-rs/mlua#366 and support
WASI targets in the `mlua` crate. This requires that the Lua source code
can be compiled to WASI targets such as `wasm32-wasip{1,2}`. The C
toolchain used for this is [wasi-sdk] and does not support all that Lua
requires out-of-the-box. This necessitates some edits to Lua sources to
exclude exposing unsupported functions to scripts.

This additionally updates CI to test the `wasm32-wasip2` target inside
of Wasmtime. This currently requires a `dev` release of Wasmtime (builds
from `main`) but the Wasmtime 37.0.0 release next week will suffice once
it's available due to WebAssembly exception-handling support. I've also
dropped the Emscripten tests here as CI wasn't actually testing
emscripten (it forgot `--target`) and the tests are otherwise broken if
re-enabled. Installation of dependencies on CI has additionally been
refactored to a shared "composite" action between the build/test
workflows to avoid duplication between them.

[wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
@alexcrichton
Copy link
Contributor Author

Sounds reasonable to me yeah, updated the various #define'd away functions to raise an error instead

@khvzak khvzak merged commit a3518c2 into mlua-rs:main Sep 13, 2025
60 checks passed
@alexcrichton alexcrichton deleted the wasi branch September 13, 2025 17:36
alexcrichton added a commit to alexcrichton/mlua that referenced this pull request Oct 3, 2025
This is a follow-up from mlua-rs/lua-src-rs#13 which verifies/tests that
mlua/lua all work when compiled for a WASI target. While this doesn't
have formal documentation yet it also codifies in CI configuration how
to build for WASI and get tests passing (notably C compiler
configuration and some misc Rust flags).

This moves some `dev-dependencies` that don't compile for
`wasm32-wasip2` to a different section of the manifest. This
additionally annotates panicking tests with `#[cfg(not(panic =
"abort"))]` to skip those tests on WASI.

This does not test either the `send` or `async` feature at this time.
Testing `send` requires threads which WASI does not yet support, and
testing `async` requires more support in Tokio which is not currently
there yet.
alexcrichton added a commit to alexcrichton/mlua that referenced this pull request Oct 3, 2025
This is a follow-up from mlua-rs/lua-src-rs#13 which verifies/tests that
mlua/lua all work when compiled for a WASI target. While this doesn't
have formal documentation yet it also codifies in CI configuration how
to build for WASI and get tests passing (notably C compiler
configuration and some misc Rust flags).

This moves some `dev-dependencies` that don't compile for
`wasm32-wasip2` to a different section of the manifest. This
additionally annotates panicking tests with `#[cfg(not(panic =
"abort"))]` to skip those tests on WASI.

This does not test either the `send` or `async` feature at this time.
Testing `send` requires threads which WASI does not yet support, and
testing `async` requires more support in Tokio which is not currently
there yet.
khvzak pushed a commit to mlua-rs/mlua that referenced this pull request Oct 4, 2025
This is a follow-up from mlua-rs/lua-src-rs#13 which verifies/tests that
mlua/lua all work when compiled for a WASI target. While this doesn't
have formal documentation yet it also codifies in CI configuration how
to build for WASI and get tests passing (notably C compiler
configuration and some misc Rust flags).

This moves some `dev-dependencies` that don't compile for
`wasm32-wasip2` to a different section of the manifest. This
additionally annotates panicking tests with `#[cfg(not(panic =
"abort"))]` to skip those tests on WASI.

This does not test either the `send` or `async` feature at this time.
Testing `send` requires threads which WASI does not yet support, and
testing `async` requires more support in Tokio which is not currently
there yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants