From 3ac9d2f2cdb586b1114dfe92d0f4e08764bf4fe8 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 16 Oct 2024 17:45:41 -0700 Subject: [PATCH] wait --- src/runner/runner.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 06815da..eb3c5df 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -23,12 +23,49 @@ impl std::fmt::Display for Error{ } impl std::error::Error for Error{} +fn coerce_float64(value:&mlua::Value)->Option{ + match value{ + &mlua::Value::Integer(i)=>Some(i as f64), + &mlua::Value::Number(f)=>Some(f), + _=>None, + } +} +fn scheduler_mut(lua:&mlua::Lua,mut f:impl FnMut(&mut crate::scheduler::Scheduler)->mlua::Result)->mlua::Result{ + let mut scheduler=lua.app_data_mut::().ok_or(mlua::Error::runtime("Scheduler missing"))?; + f(&mut *scheduler) +} fn init(lua:&mlua::Lua)->mlua::Result<()>{ lua.sandbox(true)?; //global environment let globals=lua.globals(); + let schedule_thread=lua.create_function(move|lua,dt:mlua::Value|{ + let delay=coerce_float64(&dt).ok_or(mlua::Error::runtime("Expected float"))?.max(0.0)*60.0; + if delay("coroutine")?)?; + wait_env.raw_set("schedule_thread",schedule_thread)?; + let wait=lua.load(" + local coroutine_yield=coroutine.yield + local schedule_thread=schedule_thread + return function(dt) + schedule_thread(dt) + return coroutine_yield() + end + ") + .set_name("wait") + .set_environment(wait_env) + .call::(())?; + globals.raw_set("wait",wait)?; + super::r#enum::set_globals(lua,&globals)?; super::color3::set_globals(lua,&globals)?; super::vector3::set_globals(lua,&globals)?;