diff --git a/Cargo.toml b/Cargo.toml index 0632e0d..9c86773 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,6 @@ path = "lib/lib.rs" basic-sleep = "examples/basic_sleep.rs" basic-spawn = "examples/basic_spawn.rs" callbacks = "examples/callbacks.rs" +captures = "examples/captures.rs" lots-of-threads = "examples/lots_of_threads.rs" -main = "examples/main.rs" scheduler-ordering = "examples/scheduler_ordering.rs" diff --git a/examples/main.rs b/examples/captures.rs similarity index 97% rename from examples/main.rs rename to examples/captures.rs index 398546e..423fa43 100644 --- a/examples/main.rs +++ b/examples/captures.rs @@ -9,13 +9,13 @@ use smol_mlua::{ Callbacks, IntoLuaThread, Runtime, }; -const MAIN_SCRIPT: &str = include_str!("./lua/main.luau"); +const MAIN_SCRIPT: &str = include_str!("./lua/captures.luau"); pub fn main() -> LuaResult<()> { // Set up persistent lua environment let lua = Lua::new(); lua.globals().set( - "wait", + "sleep", lua.create_async_function(|_, duration: Option| async move { let duration = duration.unwrap_or_default().max(1.0 / 250.0); let before = Instant::now(); diff --git a/examples/lua/basic_spawn.luau b/examples/lua/basic_spawn.luau index 9f7a98b..f67726e 100644 --- a/examples/lua/basic_spawn.luau +++ b/examples/lua/basic_spawn.luau @@ -1,9 +1,17 @@ --!nocheck --!nolint UnknownGlobal -local file = readFile("Cargo.toml") -if file ~= nil then - print("Cargo.toml found!") - print("Contents:") - print(file) +local _, err = pcall(function() + local file = readFile("Cargo.toml") + if file ~= nil then + print("Cargo.toml found!") + print("Contents:") + print(file) + else + print("Cargo.toml not found!") + end +end) + +if err ~= nil then + print("Error while reading file: " .. err) end diff --git a/examples/lua/main.luau b/examples/lua/captures.luau similarity index 97% rename from examples/lua/main.luau rename to examples/lua/captures.luau index f598983..0459703 100644 --- a/examples/lua/main.luau +++ b/examples/lua/captures.luau @@ -13,7 +13,7 @@ local start = os.clock() local counter = 0 for j = 1, 10_000 do __runtime__spawn(function() - wait() + sleep() counter += 1 if counter == 10_000 then local elapsed = os.clock() - start diff --git a/examples/lua/lots_of_threads.luau b/examples/lua/lots_of_threads.luau index de8cc87..3144f15 100644 --- a/examples/lua/lots_of_threads.luau +++ b/examples/lua/lots_of_threads.luau @@ -1,7 +1,7 @@ --!nocheck --!nolint UnknownGlobal -local NUM_BATCHES = 100 +local NUM_BATCHES = 10 local NUM_THREADS = 10_000 print(`Spawning {NUM_BATCHES * NUM_THREADS} threads split into {NUM_BATCHES} batches\n`) diff --git a/examples/lua/scheduler_ordering.luau b/examples/lua/scheduler_ordering.luau index 437828a..945cb5c 100644 --- a/examples/lua/scheduler_ordering.luau +++ b/examples/lua/scheduler_ordering.luau @@ -19,7 +19,7 @@ end) -- which will resume their calling thread with their result automatically __runtime__spawn(function() print(3) - wait(1) + sleep(1) print(6) end) diff --git a/examples/scheduler_ordering.rs b/examples/scheduler_ordering.rs index 9a76a3a..af73257 100644 --- a/examples/scheduler_ordering.rs +++ b/examples/scheduler_ordering.rs @@ -12,7 +12,7 @@ pub fn main() -> LuaResult<()> { // Set up persistent lua environment let lua = Lua::new(); lua.globals().set( - "wait", + "sleep", lua.create_async_function(|_, duration: Option| async move { let duration = duration.unwrap_or_default().max(1.0 / 250.0); let before = Instant::now(); diff --git a/lib/lib.rs b/lib/lib.rs index 5aa95b6..29c906c 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -10,3 +10,6 @@ pub use smol; pub use callbacks::Callbacks; pub use runtime::Runtime; pub use traits::{IntoLuaThread, LuaExecutorExt}; + +#[cfg(test)] +mod tests; diff --git a/lib/tests.rs b/lib/tests.rs new file mode 100644 index 0000000..8d7de7d --- /dev/null +++ b/lib/tests.rs @@ -0,0 +1,52 @@ +use std::time::{Duration, Instant}; + +use mlua::prelude::*; + +use smol::{fs::read_to_string, Timer}; + +use crate::Runtime; + +macro_rules! create_tests { + ($($name:ident: $value:expr,)*) => { $( + #[test] + fn $name() -> LuaResult<()> { + // Read the test script + let script = std::fs::read_to_string(concat!($value, ".luau"))?; + + // Set up persistent lua environment + let lua = Lua::new(); + lua.globals().set( + "sleep", + lua.create_async_function(|_, duration: Option| async move { + let duration = duration.unwrap_or_default().max(1.0 / 250.0); + let before = Instant::now(); + let after = Timer::after(Duration::from_secs_f64(duration)).await; + Ok((after - before).as_secs_f64()) + })? + )?; + lua.globals().set( + "readFile", + lua.create_async_function(|_, path: String| async move { + Ok(read_to_string(path).await?) + })? + )?; + + // Load the main script into a runtime and run it until completion + let rt = Runtime::new(&lua)?; + let main = lua.load(script); + rt.push_thread(main, ()); + rt.run_blocking(); + + Ok(()) + } + )* } +} + +create_tests! { + basic_sleep: "examples/lua/basic_sleep", + basic_spawn: "examples/lua/basic_spawn", + callbacks: "examples/lua/callbacks", + captures: "examples/lua/captures", + lots_of_threads: "examples/lua/lots_of_threads", + scheduler_ordering: "examples/lua/scheduler_ordering", +}