Compare commits

..

6 Commits

Author SHA1 Message Date
2c4b58c5ca set_environment makes print disappear 2024-10-16 19:11:19 -07:00
909c00c5f6 game ticks api 2024-10-16 19:11:19 -07:00
3981d25305 why is there no coroutine 2024-10-16 19:11:19 -07:00
442d7af333 wait 2024-10-16 19:11:19 -07:00
4823d73a85 Scheduler lives in Lua 2024-10-16 17:45:51 -07:00
de7a2c22be scheduler 2024-10-16 17:45:51 -07:00

View File

@ -109,7 +109,7 @@ impl Runner{
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,
_lifetime:&std::marker::PhantomData _lifetime:&std::marker::PhantomData,
}) })
} }
} }
@ -117,7 +117,7 @@ impl Runner{
//Runnable is the same thing but has context set, which it holds the lifetime for. //Runnable is the same thing but has context set, which it holds the lifetime for.
pub struct Runnable<'a>{ pub struct Runnable<'a>{
lua:mlua::Lua, lua:mlua::Lua,
_lifetime:&'a std::marker::PhantomData<()> _lifetime:&'a std::marker::PhantomData<()>,
} }
impl Runnable<'_>{ impl Runnable<'_>{
pub fn drop_context(self)->Runner{ pub fn drop_context(self)->Runner{
@ -129,10 +129,19 @@ impl Runnable<'_>{
} }
pub fn run_script(&self,script:super::instance::Instance)->Result<(),Error>{ pub fn run_script(&self,script:super::instance::Instance)->Result<(),Error>{
let (name,source)=super::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?; let (name,source)=super::instance::get_name_source(&self.lua,script).map_err(Error::RustLua)?;
self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?;
let f=self.lua.load(source.as_str()) let f=self.lua.load(source.as_str())
.set_name(name).into_function().map_err(Error::RustLua)?; .set_name(name).into_function().map_err(Error::RustLua)?;
// TODO: set_environment without losing the ability to print from Lua
// let fenv=self.lua.create_table().map_err(Error::RustLua)?;
// //there's gotta be a more concise way to do this
// for pair in f.environment().unwrap().pairs::<mlua::Value,mlua::Value>(){
// let (k,v)=pair.map_err(Error::RustLua)?;
// fenv.raw_set(k,v).map_err(Error::RustLua)?;
// }
// fenv.raw_set("script",script).map_err(Error::RustLua)?;
// f.set_environment(fenv).map_err(Error::RustLua)?;
self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?;
let thread=self.lua.create_thread(f).map_err(Error::RustLua)?; let thread=self.lua.create_thread(f).map_err(Error::RustLua)?;
thread.resume::<mlua::MultiValue>(()).map_err(|error|Error::Lua{source,error})?; thread.resume::<mlua::MultiValue>(()).map_err(|error|Error::Lua{source,error})?;
// wait() is called from inside Lua and goes to a rust function that schedules the thread and then yields // wait() is called from inside Lua and goes to a rust function that schedules the thread and then yields