make scheduler into a crate feature

This commit is contained in:
Quaternions 2024-10-16 20:26:59 -07:00
parent d4b67e93dd
commit cb70c60e93
3 changed files with 11 additions and 0 deletions

View File

@ -7,6 +7,10 @@ license = "MIT OR Apache-2.0"
description = "Run embedded Luau scripts which manipulate the DOM." description = "Run embedded Luau scripts which manipulate the DOM."
authors = ["Rhys Lloyd <krakow20@gmail.com>"] authors = ["Rhys Lloyd <krakow20@gmail.com>"]
[features]
default=["run-service"]
run-service=[]
[dependencies] [dependencies]
glam = "0.29.0" glam = "0.29.0"
mlua = { version = "0.10.0-beta", features = ["luau"] } mlua = { version = "0.10.0-beta", features = ["luau"] }

View File

@ -1,5 +1,6 @@
pub mod runner; pub mod runner;
pub mod context; pub mod context;
#[cfg(feature="run-service")]
pub(crate) mod scheduler; pub(crate) mod scheduler;
#[cfg(test)] #[cfg(test)]

View File

@ -1,4 +1,5 @@
use crate::context::Context; use crate::context::Context;
#[cfg(feature="run-service")]
use crate::scheduler::scheduler_mut; use crate::scheduler::scheduler_mut;
pub struct Runner{ pub struct Runner{
@ -30,6 +31,7 @@ fn init(lua:&mlua::Lua)->mlua::Result<()>{
//global environment //global environment
let globals=lua.globals(); let globals=lua.globals();
#[cfg(feature="run-service")]
crate::scheduler::set_globals(lua,&globals)?; crate::scheduler::set_globals(lua,&globals)?;
super::r#enum::set_globals(lua,&globals)?; super::r#enum::set_globals(lua,&globals)?;
super::color3::set_globals(lua,&globals)?; super::color3::set_globals(lua,&globals)?;
@ -62,6 +64,7 @@ impl Runner{
} }
//this makes set_app_data shut up about the lifetime //this makes set_app_data shut up about the lifetime
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)}); self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
#[cfg(feature="run-service")]
self.lua.set_app_data::<crate::scheduler::Scheduler>(crate::scheduler::Scheduler::default()); self.lua.set_app_data::<crate::scheduler::Scheduler>(crate::scheduler::Scheduler::default());
Ok(Runnable{ Ok(Runnable{
lua:self.lua, lua:self.lua,
@ -78,6 +81,7 @@ pub struct Runnable<'a>{
impl Runnable<'_>{ impl Runnable<'_>{
pub fn drop_context(self)->Runner{ pub fn drop_context(self)->Runner{
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>(); self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
#[cfg(feature="run-service")]
self.lua.remove_app_data::<crate::scheduler::Scheduler>(); self.lua.remove_app_data::<crate::scheduler::Scheduler>();
Runner{ Runner{
lua:self.lua, lua:self.lua,
@ -95,11 +99,13 @@ impl Runnable<'_>{
// No need to schedule the thread here // No need to schedule the thread here
Ok(()) Ok(())
} }
#[cfg(feature="run-service")]
pub fn has_scheduled_threads(&self)->Result<bool,mlua::Error>{ pub fn has_scheduled_threads(&self)->Result<bool,mlua::Error>{
scheduler_mut(&self.lua,|scheduler| scheduler_mut(&self.lua,|scheduler|
Ok(scheduler.has_scheduled_threads()) Ok(scheduler.has_scheduled_threads())
) )
} }
#[cfg(feature="run-service")]
pub fn game_tick(&self)->Result<(),mlua::Error>{ pub fn game_tick(&self)->Result<(),mlua::Error>{
if let Some(threads)=scheduler_mut(&self.lua,|scheduler|Ok(scheduler.tick_threads()))?{ if let Some(threads)=scheduler_mut(&self.lua,|scheduler|Ok(scheduler.tick_threads()))?{
for thread in threads{ for thread in threads{