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.
This commit is contained in:
Erica Marigold 2024-04-11 18:11:52 +05:30
parent 0d302cf0c1
commit 390937d918
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
4 changed files with 26 additions and 22 deletions

View file

@ -7,9 +7,8 @@ use tokio::{
io::{stdin, AsyncReadExt as _}, io::{stdin, AsyncReadExt as _},
}; };
use lune::Runtime;
use super::utils::files::{discover_script_path_including_lune_dirs, strip_shebang}; use super::utils::files::{discover_script_path_including_lune_dirs, strip_shebang};
use lune::Runtime;
/// Run a script /// Run a script
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
@ -41,8 +40,8 @@ impl RunCommand {
}; };
// Create a new lune object with all globals & run the script // Create a new lune object with all globals & run the script
let result = Runtime::new() let mut runtime = Runtime::new().with_args(self.script_args);
.with_args(self.script_args) let result = runtime
.run(&script_display_name, strip_shebang(script_contents)) .run(&script_display_name, strip_shebang(script_contents))
.await; .await;
Ok(match result { Ok(match result {
@ -50,7 +49,7 @@ impl RunCommand {
eprintln!("{err}"); eprintln!("{err}");
ExitCode::FAILURE ExitCode::FAILURE
} }
Ok(code) => code, Ok((code, _)) => code,
}) })
} }
} }

View file

@ -7,7 +7,7 @@ use std::{
}, },
}; };
use mlua::Lua; use mlua::{Lua, Value};
use mlua_luau_scheduler::Scheduler; use mlua_luau_scheduler::Scheduler;
mod builtins; mod builtins;
@ -64,7 +64,7 @@ impl Runtime {
&mut self, &mut self,
script_name: impl AsRef<str>, script_name: impl AsRef<str>,
script_contents: impl AsRef<[u8]>, script_contents: impl AsRef<[u8]>,
) -> Result<ExitCode, RuntimeError> { ) -> Result<(ExitCode, Vec<Value>), RuntimeError> {
// Create a new scheduler for this run // Create a new scheduler for this run
let sched = Scheduler::new(&self.lua); let sched = Scheduler::new(&self.lua);
@ -83,16 +83,23 @@ impl Runtime {
.set_name(script_name.as_ref()); .set_name(script_name.as_ref());
// Run it on our scheduler until it and any other spawned threads complete // 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; sched.run().await;
// Return the exit code - default to FAILURE if we got any errors let thread_res = sched
Ok(sched.get_exit_code().unwrap_or({ .get_thread_result(main_thread_id)
if got_any_error.load(Ordering::SeqCst) { .unwrap()
ExitCode::FAILURE .unwrap()
} else { .into_vec();
ExitCode::SUCCESS Ok((
} sched.get_exit_code().unwrap_or({
})) if got_any_error.load(Ordering::SeqCst) {
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}),
thread_res,
))
} }
} }

View file

@ -29,16 +29,14 @@ pub async fn run(patched_bin: impl AsRef<[u8]>) -> Result<ExitCode> {
let args = env::args().skip(1).collect::<Vec<_>>(); let args = env::args().skip(1).collect::<Vec<_>>();
let meta = Metadata::from_bytes(patched_bin).expect("must be a standalone binary"); let meta = Metadata::from_bytes(patched_bin).expect("must be a standalone binary");
let result = Runtime::new() let mut runtime = Runtime::new().with_args(args);
.with_args(args) let result = runtime.run("STANDALONE", meta.bytecode).await;
.run("STANDALONE", meta.bytecode)
.await;
Ok(match result { Ok(match result {
Err(err) => { Err(err) => {
eprintln!("{err}"); eprintln!("{err}");
ExitCode::FAILURE ExitCode::FAILURE
} }
Ok(code) => code, Ok((code, _)) => code,
}) })
} }

View file

@ -31,7 +31,7 @@ macro_rules! create_tests {
.trim_end_matches(".luau") .trim_end_matches(".luau")
.trim_end_matches(".lua") .trim_end_matches(".lua")
.to_string(); .to_string();
let exit_code = lune.run(&script_name, &script).await?; let (exit_code, _) = lune.run(&script_name, &script).await?;
Ok(exit_code) Ok(exit_code)
} }
)* } )* }