Fix task.wait breaking after coroutine.resume

This commit is contained in:
Filip Tibell 2023-02-17 19:47:10 +01:00
parent c45c78bdc2
commit dbe6c18d3a
No known key found for this signature in database
2 changed files with 29 additions and 9 deletions

View file

@ -131,16 +131,22 @@ fn coroutine_resume<'lua>(
lua: &'lua Lua, lua: &'lua Lua,
value: LuaThreadOrTaskReference, value: LuaThreadOrTaskReference,
) -> LuaResult<LuaMultiValue<'lua>> { ) -> LuaResult<LuaMultiValue<'lua>> {
// FIXME: Resume should return true, return vals OR false, error message
let sched = lua.app_data_ref::<&TaskScheduler>().unwrap();
match value { match value {
LuaThreadOrTaskReference::Thread(t) => { LuaThreadOrTaskReference::Thread(t) => {
let sched = lua.app_data_ref::<&TaskScheduler>().unwrap(); if sched.current_task().is_none() {
let task = sched.create_task(TaskKind::Instant, t, None, true)?; return Err(LuaError::RuntimeError(
sched.resume_task(task, None) "No current task to inherit".to_string(),
));
} }
LuaThreadOrTaskReference::TaskReference(t) => lua let task = sched.create_task(TaskKind::Instant, t, None, true)?;
.app_data_ref::<&TaskScheduler>() let current = sched.current_task().unwrap();
.unwrap() let result = sched.resume_task(task, None);
.resume_task(t, None), sched.force_set_current_task(Some(current));
result
}
LuaThreadOrTaskReference::TaskReference(t) => sched.resume_task(t, None),
} }
} }
@ -152,8 +158,18 @@ fn coroutine_wrap<'lua>(lua: &'lua Lua, func: LuaFunction) -> LuaResult<LuaFunct
false, false,
)?; )?;
lua.create_function(move |lua, args: LuaMultiValue| { lua.create_function(move |lua, args: LuaMultiValue| {
lua.app_data_ref::<&TaskScheduler>() 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() .unwrap()
.resume_task(task, Some(Ok(args))) .resume_task(task, Some(Ok(args)));
sched.force_set_current_task(Some(current));
result
}) })
} }

View file

@ -98,6 +98,10 @@ impl<'fut> TaskScheduler<'fut> {
self.exit_code.set(Some(code)); self.exit_code.set(Some(code));
} }
pub(crate) fn force_set_current_task(&self, reference: Option<TaskReference>) {
self.tasks_current.set(reference);
}
/** /**
Checks if a task still exists in the scheduler. Checks if a task still exists in the scheduler.
@ -147,7 +151,7 @@ impl<'fut> TaskScheduler<'fut> {
// Create the task ref to use // Create the task ref to use
let guid = if inherit_current_guid { let guid = if inherit_current_guid {
self.current_task() self.current_task()
.expect("No current guid to inherit") .ok_or_else(|| LuaError::RuntimeError("No current guid to inherit".to_string()))?
.id() .id()
} else { } else {
let guid = self.guid.get(); let guid = self.guid.get();