mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-07 12:00:58 +01:00
Add exit lua function to functions struct
This commit is contained in:
parent
4117cfba75
commit
743d1075bf
2 changed files with 43 additions and 36 deletions
|
@ -1,54 +1,24 @@
|
||||||
#![allow(clippy::missing_errors_doc)]
|
#![allow(clippy::missing_errors_doc)]
|
||||||
#![allow(clippy::missing_panics_doc)]
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
|
||||||
use std::process::ExitCode;
|
|
||||||
|
|
||||||
use async_io::block_on;
|
use async_io::block_on;
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use mlua_luau_runtime::{LuaRuntimeExt, Runtime};
|
use mlua_luau_runtime::{Functions, Runtime};
|
||||||
|
|
||||||
const MAIN_SCRIPT: &str = include_str!("./lua/exit_code.luau");
|
const MAIN_SCRIPT: &str = include_str!("./lua/exit_code.luau");
|
||||||
|
|
||||||
const EXIT_IMPL_LUA: &str = r"
|
|
||||||
exit(...)
|
|
||||||
yield()
|
|
||||||
";
|
|
||||||
|
|
||||||
pub fn main() -> LuaResult<()> {
|
pub fn main() -> LuaResult<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
||||||
// Set up persistent Lua environment
|
// Set up persistent Lua environment
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
|
|
||||||
// Note that our exit function is partially implemented in Lua
|
|
||||||
// because we need to also yield the thread that called it, this
|
|
||||||
// is not possible to do in Rust because of crossing C-call boundary
|
|
||||||
let exit_fn_env = lua.create_table_from(vec![
|
|
||||||
(
|
|
||||||
"exit",
|
|
||||||
lua.create_function(|lua, code: Option<u8>| {
|
|
||||||
let code = code.map(ExitCode::from).unwrap_or_default();
|
|
||||||
lua.set_exit_code(code);
|
|
||||||
Ok(())
|
|
||||||
})?,
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"yield",
|
|
||||||
lua.globals()
|
|
||||||
.get::<_, LuaTable>("coroutine")?
|
|
||||||
.get::<_, LuaFunction>("yield")?,
|
|
||||||
),
|
|
||||||
])?;
|
|
||||||
|
|
||||||
let exit_fn = lua
|
|
||||||
.load(EXIT_IMPL_LUA)
|
|
||||||
.set_environment(exit_fn_env)
|
|
||||||
.into_function()?;
|
|
||||||
lua.globals().set("exit", exit_fn)?;
|
|
||||||
|
|
||||||
// Load the main script into a runtime
|
|
||||||
let rt = Runtime::new(&lua);
|
let rt = Runtime::new(&lua);
|
||||||
|
let fns = Functions::new(&lua)?;
|
||||||
|
|
||||||
|
lua.globals().set("exit", fns.exit)?;
|
||||||
|
|
||||||
|
// Load the main script into the runtime
|
||||||
let main = lua.load(MAIN_SCRIPT);
|
let main = lua.load(MAIN_SCRIPT);
|
||||||
rt.push_thread_front(main, ())?;
|
rt.push_thread_front(main, ())?;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#![allow(unused_imports)]
|
#![allow(unused_imports)]
|
||||||
#![allow(clippy::module_name_repetitions)]
|
#![allow(clippy::module_name_repetitions)]
|
||||||
|
|
||||||
|
use std::process::ExitCode;
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -9,6 +11,7 @@ use crate::{
|
||||||
result_map::ThreadResultMap,
|
result_map::ThreadResultMap,
|
||||||
runtime::Runtime,
|
runtime::Runtime,
|
||||||
thread_id::ThreadId,
|
thread_id::ThreadId,
|
||||||
|
traits::LuaRuntimeExt,
|
||||||
util::{is_poll_pending, LuaThreadOrFunction, ThreadResult},
|
util::{is_poll_pending, LuaThreadOrFunction, ThreadResult},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,6 +21,11 @@ Lua state does not have runtime metadata attached!\
|
||||||
\nRuntime functions must always be created from within an active runtime.\
|
\nRuntime functions must always be created from within an active runtime.\
|
||||||
";
|
";
|
||||||
|
|
||||||
|
const EXIT_IMPL_LUA: &str = r"
|
||||||
|
exit(...)
|
||||||
|
yield()
|
||||||
|
";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A collection of lua functions that may be called to interact with a [`Runtime`].
|
A collection of lua functions that may be called to interact with a [`Runtime`].
|
||||||
*/
|
*/
|
||||||
|
@ -38,6 +46,12 @@ pub struct Functions<'lua> {
|
||||||
Cancels a function / thread, removing it from the queue.
|
Cancels a function / thread, removing it from the queue.
|
||||||
*/
|
*/
|
||||||
pub cancel: LuaFunction<'lua>,
|
pub cancel: LuaFunction<'lua>,
|
||||||
|
/**
|
||||||
|
Exits the runtime, stopping all other threads and closing the runtime.
|
||||||
|
|
||||||
|
Yields the calling thread to ensure that it does not continue.
|
||||||
|
*/
|
||||||
|
pub exit: LuaFunction<'lua>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'lua> Functions<'lua> {
|
impl<'lua> Functions<'lua> {
|
||||||
|
@ -128,10 +142,33 @@ impl<'lua> Functions<'lua> {
|
||||||
}
|
}
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
let exit_env = lua.create_table_from(vec![
|
||||||
|
(
|
||||||
|
"exit",
|
||||||
|
lua.create_function(|lua, code: Option<u8>| {
|
||||||
|
let code = code.map(ExitCode::from).unwrap_or_default();
|
||||||
|
lua.set_exit_code(code);
|
||||||
|
Ok(())
|
||||||
|
})?,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"yield",
|
||||||
|
lua.globals()
|
||||||
|
.get::<_, LuaTable>("coroutine")?
|
||||||
|
.get::<_, LuaFunction>("yield")?,
|
||||||
|
),
|
||||||
|
])?;
|
||||||
|
let exit = lua
|
||||||
|
.load(EXIT_IMPL_LUA)
|
||||||
|
.set_name("=__runtime_exit")
|
||||||
|
.set_environment(exit_env)
|
||||||
|
.into_function()?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
spawn,
|
spawn,
|
||||||
defer,
|
defer,
|
||||||
cancel,
|
cancel,
|
||||||
|
exit,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue