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,
value: LuaThreadOrTaskReference,
) -> LuaResult<LuaMultiValue<'lua>> {
// 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();
let task = sched.create_task(TaskKind::Instant, t, None, true)?;
sched.resume_task(task, None)
if sched.current_task().is_none() {
return Err(LuaError::RuntimeError(
"No current task to inherit".to_string(),
));
}
LuaThreadOrTaskReference::TaskReference(t) => lua
.app_data_ref::<&TaskScheduler>()
.unwrap()
.resume_task(t, None),
let task = sched.create_task(TaskKind::Instant, t, None, true)?;
let current = sched.current_task().unwrap();
let result = sched.resume_task(task, 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,
)?;
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()
.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));
}
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.
@ -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();