mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-18 10:53:45 +01:00
Clean up lib and only export necessities
This commit is contained in:
parent
c6c4c2fd40
commit
34529c0235
7 changed files with 60 additions and 59 deletions
|
@ -4,13 +4,13 @@ type ErrorCallback = Box<dyn for<'lua> Fn(&'lua Lua, LuaThread<'lua>, LuaError)
|
||||||
type ValueCallback = Box<dyn for<'lua> Fn(&'lua Lua, LuaThread<'lua>, LuaValue<'lua>) + 'static>;
|
type ValueCallback = Box<dyn for<'lua> Fn(&'lua Lua, LuaThread<'lua>, LuaValue<'lua>) + 'static>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ThreadCallbacks {
|
pub struct Callbacks {
|
||||||
on_error: Option<ErrorCallback>,
|
on_error: Option<ErrorCallback>,
|
||||||
on_value: Option<ValueCallback>,
|
on_value: Option<ValueCallback>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThreadCallbacks {
|
impl Callbacks {
|
||||||
pub fn new() -> ThreadCallbacks {
|
pub fn new() -> Callbacks {
|
||||||
Default::default()
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
13
lib/lib.rs
13
lib/lib.rs
|
@ -1,7 +1,12 @@
|
||||||
pub mod thread_callbacks;
|
mod callbacks;
|
||||||
pub mod thread_runtime;
|
mod runtime;
|
||||||
pub mod thread_storage;
|
mod storage;
|
||||||
pub mod thread_util;
|
mod traits;
|
||||||
|
mod util;
|
||||||
|
|
||||||
pub use mlua;
|
pub use mlua;
|
||||||
pub use smol;
|
pub use smol;
|
||||||
|
|
||||||
|
pub use callbacks::Callbacks;
|
||||||
|
pub use runtime::Runtime;
|
||||||
|
pub use traits::IntoLuaThread;
|
||||||
|
|
|
@ -10,12 +10,10 @@ use smol::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
thread_callbacks::ThreadCallbacks,
|
callbacks::Callbacks, storage::ThreadWithArgs, traits::IntoLuaThread, util::LuaThreadOrFunction,
|
||||||
thread_storage::ThreadWithArgs,
|
|
||||||
thread_util::{IntoLuaThread, LuaThreadOrFunction},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct ThreadRuntime {
|
pub struct Runtime {
|
||||||
queue_status: Rc<Cell<bool>>,
|
queue_status: Rc<Cell<bool>>,
|
||||||
queue_spawn: Rc<Mutex<Vec<ThreadWithArgs>>>,
|
queue_spawn: Rc<Mutex<Vec<ThreadWithArgs>>>,
|
||||||
queue_defer: Rc<Mutex<Vec<ThreadWithArgs>>>,
|
queue_defer: Rc<Mutex<Vec<ThreadWithArgs>>>,
|
||||||
|
@ -23,13 +21,13 @@ pub struct ThreadRuntime {
|
||||||
rx: Receiver<()>,
|
rx: Receiver<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ThreadRuntime {
|
impl Runtime {
|
||||||
/**
|
/**
|
||||||
Creates a new runtime for the given Lua state.
|
Creates a new runtime for the given Lua state.
|
||||||
|
|
||||||
This will inject some functions to interact with the scheduler / executor.
|
This will inject some functions to interact with the scheduler / executor.
|
||||||
*/
|
*/
|
||||||
pub fn new(lua: &Lua) -> LuaResult<ThreadRuntime> {
|
pub fn new(lua: &Lua) -> LuaResult<Runtime> {
|
||||||
let queue_status = Rc::new(Cell::new(false));
|
let queue_status = Rc::new(Cell::new(false));
|
||||||
let queue_spawn = Rc::new(Mutex::new(Vec::new()));
|
let queue_spawn = Rc::new(Mutex::new(Vec::new()));
|
||||||
let queue_defer = Rc::new(Mutex::new(Vec::new()));
|
let queue_defer = Rc::new(Mutex::new(Vec::new()));
|
||||||
|
@ -65,8 +63,8 @@ impl ThreadRuntime {
|
||||||
LuaError::runtime("Tried to spawn thread to a dropped queue")
|
LuaError::runtime("Tried to spawn thread to a dropped queue")
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
Ok(v) => ThreadCallbacks::forward_value(lua, thread.clone(), v),
|
Ok(v) => Callbacks::forward_value(lua, thread.clone(), v),
|
||||||
Err(e) => ThreadCallbacks::forward_error(lua, thread.clone(), e),
|
Err(e) => Callbacks::forward_error(lua, thread.clone(), e),
|
||||||
}
|
}
|
||||||
Ok(thread)
|
Ok(thread)
|
||||||
} else {
|
} else {
|
||||||
|
@ -101,7 +99,7 @@ impl ThreadRuntime {
|
||||||
lua.globals().set("spawn", fn_spawn)?;
|
lua.globals().set("spawn", fn_spawn)?;
|
||||||
lua.globals().set("defer", fn_defer)?;
|
lua.globals().set("defer", fn_defer)?;
|
||||||
|
|
||||||
Ok(ThreadRuntime {
|
Ok(Runtime {
|
||||||
queue_status,
|
queue_status,
|
||||||
queue_spawn,
|
queue_spawn,
|
||||||
queue_defer,
|
queue_defer,
|
||||||
|
@ -182,8 +180,8 @@ impl ThreadRuntime {
|
||||||
// drop it right away to clear stack space since detached tasks dont drop
|
// drop it right away to clear stack space since detached tasks dont drop
|
||||||
// until the executor drops https://github.com/smol-rs/smol/issues/294
|
// until the executor drops https://github.com/smol-rs/smol/issues/294
|
||||||
match stream.next().await.unwrap() {
|
match stream.next().await.unwrap() {
|
||||||
Ok(v) => ThreadCallbacks::forward_value(lua, thread, v),
|
Ok(v) => Callbacks::forward_value(lua, thread, v),
|
||||||
Err(e) => ThreadCallbacks::forward_error(lua, thread, e),
|
Err(e) => Callbacks::forward_error(lua, thread, e),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
|
@ -1,7 +1,7 @@
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ThreadWithArgs {
|
pub(crate) struct ThreadWithArgs {
|
||||||
key_thread: LuaRegistryKey,
|
key_thread: LuaRegistryKey,
|
||||||
key_args: LuaRegistryKey,
|
key_args: LuaRegistryKey,
|
||||||
}
|
}
|
34
lib/traits.rs
Normal file
34
lib/traits.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
use mlua::prelude::*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
Trait for any struct that can be turned into an [`LuaThread`]
|
||||||
|
and passed to the runtime, implemented for the following types:
|
||||||
|
|
||||||
|
- Lua threads ([`LuaThread`])
|
||||||
|
- Lua functions ([`LuaFunction`])
|
||||||
|
- Lua chunks ([`LuaChunk`])
|
||||||
|
*/
|
||||||
|
pub trait IntoLuaThread<'lua> {
|
||||||
|
/**
|
||||||
|
Converts the value into a Lua thread.
|
||||||
|
*/
|
||||||
|
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> {
|
||||||
|
fn into_lua_thread(self, _: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> {
|
||||||
|
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
||||||
|
lua.create_thread(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'lua> IntoLuaThread<'lua> for LuaChunk<'lua, '_> {
|
||||||
|
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
||||||
|
lua.create_thread(self.into_function()?)
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ use mlua::prelude::*;
|
||||||
[`LuaThreadOrFunction::into_thread`] may be used to convert the value into a Lua thread.
|
[`LuaThreadOrFunction::into_thread`] may be used to convert the value into a Lua thread.
|
||||||
*/
|
*/
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum LuaThreadOrFunction<'lua> {
|
pub(crate) enum LuaThreadOrFunction<'lua> {
|
||||||
Thread(LuaThread<'lua>),
|
Thread(LuaThread<'lua>),
|
||||||
Function(LuaFunction<'lua>),
|
Function(LuaFunction<'lua>),
|
||||||
}
|
}
|
||||||
|
@ -33,36 +33,3 @@ impl<'lua> FromLua<'lua> for LuaThreadOrFunction<'lua> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
Trait for any struct that can be turned into an [`LuaThread`]
|
|
||||||
and given to the scheduler, implemented for the following types:
|
|
||||||
|
|
||||||
- Lua threads ([`LuaThread`])
|
|
||||||
- Lua functions ([`LuaFunction`])
|
|
||||||
- Lua chunks ([`LuaChunk`])
|
|
||||||
*/
|
|
||||||
pub trait IntoLuaThread<'lua> {
|
|
||||||
/**
|
|
||||||
Converts the value into a Lua thread.
|
|
||||||
*/
|
|
||||||
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'lua> IntoLuaThread<'lua> for LuaThread<'lua> {
|
|
||||||
fn into_lua_thread(self, _: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
|
||||||
Ok(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'lua> IntoLuaThread<'lua> for LuaFunction<'lua> {
|
|
||||||
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
|
||||||
lua.create_thread(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'lua, 'a> IntoLuaThread<'lua> for LuaChunk<'lua, 'a> {
|
|
||||||
fn into_lua_thread(self, lua: &'lua Lua) -> LuaResult<LuaThread<'lua>> {
|
|
||||||
lua.create_thread(self.into_function()?)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,12 +1,9 @@
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use smol_mlua::{mlua::prelude::*, smol::*, Callbacks, Runtime};
|
||||||
use smol::*;
|
|
||||||
|
|
||||||
const MAIN_SCRIPT: &str = include_str!("./main.luau");
|
const MAIN_SCRIPT: &str = include_str!("./main.luau");
|
||||||
|
|
||||||
use smol_mlua::{thread_callbacks::ThreadCallbacks, thread_runtime::ThreadRuntime};
|
|
||||||
|
|
||||||
pub fn main() -> LuaResult<()> {
|
pub fn main() -> LuaResult<()> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
|
@ -23,12 +20,12 @@ pub fn main() -> LuaResult<()> {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
// Set up runtime (thread queue / async executors)
|
// Set up runtime (thread queue / async executors)
|
||||||
let rt = ThreadRuntime::new(&lua)?;
|
let rt = Runtime::new(&lua)?;
|
||||||
let main = rt.push_main(&lua, lua.load(MAIN_SCRIPT), ());
|
let main = rt.push_main(&lua, lua.load(MAIN_SCRIPT), ());
|
||||||
lua.set_named_registry_value("main", main)?;
|
lua.set_named_registry_value("main", main)?;
|
||||||
|
|
||||||
// Add callbacks to capture resulting value/error of main thread
|
// Add callbacks to capture resulting value/error of main thread
|
||||||
ThreadCallbacks::new()
|
Callbacks::new()
|
||||||
.on_value(|lua, thread, val| {
|
.on_value(|lua, thread, val| {
|
||||||
let main = lua.named_registry_value::<LuaThread>("main").unwrap();
|
let main = lua.named_registry_value::<LuaThread>("main").unwrap();
|
||||||
if main == thread {
|
if main == thread {
|
||||||
|
|
Loading…
Add table
Reference in a new issue