Fix coroutine.resume

This commit is contained in:
Filip Tibell 2023-01-23 17:39:16 -05:00
parent b1e7b2dd77
commit 8fca650f46
No known key found for this signature in database
2 changed files with 16 additions and 2 deletions

View file

@ -15,6 +15,11 @@ pub fn create(lua: &Lua) -> LuaResult<()> {
let coroutine: LuaTable = lua.globals().raw_get("coroutine")?; let coroutine: LuaTable = lua.globals().raw_get("coroutine")?;
let close: LuaFunction = coroutine.raw_get("close")?; let close: LuaFunction = coroutine.raw_get("close")?;
lua.set_named_registry_value("coroutine.close", close)?; lua.set_named_registry_value("coroutine.close", close)?;
// HACK: coroutine.resume has some weird scheduling issues, but our custom
// task.spawn implementation is more or less a replacement for it, so we
// overwrite the original coroutine.resume function with it to fix that
coroutine.raw_set("resume", lua.create_async_function(task_spawn)?)?;
// Rest of the task library is normal, just async functions, no metatable
lua.globals().raw_set( lua.globals().raw_set(
"task", "task",
TableBuilder::new(lua)? TableBuilder::new(lua)?

View file

@ -41,6 +41,15 @@ task.spawn(function()
task.wait(0.1) task.wait(0.1)
flag = true flag = true
end) end)
assert(not flag, "Wait failed for a task-spawned thread (1)") assert(not flag, "Wait failed while inside task-spawned thread (1)")
task.wait(0.2) task.wait(0.2)
assert(flag, "Wait failed for a task-spawned thread (2)") assert(flag, "Wait failed while inside task-spawned thread (2)")
local flag2: boolean = false
coroutine.resume(coroutine.create(function()
task.wait(0.1)
flag2 = true
end))
assert(not flag2, "Wait failed while inside coroutine (1)")
task.wait(0.2)
assert(flag2, "Wait failed while inside coroutine (2)")