From 390937d9189490b2acdfdf60a58ed73c6e4ce38b Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Thu, 11 Apr 2024 18:11:52 +0530 Subject: [PATCH] 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 35e523e..5ce8ab7 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 ce676bf..0f2ad02 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 fe58913..03555b5 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 fad6026..ad9a2eb 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) } )* }