scheduler code cleanup
This commit is contained in:
parent
bffc5bb8f1
commit
da169ade70
@ -61,45 +61,56 @@ pub fn scheduler_mut<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut crate::scheduler::S
|
|||||||
let mut scheduler=lua.app_data_mut::<crate::scheduler::Scheduler>().ok_or(mlua::Error::runtime("Scheduler missing"))?;
|
let mut scheduler=lua.app_data_mut::<crate::scheduler::Scheduler>().ok_or(mlua::Error::runtime("Scheduler missing"))?;
|
||||||
f(&mut *scheduler)
|
f(&mut *scheduler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn schedule_thread(lua:&mlua::Lua,dt:mlua::Value)->Result<(),mlua::Error>{
|
||||||
|
let delay=match dt{
|
||||||
|
mlua::Value::Integer(i)=>i.max(0) as u64*60,
|
||||||
|
mlua::Value::Number(f)=>{
|
||||||
|
let delay=f.max(0.0)*60.0;
|
||||||
|
match delay.classify(){
|
||||||
|
std::num::FpCategory::Nan=>Err(mlua::Error::runtime("NaN"))?,
|
||||||
|
// cases where the number is too large to schedule
|
||||||
|
std::num::FpCategory::Infinite=>return Ok(()),
|
||||||
|
std::num::FpCategory::Normal=>if (u64::MAX as f64)<delay{
|
||||||
|
return Ok(());
|
||||||
|
},
|
||||||
|
_=>(),
|
||||||
|
}
|
||||||
|
delay as u64
|
||||||
|
},
|
||||||
|
mlua::Value::Nil=>0,
|
||||||
|
_=>Err(mlua::Error::runtime("Expected float"))?,
|
||||||
|
};
|
||||||
|
scheduler_mut(lua,|scheduler|{
|
||||||
|
scheduler.schedule_thread(delay.max(2),lua.current_thread());
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is used to avoid calling coroutine.yield from the rust side.
|
||||||
|
const LUA_WAIT:&str=
|
||||||
|
"local coroutine_yield=coroutine.yield
|
||||||
|
local schedule_thread=schedule_thread
|
||||||
|
return function(dt)
|
||||||
|
schedule_thread(dt)
|
||||||
|
return coroutine_yield()
|
||||||
|
end";
|
||||||
|
|
||||||
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
|
||||||
let schedule_thread=lua.create_function(|lua,dt:mlua::Value|{
|
let coroutine_table=globals.get::<mlua::Table>("coroutine")?;
|
||||||
let delay=match dt{
|
let schedule_thread=lua.create_function(schedule_thread)?;
|
||||||
mlua::Value::Integer(i)=>i.max(0) as u64*60,
|
|
||||||
mlua::Value::Number(f)=>{
|
//create wait function environment
|
||||||
let delay=f.max(0.0)*60.0;
|
|
||||||
match delay.classify(){
|
|
||||||
std::num::FpCategory::Nan=>Err(mlua::Error::runtime("NaN"))?,
|
|
||||||
// cases where the number is too large to schedule
|
|
||||||
std::num::FpCategory::Infinite=>return Ok(()),
|
|
||||||
std::num::FpCategory::Normal=>if (u64::MAX as f64)<delay{
|
|
||||||
return Ok(());
|
|
||||||
},
|
|
||||||
_=>(),
|
|
||||||
}
|
|
||||||
delay as u64
|
|
||||||
},
|
|
||||||
mlua::Value::Nil=>0,
|
|
||||||
_=>Err(mlua::Error::runtime("Expected float"))?,
|
|
||||||
};
|
|
||||||
scheduler_mut(lua,|scheduler|{
|
|
||||||
scheduler.schedule_thread(delay.max(2),lua.current_thread());
|
|
||||||
Ok(())
|
|
||||||
})
|
|
||||||
})?;
|
|
||||||
let wait_env=lua.create_table()?;
|
let wait_env=lua.create_table()?;
|
||||||
wait_env.raw_set("coroutine",globals.get::<mlua::Table>("coroutine")?)?;
|
wait_env.raw_set("coroutine",coroutine_table)?;
|
||||||
wait_env.raw_set("schedule_thread",schedule_thread)?;
|
wait_env.raw_set("schedule_thread",schedule_thread)?;
|
||||||
let wait=lua.load("
|
|
||||||
local coroutine_yield=coroutine.yield
|
//construct wait function from Lua code
|
||||||
local schedule_thread=schedule_thread
|
let wait=lua.load(LUA_WAIT)
|
||||||
return function(dt)
|
|
||||||
schedule_thread(dt)
|
|
||||||
return coroutine_yield()
|
|
||||||
end
|
|
||||||
")
|
|
||||||
.set_name("wait")
|
.set_name("wait")
|
||||||
.set_environment(wait_env)
|
.set_environment(wait_env)
|
||||||
.call::<mlua::Function>(())?;
|
.call::<mlua::Function>(())?;
|
||||||
|
|
||||||
globals.raw_set("wait",wait)?;
|
globals.raw_set("wait",wait)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
Reference in New Issue
Block a user