From 962a2e50be3638b0dd64fb90ffe25208e4ca13ca Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 12 May 2024 19:59:24 +0530 Subject: [PATCH] refactor: migrate to new project structure (see https://github.com/lune-org/lune/commit/de71558c5df94e7cb466acb9e2810ebcfb3028a4) --- Cargo.lock | 30 +++++++++++++++++----- crates/lune-roblox/src/instance/terrain.rs | 8 +++--- crates/lune/Cargo.toml | 2 +- crates/lune/src/cli/run.rs | 6 ++--- crates/lune/src/rt/runtime.rs | 27 ++++++++++--------- crates/lune/src/standalone/mod.rs | 8 +++--- crates/lune/src/tests.rs | 4 +-- 7 files changed, 52 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 988c44a..fdfa079 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1485,7 +1485,7 @@ dependencies = [ "lune-std", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git)", "once_cell", "reqwest", "rustyline", @@ -1531,7 +1531,7 @@ dependencies = [ "lune-std-task", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "serde_json", "tokio", @@ -1581,7 +1581,7 @@ dependencies = [ "lune-std-serde", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest", "tokio", "tokio-tungstenite", @@ -1595,7 +1595,7 @@ dependencies = [ "directories", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "os_str_bytes", "pin-project", "tokio", @@ -1618,7 +1618,7 @@ dependencies = [ "lune-roblox", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "once_cell", "rbx_cookie", ] @@ -1646,7 +1646,7 @@ dependencies = [ "dialoguer", "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio", ] @@ -1656,7 +1656,7 @@ version = "0.1.0" dependencies = [ "lune-utils", "mlua", - "mlua-luau-scheduler", + "mlua-luau-scheduler 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio", ] @@ -1779,6 +1779,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "mlua-luau-scheduler" +version = "0.0.2" +source = "git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git#82c8b902e09a21a9befa4e037beadefbe2360b7f" +dependencies = [ + "async-executor", + "blocking", + "concurrent-queue", + "derive_more", + "event-listener 4.0.3", + "futures-lite", + "mlua", + "rustc-hash", + "tracing", +] + [[package]] name = "mlua-sys" version = "0.5.2" diff --git a/crates/lune-roblox/src/instance/terrain.rs b/crates/lune-roblox/src/instance/terrain.rs index 852f321..f39d59e 100644 --- a/crates/lune-roblox/src/instance/terrain.rs +++ b/crates/lune-roblox/src/instance/terrain.rs @@ -27,11 +27,13 @@ pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(methods: &mut M) } fn get_or_create_material_colors(instance: &Instance) -> MaterialColors { - if let Some(Variant::MaterialColors(material_colors)) = instance.get_property("MaterialColors") + if let Variant::MaterialColors(inner) = instance + .get_property("MaterialColors") + .unwrap_or(Variant::MaterialColors(MaterialColors::default())) { - material_colors + inner } else { - MaterialColors::default() + unreachable!() } } diff --git a/crates/lune/Cargo.toml b/crates/lune/Cargo.toml index 4e43302..1afb52a 100644 --- a/crates/lune/Cargo.toml +++ b/crates/lune/Cargo.toml @@ -57,7 +57,7 @@ workspace = true [dependencies] mlua = { version = "0.9.7", features = ["luau"] } -mlua-luau-scheduler = "0.0.2" +mlua-luau-scheduler = { git = "https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git" } anyhow = "1.0" console = "0.15" diff --git a/crates/lune/src/cli/run.rs b/crates/lune/src/cli/run.rs index 35e523e..fcce205 100644 --- a/crates/lune/src/cli/run.rs +++ b/crates/lune/src/cli/run.rs @@ -41,8 +41,8 @@ impl RunCommand { }; // Create a new lune object with all globals & run the script - let result = Runtime::new() - .with_args(self.script_args) + let mut runtime = Runtime::new().with_args(self.script_args); + let result = runtime .run(&script_display_name, strip_shebang(script_contents)) .await; Ok(match result { @@ -50,7 +50,7 @@ impl RunCommand { eprintln!("{err}"); ExitCode::FAILURE } - Ok(code) => code, + Ok((code, _)) => ExitCode::from(code), }) } } diff --git a/crates/lune/src/rt/runtime.rs b/crates/lune/src/rt/runtime.rs index a5501c6..0499b3b 100644 --- a/crates/lune/src/rt/runtime.rs +++ b/crates/lune/src/rt/runtime.rs @@ -1,7 +1,6 @@ #![allow(clippy::missing_panics_doc)] use std::{ - process::ExitCode, rc::Rc, sync::{ atomic::{AtomicBool, Ordering}, @@ -9,7 +8,7 @@ use std::{ }, }; -use mlua::Lua; +use mlua::{IntoLuaMulti as _, Lua, Value}; use mlua_luau_scheduler::Scheduler; use super::{RuntimeError, RuntimeResult}; @@ -82,7 +81,7 @@ impl Runtime { &mut self, script_name: impl AsRef, script_contents: impl AsRef<[u8]>, - ) -> RuntimeResult { + ) -> RuntimeResult<(u8, Vec)> { // Create a new scheduler for this run let sched = Scheduler::new(&self.lua); @@ -101,16 +100,20 @@ impl Runtime { .set_name(script_name.as_ref()); // Run it on our scheduler until it and any other spawned threads complete - sched.push_thread_back(main, ())?; + let main_thread_id = sched.push_thread_back(main, ())?; sched.run().await; - // Return the exit code - default to FAILURE if we got any errors - Ok(sched.get_exit_code().unwrap_or({ - if got_any_error.load(Ordering::SeqCst) { - ExitCode::FAILURE - } else { - ExitCode::SUCCESS - } - })) + let thread_res = match sched.get_thread_result(main_thread_id) { + Some(res) => res, + None => Value::Nil.into_lua_multi(&self.lua), + }? + .into_vec(); + + Ok(( + sched + .get_exit_code() + .unwrap_or(u8::from(got_any_error.load(Ordering::SeqCst))), + thread_res, + )) } } diff --git a/crates/lune/src/standalone/mod.rs b/crates/lune/src/standalone/mod.rs index fe58913..805eb95 100644 --- a/crates/lune/src/standalone/mod.rs +++ b/crates/lune/src/standalone/mod.rs @@ -29,16 +29,14 @@ pub async fn run(patched_bin: impl AsRef<[u8]>) -> Result { let args = env::args().skip(1).collect::>(); let meta = Metadata::from_bytes(patched_bin).expect("must be a standalone binary"); - let result = Runtime::new() - .with_args(args) - .run("STANDALONE", meta.bytecode) - .await; + let mut rt = Runtime::new().with_args(args); + let result = rt.run("STANDALONE", meta.bytecode).await; Ok(match result { Err(err) => { eprintln!("{err}"); ExitCode::FAILURE } - Ok(code) => code, + Ok((code, _)) => ExitCode::from(code), }) } diff --git a/crates/lune/src/tests.rs b/crates/lune/src/tests.rs index ae599da..7a5f5a7 100644 --- a/crates/lune/src/tests.rs +++ b/crates/lune/src/tests.rs @@ -42,8 +42,8 @@ macro_rules! create_tests { .trim_end_matches(".luau") .trim_end_matches(".lua") .to_string(); - let exit_code = lune.run(&script_name, &script).await?; - Ok(exit_code) + let (exit_code, _) = lune.run(&script_name, &script).await?; + Ok(ExitCode::from(exit_code)) } )* } }