From 390937d9189490b2acdfdf60a58ed73c6e4ce38b Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Thu, 11 Apr 2024 18:11:52 +0530 Subject: [PATCH 01/10] feat(Runtime): make `Runtime::run` return lua return values `Runtime::run` now returns a tuple of both the `ExitCode` (denoting whether the lua thread generally succeeded or not) and also the values returned by it. --- src/cli/run.rs | 9 ++++----- src/lune/mod.rs | 29 ++++++++++++++++++----------- src/standalone/mod.rs | 8 +++----- src/tests.rs | 2 +- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/cli/run.rs b/src/cli/run.rs index 35e523e6..5ce8ab7d 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -7,9 +7,8 @@ use tokio::{ io::{stdin, AsyncReadExt as _}, }; -use lune::Runtime; - use super::utils::files::{discover_script_path_including_lune_dirs, strip_shebang}; +use lune::Runtime; /// Run a script #[derive(Debug, Clone, Parser)] @@ -41,8 +40,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 +49,7 @@ impl RunCommand { eprintln!("{err}"); ExitCode::FAILURE } - Ok(code) => code, + Ok((code, _)) => code, }) } } diff --git a/src/lune/mod.rs b/src/lune/mod.rs index ce676bfe..0f2ad028 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -7,7 +7,7 @@ use std::{ }, }; -use mlua::Lua; +use mlua::{Lua, Value}; use mlua_luau_scheduler::Scheduler; mod builtins; @@ -64,7 +64,7 @@ impl Runtime { &mut self, script_name: impl AsRef, script_contents: impl AsRef<[u8]>, - ) -> Result { + ) -> Result<(ExitCode, Vec), RuntimeError> { // Create a new scheduler for this run let sched = Scheduler::new(&self.lua); @@ -83,16 +83,23 @@ 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 = sched + .get_thread_result(main_thread_id) + .unwrap() + .unwrap() + .into_vec(); + Ok(( + sched.get_exit_code().unwrap_or({ + if got_any_error.load(Ordering::SeqCst) { + ExitCode::FAILURE + } else { + ExitCode::SUCCESS + } + }), + thread_res, + )) } } diff --git a/src/standalone/mod.rs b/src/standalone/mod.rs index fe589130..03555b57 100644 --- a/src/standalone/mod.rs +++ b/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 runtime = Runtime::new().with_args(args); + let result = runtime.run("STANDALONE", meta.bytecode).await; Ok(match result { Err(err) => { eprintln!("{err}"); ExitCode::FAILURE } - Ok(code) => code, + Ok((code, _)) => code, }) } diff --git a/src/tests.rs b/src/tests.rs index fad6026b..ad9a2ebf 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -31,7 +31,7 @@ macro_rules! create_tests { .trim_end_matches(".luau") .trim_end_matches(".lua") .to_string(); - let exit_code = lune.run(&script_name, &script).await?; + let (exit_code, _) = lune.run(&script_name, &script).await?; Ok(exit_code) } )* } From 9cb1ba43d085c4eea234b3290b18ddfd7d302419 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Thu, 11 Apr 2024 18:18:10 +0530 Subject: [PATCH 02/10] fix(Runtime): remove unwraps and handle None case This commit removes the chained unwraps introduced previously with a match statement which handles the case when a lua thread may return no values. --- src/lune/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lune/mod.rs b/src/lune/mod.rs index 0f2ad028..b54c5841 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -7,7 +7,7 @@ use std::{ }, }; -use mlua::{Lua, Value}; +use mlua::{IntoLuaMulti, Lua, Value}; use mlua_luau_scheduler::Scheduler; mod builtins; @@ -86,11 +86,11 @@ impl Runtime { let main_thread_id = sched.push_thread_back(main, ())?; sched.run().await; - let thread_res = sched - .get_thread_result(main_thread_id) - .unwrap() - .unwrap() - .into_vec(); + 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({ if got_any_error.load(Ordering::SeqCst) { From 902c89acabfd42099c0dbf4ee02a1e3a50dc0d2d Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 12 Apr 2024 15:36:14 +0530 Subject: [PATCH 03/10] feat(Runtime): handle updated i32 returns for `Scheduler::get_exit_code` --- Cargo.lock | 3 +-- Cargo.toml | 2 +- src/lune/mod.rs | 9 +++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8159d41b..45561f3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1489,8 +1489,7 @@ dependencies = [ [[package]] name = "mlua-luau-scheduler" version = "0.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a13eabdbc57fa38cf0b604d98ce3431573c79a964aac56e09c16c240d36cb1bf" +source = "git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git#22d15b36499db93c46ec381a562a3d1f440d6d02" dependencies = [ "async-executor", "blocking", diff --git a/Cargo.toml b/Cargo.toml index 2c79be68..0c7b4118 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] } tokio = { version = "1.24", features = ["full", "tracing"] } os_str_bytes = { version = "7.0", features = ["conversions"] } -mlua-luau-scheduler = { version = "0.0.2" } +mlua-luau-scheduler = { git = "https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git" } mlua = { version = "0.9.6", features = [ "luau", "luau-jit", diff --git a/src/lune/mod.rs b/src/lune/mod.rs index b54c5841..2d0821e8 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -91,14 +91,15 @@ impl Runtime { None => Value::Nil.into_lua_multi(&self.lua), }? .into_vec(); + Ok(( - sched.get_exit_code().unwrap_or({ + ExitCode::from(sched.get_exit_code().unwrap_or({ if got_any_error.load(Ordering::SeqCst) { - ExitCode::FAILURE + 1 } else { - ExitCode::SUCCESS + 0 } - }), + }) as u8), thread_res, )) } From 13198e18517b627c150aa583be7a05e0e9364e7a Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 12 Apr 2024 16:05:24 +0530 Subject: [PATCH 04/10] feat(Runtime): return raw exit code as `i32` instead of `ExitCode` --- src/lune/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lune/mod.rs b/src/lune/mod.rs index 2d0821e8..51ac6710 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -64,7 +64,7 @@ impl Runtime { &mut self, script_name: impl AsRef, script_contents: impl AsRef<[u8]>, - ) -> Result<(ExitCode, Vec), RuntimeError> { + ) -> Result<(i32, Vec), RuntimeError> { // Create a new scheduler for this run let sched = Scheduler::new(&self.lua); @@ -93,13 +93,13 @@ impl Runtime { .into_vec(); Ok(( - ExitCode::from(sched.get_exit_code().unwrap_or({ + sched.get_exit_code().unwrap_or({ if got_any_error.load(Ordering::SeqCst) { 1 } else { 0 } - }) as u8), + }), thread_res, )) } From e7cbd93b401bcf6e00d7bf40d1526adabc13dc2c Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Fri, 12 Apr 2024 21:47:47 +0530 Subject: [PATCH 05/10] fix(Runtime): lune exit codes are `u8` See https://github.com/0x5eal/mlua-luau-scheduler-exitstatus/commit/82c8b902e09a21a9befa4e037beadefbe2360b7f. --- Cargo.lock | 34 +++++++++++++++++----------------- src/cli/run.rs | 2 +- src/lune/mod.rs | 3 +-- src/standalone/mod.rs | 2 +- src/tests.rs | 2 +- 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45561f3b..39ce2128 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,9 +106,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.81" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" +checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" [[package]] name = "arrayref" @@ -188,9 +188,9 @@ checksum = "fbb36e985947064623dbd357f727af08ffd077f93d696782f3c56365fa2e2799" [[package]] name = "async-trait" -version = "0.1.79" +version = "0.1.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" +checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", @@ -675,9 +675,9 @@ checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] name = "encoding_rs" -version = "0.8.33" +version = "0.8.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" dependencies = [ "cfg-if", ] @@ -1489,7 +1489,7 @@ dependencies = [ [[package]] name = "mlua-luau-scheduler" version = "0.0.2" -source = "git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git#22d15b36499db93c46ec381a562a3d1f440d6d02" +source = "git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git#82c8b902e09a21a9befa4e037beadefbe2360b7f" dependencies = [ "async-executor", "blocking", @@ -1737,7 +1737,7 @@ dependencies = [ "line-wrap", "quick-xml", "serde", - "time 0.3.34", + "time 0.3.36", ] [[package]] @@ -1797,9 +1797,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] @@ -2627,9 +2627,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.34" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -2637,7 +2637,7 @@ dependencies = [ "powerfmt", "serde", "time-core", - "time-macros 0.2.17", + "time-macros 0.2.18", ] [[package]] @@ -2658,9 +2658,9 @@ dependencies = [ [[package]] name = "time-macros" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", @@ -3326,9 +3326,9 @@ checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" [[package]] name = "winnow" -version = "0.6.5" +version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +checksum = "f0c976aaaa0e1f90dbb21e9587cdaf1d9679a1cde8875c0d6bd83ab96a208352" dependencies = [ "memchr", ] diff --git a/src/cli/run.rs b/src/cli/run.rs index 5ce8ab7d..41c4c27e 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -49,7 +49,7 @@ impl RunCommand { eprintln!("{err}"); ExitCode::FAILURE } - Ok((code, _)) => code, + Ok((code, _)) => ExitCode::from(code), }) } } diff --git a/src/lune/mod.rs b/src/lune/mod.rs index 51ac6710..45c1715f 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -1,5 +1,4 @@ use std::{ - process::ExitCode, rc::Rc, sync::{ atomic::{AtomicBool, Ordering}, @@ -64,7 +63,7 @@ impl Runtime { &mut self, script_name: impl AsRef, script_contents: impl AsRef<[u8]>, - ) -> Result<(i32, Vec), RuntimeError> { + ) -> Result<(u8, Vec), RuntimeError> { // Create a new scheduler for this run let sched = Scheduler::new(&self.lua); diff --git a/src/standalone/mod.rs b/src/standalone/mod.rs index 03555b57..53816ab8 100644 --- a/src/standalone/mod.rs +++ b/src/standalone/mod.rs @@ -37,6 +37,6 @@ pub async fn run(patched_bin: impl AsRef<[u8]>) -> Result { eprintln!("{err}"); ExitCode::FAILURE } - Ok((code, _)) => code, + Ok((code, _)) => ExitCode::from(code), }) } diff --git a/src/tests.rs b/src/tests.rs index ad9a2ebf..6fce6e5a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -32,7 +32,7 @@ macro_rules! create_tests { .trim_end_matches(".lua") .to_string(); let (exit_code, _) = lune.run(&script_name, &script).await?; - Ok(exit_code) + Ok(ExitCode::from(exit_code as u8)) } )* } } From 8c16abd6b1156ae8e8ca409fadcda4bb6d6c2e98 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 13 Apr 2024 18:19:36 +0530 Subject: [PATCH 06/10] refactor: fix clippy warns I know this is out of the scope of this current PRs, but these minor clippy warnings have been a bit annoying to look at lately, so I've gone ahead and fixed them! :) --- src/lune/util/formatting.rs | 2 +- src/lune/util/traits.rs | 1 + src/roblox/instance/terrain.rs | 8 +++++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lune/util/formatting.rs b/src/lune/util/formatting.rs index 19f9a884..8ca4b0ca 100644 --- a/src/lune/util/formatting.rs +++ b/src/lune/util/formatting.rs @@ -231,7 +231,7 @@ pub fn pretty_format_luau_error(e: &LuaError, colorized: bool) -> String { let mut found_stack_begin = false; for (index, line) in err_lines.clone().iter().enumerate().rev() { if *line == "stack traceback:" { - err_lines[index] = stack_begin.clone(); + err_lines[index].clone_from(&stack_begin); found_stack_begin = true; break; } diff --git a/src/lune/util/traits.rs b/src/lune/util/traits.rs index 2c75d65e..523a33c4 100644 --- a/src/lune/util/traits.rs +++ b/src/lune/util/traits.rs @@ -4,6 +4,7 @@ use super::formatting::format_label; use crate::RuntimeError; pub trait LuaEmitErrorExt { + #[allow(dead_code)] fn emit_error(&self, err: LuaError); } diff --git a/src/roblox/instance/terrain.rs b/src/roblox/instance/terrain.rs index fad8e3d7..a0236a66 100644 --- a/src/roblox/instance/terrain.rs +++ b/src/roblox/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!() } } From 962a2e50be3638b0dd64fb90ffe25208e4ca13ca Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 12 May 2024 19:59:24 +0530 Subject: [PATCH 07/10] 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 988c44aa..fdfa079b 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 852f321a..f39d59e6 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 4e43302a..1afb52a0 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 35e523e6..fcce205d 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 a5501c6d..0499b3b9 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 fe589130..805eb955 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 ae599da3..7a5f5a73 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)) } )* } } From 6c2da4a7ec1fc2c3398a5b1169f2f8b12a5d3c66 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sun, 12 May 2024 20:18:11 +0530 Subject: [PATCH 08/10] chore: regenerate lockfile --- Cargo.lock | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 04290170..fdfa079b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1765,7 +1765,8 @@ dependencies = [ [[package]] name = "mlua-luau-scheduler" version = "0.0.2" -source = "git+https://github.com/0x5eal/mlua-luau-scheduler-exitstatus.git#82c8b902e09a21a9befa4e037beadefbe2360b7f" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a13eabdbc57fa38cf0b604d98ce3431573c79a964aac56e09c16c240d36cb1bf" dependencies = [ "async-executor", "blocking", From 4c7701bfde30093c4aad3d9c2d85b7b64e5611b8 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 3 Jun 2024 09:56:56 +0530 Subject: [PATCH 09/10] chore: remove old repo structure dir --- src/lune/util/formatting.rs | 477 ------------------------------------ src/lune/util/traits.rs | 16 -- 2 files changed, 493 deletions(-) delete mode 100644 src/lune/util/formatting.rs delete mode 100644 src/lune/util/traits.rs diff --git a/src/lune/util/formatting.rs b/src/lune/util/formatting.rs deleted file mode 100644 index ca2573c8..00000000 --- a/src/lune/util/formatting.rs +++ /dev/null @@ -1,477 +0,0 @@ -use std::fmt::Write; - -use console::{colors_enabled, set_colors_enabled, style, Style}; -use mlua::prelude::*; -use once_cell::sync::Lazy; - -const MAX_FORMAT_DEPTH: usize = 4; - -const INDENT: &str = " "; - -pub const STYLE_RESET_STR: &str = "\x1b[0m"; - -// Colors -pub static COLOR_BLACK: Lazy