run script as a thread

This commit is contained in:
Quaternions 2024-10-16 19:54:10 -07:00
parent eb66516f83
commit d4b67e93dd

View File

@ -85,10 +85,15 @@ 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().set("script",script).map_err(Error::RustLua)?; self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?;
self.lua.load(source.as_str()) let f=self.lua.load(source.as_str())
.set_name(name) .set_name(name).into_function().map_err(Error::RustLua)?;
.exec().map_err(|error|Error::Lua{source,error}) // TODO: set_environment without losing the ability to print from Lua
let thread=self.lua.create_thread(f).map_err(Error::RustLua)?;
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
// No need to schedule the thread here
Ok(())
} }
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|