Pass arguments to coroutine in coroutine.resume (#86)

This commit is contained in:
David C 2023-08-14 13:52:53 -07:00 committed by GitHub
parent c86190def1
commit 72adb2172f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View file

@ -117,7 +117,7 @@ fn coroutine_status<'a>(
fn coroutine_resume<'lua>(
lua: &'lua Lua,
value: LuaThreadOrTaskReference,
(value, arguments): (LuaThreadOrTaskReference, LuaMultiValue<'lua>),
) -> LuaResult<(bool, LuaMultiValue<'lua>)> {
let sched = lua.app_data_ref::<&TaskScheduler>().unwrap();
if sched.current_task().is_none() {
@ -128,10 +128,10 @@ fn coroutine_resume<'lua>(
let current = sched.current_task().unwrap();
let result = match value {
LuaThreadOrTaskReference::Thread(t) => {
let task = sched.create_task(TaskKind::Instant, t, None, true)?;
let task = sched.create_task(TaskKind::Instant, t, Some(arguments), true)?;
sched.resume_task(task, None)
}
LuaThreadOrTaskReference::TaskReference(t) => sched.resume_task(t, None),
LuaThreadOrTaskReference::TaskReference(t) => sched.resume_task(t, Some(Ok(arguments))),
};
sched.force_set_current_task(Some(current));
match result {

View file

@ -74,3 +74,13 @@ end)()
assert(not flag2, "Wait failed while inside wrap (1)")
task.wait(0.2)
assert(flag2, "Wait failed while inside wrap (2)")
-- Coroutines should be passed arguments on initial resume
local co = coroutine.create(function(a, b, c)
assert(a == 1)
assert(b == "Hello, world!")
assert(c == true)
end)
coroutine.resume(co, 1, "Hello, world!", true)