diff --git a/src/lune/error.rs b/src/lune/error.rs index c4d9f8c..dcb9222 100644 --- a/src/lune/error.rs +++ b/src/lune/error.rs @@ -5,8 +5,6 @@ use std::{ use mlua::prelude::*; -use crate::lune::lua::stdio::formatting::pretty_format_luau_error; - /** An opaque error type for formatted lua errors. */ @@ -66,11 +64,7 @@ impl From for LuneError { impl Display for LuneError { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - write!( - f, - "{}", - pretty_format_luau_error(&self.error, !self.disable_colors) - ) + write!(f, "{}", self.error) // TODO: Pretty formatting } } diff --git a/src/lune/mod.rs b/src/lune/mod.rs index a5050b7..6e5c286 100644 --- a/src/lune/mod.rs +++ b/src/lune/mod.rs @@ -1,12 +1,6 @@ use std::process::ExitCode; -use lua::task::{TaskScheduler, TaskSchedulerResumeExt, TaskSchedulerScheduleExt}; use mlua::prelude::*; -use tokio::task::LocalSet; - -pub mod builtins; -pub mod importer; -pub mod lua; mod error; @@ -37,17 +31,7 @@ impl Lune { } /** - Runs a Lune script. - - This will create a new sandboxed Luau environment with the configured - globals and arguments, running inside of a [`tokio::task::LocalSet`]. - - Some Lune globals may spawn separate tokio tasks on other threads, but the Luau - environment itself is guaranteed to run on a single thread in the local set. - - Note that this will create a static Lua instance and task scheduler that will - both live for the remainer of the program, and that this leaks memory using - [`Box::leak`] that will then get deallocated when the program exits. + Runs a Lune script inside of a new Luau VM. */ pub async fn run( &self, @@ -61,47 +45,9 @@ impl Lune { async fn run_inner( &self, - script_name: impl AsRef, - script_contents: impl AsRef<[u8]>, - ) -> Result { - // Create our special lune-flavored Lua object with extra registry values - let lua = lua::create_lune_lua()?.into_static(); - // Create our task scheduler and all globals - // NOTE: Some globals require the task scheduler to exist on startup - let sched = TaskScheduler::new(lua)?.into_static(); - lua.set_app_data(sched); - importer::create(lua, self.args.clone())?; - // Create the main thread and schedule it - let main_chunk = lua - .load(script_contents.as_ref()) - .set_name(script_name.as_ref()) - .into_function()?; - let main_thread = lua.create_thread(main_chunk)?; - let main_thread_args = LuaValue::Nil.into_lua_multi(lua)?; - sched.schedule_blocking(main_thread, main_thread_args)?; - // Keep running the scheduler until there are either no tasks - // left to run, or until a task requests to exit the process - let exit_code = LocalSet::new() - .run_until(async move { - let mut got_error = false; - loop { - let result = sched.resume_queue().await; - if let Some(err) = result.get_lua_error() { - eprintln!("{}", LuneError::from(err)); - got_error = true; - } - if result.is_done() { - if let Some(exit_code) = result.get_exit_code() { - break exit_code; - } else if got_error { - break ExitCode::FAILURE; - } else { - break ExitCode::SUCCESS; - } - } - } - }) - .await; - Ok(exit_code) + _script_name: impl AsRef, + _script_contents: impl AsRef<[u8]>, + ) -> LuaResult { + Ok(ExitCode::SUCCESS) } } diff --git a/src/lune/builtins/fs.rs b/src/old/builtins/fs.rs similarity index 99% rename from src/lune/builtins/fs.rs rename to src/old/builtins/fs.rs index fd2db01..d2b9bba 100644 --- a/src/lune/builtins/fs.rs +++ b/src/old/builtins/fs.rs @@ -4,7 +4,7 @@ use std::path::{PathBuf, MAIN_SEPARATOR}; use mlua::prelude::*; use tokio::fs; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ fs::{copy, FsMetadata, FsWriteOptions}, table::TableBuilder, }; diff --git a/src/lune/builtins/luau.rs b/src/old/builtins/luau.rs similarity index 97% rename from src/lune/builtins/luau.rs rename to src/old/builtins/luau.rs index 6d0c7e4..54aeb6a 100644 --- a/src/lune/builtins/luau.rs +++ b/src/old/builtins/luau.rs @@ -1,6 +1,6 @@ use mlua::prelude::*; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ luau::{LuauCompileOptions, LuauLoadOptions}, table::TableBuilder, }; diff --git a/src/lune/builtins/mod.rs b/src/old/builtins/mod.rs similarity index 100% rename from src/lune/builtins/mod.rs rename to src/old/builtins/mod.rs diff --git a/src/lune/builtins/net.rs b/src/old/builtins/net.rs similarity index 99% rename from src/lune/builtins/net.rs rename to src/old/builtins/net.rs index 0be55b5..e313928 100644 --- a/src/lune/builtins/net.rs +++ b/src/old/builtins/net.rs @@ -9,7 +9,7 @@ use hyper::{ }; use tokio::{sync::mpsc, task}; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ net::{ NetClient, NetClientBuilder, NetLocalExec, NetService, NetWebSocket, RequestConfig, ServeConfig, diff --git a/src/lune/builtins/process.rs b/src/old/builtins/process.rs similarity index 99% rename from src/lune/builtins/process.rs rename to src/old/builtins/process.rs index 51e7c6f..6ef0fa8 100644 --- a/src/lune/builtins/process.rs +++ b/src/old/builtins/process.rs @@ -11,7 +11,7 @@ use mlua::prelude::*; use os_str_bytes::RawOsString; use tokio::process::Command; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ process::pipe_and_inherit_child_process_stdio, table::TableBuilder, task::TaskScheduler, }; diff --git a/src/lune/builtins/roblox.rs b/src/old/builtins/roblox.rs similarity index 98% rename from src/lune/builtins/roblox.rs rename to src/old/builtins/roblox.rs index 2141d5c..2664f03 100644 --- a/src/lune/builtins/roblox.rs +++ b/src/old/builtins/roblox.rs @@ -10,7 +10,7 @@ use crate::roblox::{ use tokio::task; -use crate::lune::lua::table::TableBuilder; +use crate::lune_temp::lua::table::TableBuilder; static REFLECTION_DATABASE: OnceCell = OnceCell::new(); diff --git a/src/lune/builtins/serde.rs b/src/old/builtins/serde.rs similarity index 98% rename from src/lune/builtins/serde.rs rename to src/old/builtins/serde.rs index 5069337..09a4abb 100644 --- a/src/lune/builtins/serde.rs +++ b/src/old/builtins/serde.rs @@ -1,6 +1,6 @@ use mlua::prelude::*; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ serde::{ compress, decompress, CompressDecompressFormat, EncodeDecodeConfig, EncodeDecodeFormat, }, diff --git a/src/lune/builtins/stdio.rs b/src/old/builtins/stdio.rs similarity index 99% rename from src/lune/builtins/stdio.rs rename to src/old/builtins/stdio.rs index acd3727..98a768d 100644 --- a/src/lune/builtins/stdio.rs +++ b/src/old/builtins/stdio.rs @@ -5,7 +5,7 @@ use tokio::{ task, }; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ stdio::{ formatting::{ format_style, pretty_format_multi_value, style_from_color_str, style_from_style_str, diff --git a/src/lune/builtins/task.rs b/src/old/builtins/task.rs similarity index 99% rename from src/lune/builtins/task.rs rename to src/old/builtins/task.rs index 98a0383..3db015a 100644 --- a/src/lune/builtins/task.rs +++ b/src/old/builtins/task.rs @@ -1,6 +1,6 @@ use mlua::prelude::*; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ async_ext::LuaAsyncExt, table::TableBuilder, task::{ diff --git a/src/lune/builtins/top_level.rs b/src/old/builtins/top_level.rs similarity index 98% rename from src/lune/builtins/top_level.rs rename to src/old/builtins/top_level.rs index a101fb0..0261a21 100644 --- a/src/lune/builtins/top_level.rs +++ b/src/old/builtins/top_level.rs @@ -4,7 +4,7 @@ use std::io::{self, Write as _}; #[cfg(feature = "roblox")] use crate::roblox::datatypes::extension::RobloxUserdataTypenameExt; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ stdio::formatting::{format_label, pretty_format_multi_value}, task::TaskReference, }; diff --git a/src/old/error.rs b/src/old/error.rs new file mode 100644 index 0000000..e184e2e --- /dev/null +++ b/src/old/error.rs @@ -0,0 +1,83 @@ +use std::{ + error::Error, + fmt::{Debug, Display, Formatter, Result as FmtResult}, +}; + +use mlua::prelude::*; + +use crate::lune_temp::lua::stdio::formatting::pretty_format_luau_error; + +/** + An opaque error type for formatted lua errors. +*/ +#[derive(Debug, Clone)] +pub struct LuneError { + error: LuaError, + disable_colors: bool, +} + +impl LuneError { + /** + Enables colorization of the error message when formatted using the [`Display`] trait. + + Colorization is enabled by default. + */ + #[doc(hidden)] + pub fn enable_colors(mut self) -> Self { + self.disable_colors = false; + self + } + + /** + Disables colorization of the error message when formatted using the [`Display`] trait. + + Colorization is enabled by default. + */ + #[doc(hidden)] + pub fn disable_colors(mut self) -> Self { + self.disable_colors = true; + self + } + + /** + Returns `true` if the error can likely be fixed by appending more input to the source code. + + See [`mlua::Error::SyntaxError`] for more information. + */ + pub fn is_incomplete_input(&self) -> bool { + matches!( + self.error, + LuaError::SyntaxError { + incomplete_input: true, + .. + } + ) + } +} + +impl From for LuneError { + fn from(value: LuaError) -> Self { + Self { + error: value, + disable_colors: false, + } + } +} + +impl Display for LuneError { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + write!( + f, + "{}", + pretty_format_luau_error(&self.error, !self.disable_colors) + ) + } +} + +impl Error for LuneError { + // TODO: Comment this out when we are ready to also re-export + // `mlua` as part of our public library interface in Lune + // fn cause(&self) -> Option<&dyn Error> { + // Some(&self.error) + // } +} diff --git a/src/lune/importer/mod.rs b/src/old/importer/mod.rs similarity index 96% rename from src/lune/importer/mod.rs rename to src/old/importer/mod.rs index de3cdf6..86165cc 100644 --- a/src/lune/importer/mod.rs +++ b/src/old/importer/mod.rs @@ -3,7 +3,7 @@ use mlua::prelude::*; mod require; mod require_waker; -use crate::lune::builtins::{self, top_level}; +use crate::lune_temp::builtins::{self, top_level}; pub fn create(lua: &'static Lua, args: Vec) -> LuaResult<()> { // Create all builtins diff --git a/src/lune/importer/require.rs b/src/old/importer/require.rs similarity index 99% rename from src/lune/importer/require.rs rename to src/old/importer/require.rs index 113c593..0f1d7f4 100644 --- a/src/lune/importer/require.rs +++ b/src/old/importer/require.rs @@ -10,7 +10,7 @@ use mlua::prelude::*; use tokio::fs; use tokio::sync::Mutex as AsyncMutex; -use crate::lune::lua::{ +use crate::lune_temp::lua::{ table::TableBuilder, task::{TaskScheduler, TaskSchedulerScheduleExt}, }; diff --git a/src/lune/importer/require_waker.rs b/src/old/importer/require_waker.rs similarity index 100% rename from src/lune/importer/require_waker.rs rename to src/old/importer/require_waker.rs diff --git a/src/lune/lua/async_ext.rs b/src/old/lua/async_ext.rs similarity index 97% rename from src/lune/lua/async_ext.rs rename to src/old/lua/async_ext.rs index afdd29b..7e6fef9 100644 --- a/src/lune/lua/async_ext.rs +++ b/src/old/lua/async_ext.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use futures_util::Future; use mlua::prelude::*; -use crate::lune::{lua::table::TableBuilder, lua::task::TaskScheduler}; +use crate::lune_temp::{lua::table::TableBuilder, lua::task::TaskScheduler}; use super::task::TaskSchedulerAsyncExt; diff --git a/src/lune/lua/create.rs b/src/old/lua/create.rs similarity index 100% rename from src/lune/lua/create.rs rename to src/old/lua/create.rs diff --git a/src/lune/lua/fs/copy.rs b/src/old/lua/fs/copy.rs similarity index 100% rename from src/lune/lua/fs/copy.rs rename to src/old/lua/fs/copy.rs diff --git a/src/lune/lua/fs/metadata.rs b/src/old/lua/fs/metadata.rs similarity index 100% rename from src/lune/lua/fs/metadata.rs rename to src/old/lua/fs/metadata.rs diff --git a/src/lune/lua/fs/mod.rs b/src/old/lua/fs/mod.rs similarity index 100% rename from src/lune/lua/fs/mod.rs rename to src/old/lua/fs/mod.rs diff --git a/src/lune/lua/fs/options.rs b/src/old/lua/fs/options.rs similarity index 100% rename from src/lune/lua/fs/options.rs rename to src/old/lua/fs/options.rs diff --git a/src/lune/lua/luau/mod.rs b/src/old/lua/luau/mod.rs similarity index 100% rename from src/lune/lua/luau/mod.rs rename to src/old/lua/luau/mod.rs diff --git a/src/lune/lua/luau/options.rs b/src/old/lua/luau/options.rs similarity index 100% rename from src/lune/lua/luau/options.rs rename to src/old/lua/luau/options.rs diff --git a/src/lune/lua/mod.rs b/src/old/lua/mod.rs similarity index 100% rename from src/lune/lua/mod.rs rename to src/old/lua/mod.rs diff --git a/src/lune/lua/net/client.rs b/src/old/lua/net/client.rs similarity index 100% rename from src/lune/lua/net/client.rs rename to src/old/lua/net/client.rs diff --git a/src/lune/lua/net/config.rs b/src/old/lua/net/config.rs similarity index 100% rename from src/lune/lua/net/config.rs rename to src/old/lua/net/config.rs diff --git a/src/lune/lua/net/mod.rs b/src/old/lua/net/mod.rs similarity index 100% rename from src/lune/lua/net/mod.rs rename to src/old/lua/net/mod.rs diff --git a/src/lune/lua/net/response.rs b/src/old/lua/net/response.rs similarity index 100% rename from src/lune/lua/net/response.rs rename to src/old/lua/net/response.rs diff --git a/src/lune/lua/net/server.rs b/src/old/lua/net/server.rs similarity index 99% rename from src/lune/lua/net/server.rs rename to src/old/lua/net/server.rs index d3428df..807a0f2 100644 --- a/src/lune/lua/net/server.rs +++ b/src/old/lua/net/server.rs @@ -12,7 +12,7 @@ use hyper::{Body, Request, Response}; use hyper_tungstenite::{is_upgrade_request as is_ws_upgrade_request, upgrade as ws_upgrade}; use tokio::task; -use crate::lune::{ +use crate::lune_temp::{ lua::table::TableBuilder, lua::task::{TaskScheduler, TaskSchedulerAsyncExt, TaskSchedulerScheduleExt}, }; diff --git a/src/lune/lua/net/websocket.rs b/src/old/lua/net/websocket.rs similarity index 99% rename from src/lune/lua/net/websocket.rs rename to src/old/lua/net/websocket.rs index 1057b1b..ece0ce9 100644 --- a/src/lune/lua/net/websocket.rs +++ b/src/old/lua/net/websocket.rs @@ -22,7 +22,7 @@ use hyper_tungstenite::{ }; use tokio_tungstenite::MaybeTlsStream; -use crate::lune::lua::table::TableBuilder; +use crate::lune_temp::lua::table::TableBuilder; const WEB_SOCKET_IMPL_LUA: &str = r#" return freeze(setmetatable({ diff --git a/src/lune/lua/process/mod.rs b/src/old/lua/process/mod.rs similarity index 100% rename from src/lune/lua/process/mod.rs rename to src/old/lua/process/mod.rs diff --git a/src/lune/lua/process/tee_writer.rs b/src/old/lua/process/tee_writer.rs similarity index 100% rename from src/lune/lua/process/tee_writer.rs rename to src/old/lua/process/tee_writer.rs diff --git a/src/lune/lua/serde/compress_decompress.rs b/src/old/lua/serde/compress_decompress.rs similarity index 100% rename from src/lune/lua/serde/compress_decompress.rs rename to src/old/lua/serde/compress_decompress.rs diff --git a/src/lune/lua/serde/encode_decode.rs b/src/old/lua/serde/encode_decode.rs similarity index 100% rename from src/lune/lua/serde/encode_decode.rs rename to src/old/lua/serde/encode_decode.rs diff --git a/src/lune/lua/serde/mod.rs b/src/old/lua/serde/mod.rs similarity index 100% rename from src/lune/lua/serde/mod.rs rename to src/old/lua/serde/mod.rs diff --git a/src/lune/lua/stdio/formatting.rs b/src/old/lua/stdio/formatting.rs similarity index 99% rename from src/lune/lua/stdio/formatting.rs rename to src/old/lua/stdio/formatting.rs index 8437299..02dd286 100644 --- a/src/lune/lua/stdio/formatting.rs +++ b/src/old/lua/stdio/formatting.rs @@ -4,7 +4,7 @@ use console::{colors_enabled, set_colors_enabled, style, Style}; use mlua::prelude::*; use once_cell::sync::Lazy; -use crate::lune::lua::task::TaskReference; +use crate::lune_temp::lua::task::TaskReference; const MAX_FORMAT_DEPTH: usize = 4; diff --git a/src/lune/lua/stdio/mod.rs b/src/old/lua/stdio/mod.rs similarity index 100% rename from src/lune/lua/stdio/mod.rs rename to src/old/lua/stdio/mod.rs diff --git a/src/lune/lua/stdio/prompt.rs b/src/old/lua/stdio/prompt.rs similarity index 100% rename from src/lune/lua/stdio/prompt.rs rename to src/old/lua/stdio/prompt.rs diff --git a/src/lune/lua/table/builder.rs b/src/old/lua/table/builder.rs similarity index 97% rename from src/lune/lua/table/builder.rs rename to src/old/lua/table/builder.rs index b320707..836313b 100644 --- a/src/lune/lua/table/builder.rs +++ b/src/old/lua/table/builder.rs @@ -2,7 +2,7 @@ use std::future::Future; use mlua::prelude::*; -use crate::lune::lua::async_ext::LuaAsyncExt; +use crate::lune_temp::lua::async_ext::LuaAsyncExt; pub struct TableBuilder { lua: &'static Lua, diff --git a/src/lune/lua/table/mod.rs b/src/old/lua/table/mod.rs similarity index 100% rename from src/lune/lua/table/mod.rs rename to src/old/lua/table/mod.rs diff --git a/src/lune/lua/task/ext/async_ext.rs b/src/old/lua/task/ext/async_ext.rs similarity index 99% rename from src/lune/lua/task/ext/async_ext.rs rename to src/old/lua/task/ext/async_ext.rs index 681c01a..26d1da8 100644 --- a/src/lune/lua/task/ext/async_ext.rs +++ b/src/old/lua/task/ext/async_ext.rs @@ -6,7 +6,7 @@ use futures_util::Future; use mlua::prelude::*; use tokio::time::{sleep, Instant}; -use crate::lune::lua::task::TaskKind; +use crate::lune_temp::lua::task::TaskKind; use super::super::{ scheduler::TaskReference, scheduler::TaskScheduler, scheduler_handle::TaskSchedulerAsyncHandle, diff --git a/src/lune/lua/task/ext/mod.rs b/src/old/lua/task/ext/mod.rs similarity index 100% rename from src/lune/lua/task/ext/mod.rs rename to src/old/lua/task/ext/mod.rs diff --git a/src/lune/lua/task/ext/resume_ext.rs b/src/old/lua/task/ext/resume_ext.rs similarity index 100% rename from src/lune/lua/task/ext/resume_ext.rs rename to src/old/lua/task/ext/resume_ext.rs diff --git a/src/lune/lua/task/ext/schedule_ext.rs b/src/old/lua/task/ext/schedule_ext.rs similarity index 100% rename from src/lune/lua/task/ext/schedule_ext.rs rename to src/old/lua/task/ext/schedule_ext.rs diff --git a/src/lune/lua/task/mod.rs b/src/old/lua/task/mod.rs similarity index 100% rename from src/lune/lua/task/mod.rs rename to src/old/lua/task/mod.rs diff --git a/src/lune/lua/task/proxy.rs b/src/old/lua/task/proxy.rs similarity index 100% rename from src/lune/lua/task/proxy.rs rename to src/old/lua/task/proxy.rs diff --git a/src/lune/lua/task/scheduler.rs b/src/old/lua/task/scheduler.rs similarity index 100% rename from src/lune/lua/task/scheduler.rs rename to src/old/lua/task/scheduler.rs diff --git a/src/lune/lua/task/scheduler_handle.rs b/src/old/lua/task/scheduler_handle.rs similarity index 100% rename from src/lune/lua/task/scheduler_handle.rs rename to src/old/lua/task/scheduler_handle.rs diff --git a/src/lune/lua/task/scheduler_message.rs b/src/old/lua/task/scheduler_message.rs similarity index 100% rename from src/lune/lua/task/scheduler_message.rs rename to src/old/lua/task/scheduler_message.rs diff --git a/src/lune/lua/task/scheduler_state.rs b/src/old/lua/task/scheduler_state.rs similarity index 100% rename from src/lune/lua/task/scheduler_state.rs rename to src/old/lua/task/scheduler_state.rs diff --git a/src/lune/lua/task/task_kind.rs b/src/old/lua/task/task_kind.rs similarity index 100% rename from src/lune/lua/task/task_kind.rs rename to src/old/lua/task/task_kind.rs diff --git a/src/lune/lua/task/task_reference.rs b/src/old/lua/task/task_reference.rs similarity index 100% rename from src/lune/lua/task/task_reference.rs rename to src/old/lua/task/task_reference.rs diff --git a/src/lune/lua/task/task_waiter.rs b/src/old/lua/task/task_waiter.rs similarity index 100% rename from src/lune/lua/task/task_waiter.rs rename to src/old/lua/task/task_waiter.rs diff --git a/src/old/mod.rs b/src/old/mod.rs new file mode 100644 index 0000000..a5050b7 --- /dev/null +++ b/src/old/mod.rs @@ -0,0 +1,107 @@ +use std::process::ExitCode; + +use lua::task::{TaskScheduler, TaskSchedulerResumeExt, TaskSchedulerScheduleExt}; +use mlua::prelude::*; +use tokio::task::LocalSet; + +pub mod builtins; +pub mod importer; +pub mod lua; + +mod error; + +pub use error::LuneError; + +#[derive(Clone, Debug, Default)] +pub struct Lune { + args: Vec, +} + +impl Lune { + /** + Creates a new Lune script runner. + */ + pub fn new() -> Self { + Self::default() + } + + /** + Arguments to give in `process.args` for a Lune script. + */ + pub fn with_args(mut self, args: V) -> Self + where + V: Into>, + { + self.args = args.into(); + self + } + + /** + Runs a Lune script. + + This will create a new sandboxed Luau environment with the configured + globals and arguments, running inside of a [`tokio::task::LocalSet`]. + + Some Lune globals may spawn separate tokio tasks on other threads, but the Luau + environment itself is guaranteed to run on a single thread in the local set. + + Note that this will create a static Lua instance and task scheduler that will + both live for the remainer of the program, and that this leaks memory using + [`Box::leak`] that will then get deallocated when the program exits. + */ + pub async fn run( + &self, + script_name: impl AsRef, + script_contents: impl AsRef<[u8]>, + ) -> Result { + self.run_inner(script_name, script_contents) + .await + .map_err(LuneError::from) + } + + async fn run_inner( + &self, + script_name: impl AsRef, + script_contents: impl AsRef<[u8]>, + ) -> Result { + // Create our special lune-flavored Lua object with extra registry values + let lua = lua::create_lune_lua()?.into_static(); + // Create our task scheduler and all globals + // NOTE: Some globals require the task scheduler to exist on startup + let sched = TaskScheduler::new(lua)?.into_static(); + lua.set_app_data(sched); + importer::create(lua, self.args.clone())?; + // Create the main thread and schedule it + let main_chunk = lua + .load(script_contents.as_ref()) + .set_name(script_name.as_ref()) + .into_function()?; + let main_thread = lua.create_thread(main_chunk)?; + let main_thread_args = LuaValue::Nil.into_lua_multi(lua)?; + sched.schedule_blocking(main_thread, main_thread_args)?; + // Keep running the scheduler until there are either no tasks + // left to run, or until a task requests to exit the process + let exit_code = LocalSet::new() + .run_until(async move { + let mut got_error = false; + loop { + let result = sched.resume_queue().await; + if let Some(err) = result.get_lua_error() { + eprintln!("{}", LuneError::from(err)); + got_error = true; + } + if result.is_done() { + if let Some(exit_code) = result.get_exit_code() { + break exit_code; + } else if got_error { + break ExitCode::FAILURE; + } else { + break ExitCode::SUCCESS; + } + } + } + }) + .await; + Ok(exit_code) + } +}