Skip to content

Commit

Permalink
Run tests for wasm32-unknown-emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jan 6, 2024
1 parent 514ec24 commit 9ed0d90
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 26 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ jobs:
(cd tests/module && cargo build --release --features "${{ matrix.lua }}")
(cd tests/module/loader && cargo test --release --features "${{ matrix.lua }}")
test_wasm32_emscripten:
name: Test on wasm32-unknown-emscripten
runs-on: ubuntu-22.04
needs: build
strategy:
matrix:
lua: [lua54, lua53, lua52, lua51, luau]
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: wasm32-unknown-emscripten
- name: Install Emscripten
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends emscripten
- name: Run ${{ matrix.lua }} tests
run: |
cargo test --tests --features "${{ matrix.lua }},vendored"
cargo test --tests --features "${{ matrix.lua }},vendored,async,send,serialize,macros,parking_lot"
cargo test --tests --features "${{ matrix.lua }},vendored,async,serialize,macros,parking_lot,unstable"
rustfmt:
name: Rustfmt
runs-on: ubuntu-22.04
Expand Down
5 changes: 1 addition & 4 deletions examples/async_http_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::collections::HashMap;

use hyper::body::{Body as HyperBody, HttpBody as _};
Expand All @@ -22,7 +19,7 @@ impl UserData for BodyReader {
}
}

#[tokio::main]
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let lua = Lua::new();

Expand Down
3 changes: 0 additions & 3 deletions examples/async_http_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
Expand Down
3 changes: 0 additions & 3 deletions examples/async_tcp_server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use std::io;
use std::net::SocketAddr;
use std::rc::Rc;
Expand Down
2 changes: 0 additions & 2 deletions examples/repl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! This example shows a simple read-evaluate-print-loop (REPL).
#[cfg(target_arch = "wasm32")]
compile_error!("Not available for wasm");

use mlua::{Error, Lua, MultiValue};
use rustyline::DefaultEditor;
Expand Down
7 changes: 7 additions & 0 deletions tests/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ use mlua::{
UserData, UserDataMethods, Value,
};

#[cfg(not(target_arch = "wasm32"))]
async fn sleep_ms(ms: u64) {
tokio::time::sleep(Duration::from_millis(ms)).await;
}

#[cfg(target_arch = "wasm32")]
async fn sleep_ms(_ms: u64) {
// I was unable to make sleep() work in wasm32-emscripten target
tokio::task::yield_now().await;
}

#[tokio::test]
async fn test_async_function() -> Result<()> {
let lua = Lua::new();
Expand Down
9 changes: 6 additions & 3 deletions tests/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#![allow(unused_imports)]

use std::fs;
use std::io;

use mlua::{Lua, Result};

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_chunk_path() -> Result<()> {
let lua = Lua::new();

if cfg!(target_arch = "wasm32") {
// TODO: figure out why emscripten fails on file operations
// Also see https://github.com/rust-lang/rust/issues/119250
return Ok(());
}

let temp_dir = tempfile::tempdir().unwrap();
fs::write(
temp_dir.path().join("module.lua"),
Expand Down
6 changes: 6 additions & 0 deletions tests/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ fn test_require() -> Result<()> {
assert!(lua.globals().get::<_, Option<Value>>("require")?.is_none());
assert!(lua.globals().get::<_, Option<Value>>("package")?.is_none());

if cfg!(target_arch = "wasm32") {
// TODO: figure out why emscripten fails on file operations
// Also see https://github.com/rust-lang/rust/issues/119250
return Ok(());
}

lua = Lua::new();

let temp_dir = tempfile::tempdir().unwrap();
Expand Down
13 changes: 11 additions & 2 deletions tests/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ fn test_static_lua_coroutine() -> Result<()> {
async fn test_static_async() -> Result<()> {
let lua = Lua::new().into_static();

#[cfg(not(target_arch = "wasm32"))]
async fn sleep_ms(ms: u64) {
tokio::time::sleep(std::time::Duration::from_millis(ms)).await;
}

#[cfg(target_arch = "wasm32")]
async fn sleep_ms(_ms: u64) {
tokio::task::yield_now().await;
}

let timer =
lua.create_async_function(|_, (i, n, f): (u64, u64, mlua::Function)| async move {
tokio::task::spawn_local(async move {
let dur = std::time::Duration::from_millis(i);
for _ in 0..n {
tokio::task::spawn_local(f.call_async::<(), ()>(()));
tokio::time::sleep(dur).await;
sleep_ms(i).await;
}
});
Ok(())
Expand Down
30 changes: 21 additions & 9 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use std::iter::FromIterator;
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::string::String as StdString;
Expand Down Expand Up @@ -312,30 +313,29 @@ fn test_error() -> Result<()> {
globals.set("rust_error_function", rust_error_function)?;

let no_error = globals.get::<_, Function>("no_error")?;
let lua_error = globals.get::<_, Function>("lua_error")?;
let rust_error = globals.get::<_, Function>("rust_error")?;
let return_error = globals.get::<_, Function>("return_error")?;
let return_string_error = globals.get::<_, Function>("return_string_error")?;
let test_pcall = globals.get::<_, Function>("test_pcall")?;
let understand_recursion = globals.get::<_, Function>("understand_recursion")?;

assert!(no_error.call::<_, ()>(()).is_ok());

let lua_error = globals.get::<_, Function>("lua_error")?;
match lua_error.call::<_, ()>(()) {
Err(Error::RuntimeError(_)) => {}
Err(e) => panic!("error is not RuntimeError kind, got {:?}", e),
_ => panic!("error not returned"),
}

let rust_error = globals.get::<_, Function>("rust_error")?;
match rust_error.call::<_, ()>(()) {
Err(Error::CallbackError { .. }) => {}
Err(e) => panic!("error is not CallbackError kind, got {:?}", e),
_ => panic!("error not returned"),
}

let return_error = globals.get::<_, Function>("return_error")?;
match return_error.call::<_, Value>(()) {
Ok(Value::Error(_)) => {}
_ => panic!("Value::Error not returned"),
}

let return_string_error = globals.get::<_, Function>("return_string_error")?;
assert!(return_string_error.call::<_, Error>(()).is_ok());

match lua
Expand All @@ -358,9 +358,14 @@ fn test_error() -> Result<()> {
_ => panic!("error not returned"),
}

let test_pcall = globals.get::<_, Function>("test_pcall")?;
test_pcall.call::<_, ()>(())?;

assert!(understand_recursion.call::<_, ()>(()).is_err());
#[cfg(not(target_arch = "wasm32"))]
{
let understand_recursion = globals.get::<_, Function>("understand_recursion")?;
assert!(understand_recursion.call::<_, ()>(()).is_err());
}

Ok(())
}
Expand Down Expand Up @@ -947,6 +952,7 @@ fn test_application_data() -> Result<()> {
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_recursion() -> Result<()> {
let lua = Lua::new();

Expand All @@ -966,14 +972,16 @@ fn test_recursion() -> Result<()> {
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_too_many_returns() -> Result<()> {
let lua = Lua::new();
let f = lua.create_function(|_, ()| Ok(Variadic::from_iter(1..1000000)))?;
assert!(f.call::<_, Vec<u32>>(()).is_err());
assert!(f.call::<_, Variadic<u32>>(()).is_err());
Ok(())
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_too_many_arguments() -> Result<()> {
let lua = Lua::new();
lua.load("function test(...) end").exec()?;
Expand All @@ -988,6 +996,7 @@ fn test_too_many_arguments() -> Result<()> {

#[test]
#[cfg(not(feature = "luajit"))]
#[cfg(not(target_arch = "wasm32"))]
fn test_too_many_recursions() -> Result<()> {
let lua = Lua::new();

Expand All @@ -1001,6 +1010,7 @@ fn test_too_many_recursions() -> Result<()> {
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_too_many_binds() -> Result<()> {
let lua = Lua::new();
let globals = lua.globals();
Expand All @@ -1022,6 +1032,7 @@ fn test_too_many_binds() -> Result<()> {
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_ref_stack_exhaustion() {
match catch_unwind(AssertUnwindSafe(|| -> Result<()> {
let lua = Lua::new();
Expand Down Expand Up @@ -1351,6 +1362,7 @@ fn test_luajit_cdata() -> Result<()> {

#[test]
#[cfg(feature = "send")]
#[cfg(not(target_arch = "wasm32"))]
fn test_send() {
let lua = Lua::new();
std::thread::spawn(move || {
Expand Down

0 comments on commit 9ed0d90

Please sign in to comment.