diff --git a/packages/lib/src/globals/task.rs b/packages/lib/src/globals/task.rs index fafa398..a788934 100644 --- a/packages/lib/src/globals/task.rs +++ b/packages/lib/src/globals/task.rs @@ -131,16 +131,22 @@ fn coroutine_resume<'lua>( lua: &'lua Lua, value: LuaThreadOrTaskReference, ) -> LuaResult> { + // FIXME: Resume should return true, return vals OR false, error message + let sched = lua.app_data_ref::<&TaskScheduler>().unwrap(); match value { LuaThreadOrTaskReference::Thread(t) => { - let sched = lua.app_data_ref::<&TaskScheduler>().unwrap(); + if sched.current_task().is_none() { + return Err(LuaError::RuntimeError( + "No current task to inherit".to_string(), + )); + } let task = sched.create_task(TaskKind::Instant, t, None, true)?; - sched.resume_task(task, None) + let current = sched.current_task().unwrap(); + let result = sched.resume_task(task, None); + sched.force_set_current_task(Some(current)); + result } - LuaThreadOrTaskReference::TaskReference(t) => lua - .app_data_ref::<&TaskScheduler>() - .unwrap() - .resume_task(t, None), + LuaThreadOrTaskReference::TaskReference(t) => sched.resume_task(t, None), } } @@ -152,8 +158,18 @@ fn coroutine_wrap<'lua>(lua: &'lua Lua, func: LuaFunction) -> LuaResult() + let sched = lua.app_data_ref::<&TaskScheduler>().unwrap(); + if sched.current_task().is_none() { + return Err(LuaError::RuntimeError( + "No current task to inherit".to_string(), + )); + } + let current = sched.current_task().unwrap(); + let result = lua + .app_data_ref::<&TaskScheduler>() .unwrap() - .resume_task(task, Some(Ok(args))) + .resume_task(task, Some(Ok(args))); + sched.force_set_current_task(Some(current)); + result }) } diff --git a/packages/lib/src/lua/task/scheduler.rs b/packages/lib/src/lua/task/scheduler.rs index a7d2463..954ff9f 100644 --- a/packages/lib/src/lua/task/scheduler.rs +++ b/packages/lib/src/lua/task/scheduler.rs @@ -98,6 +98,10 @@ impl<'fut> TaskScheduler<'fut> { self.exit_code.set(Some(code)); } + pub(crate) fn force_set_current_task(&self, reference: Option) { + self.tasks_current.set(reference); + } + /** Checks if a task still exists in the scheduler. @@ -147,7 +151,7 @@ impl<'fut> TaskScheduler<'fut> { // Create the task ref to use let guid = if inherit_current_guid { self.current_task() - .expect("No current guid to inherit") + .ok_or_else(|| LuaError::RuntimeError("No current guid to inherit".to_string()))? .id() } else { let guid = self.guid.get();