From d4b67e93dd55956c20325b778e07b18e0761ec30 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 16 Oct 2024 19:54:10 -0700 Subject: [PATCH] run script as a thread --- src/runner/runner.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 4c6cedb..2bffed5 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -85,10 +85,15 @@ impl Runnable<'_>{ } 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)?; - self.lua.globals().set("script",script).map_err(Error::RustLua)?; - self.lua.load(source.as_str()) - .set_name(name) - .exec().map_err(|error|Error::Lua{source,error}) + self.lua.globals().raw_set("script",script).map_err(Error::RustLua)?; + let f=self.lua.load(source.as_str()) + .set_name(name).into_function().map_err(Error::RustLua)?; + // TODO: set_environment without losing the ability to print from Lua + let thread=self.lua.create_thread(f).map_err(Error::RustLua)?; + thread.resume::(()).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{ scheduler_mut(&self.lua,|scheduler|