From 8fca650f466ca2e06de20c021e77f2b6efd1517e Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Mon, 23 Jan 2023 17:39:16 -0500 Subject: [PATCH] Fix coroutine.resume --- src/lib/globals/task.rs | 5 +++++ src/tests/task/wait.luau | 13 +++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/globals/task.rs b/src/lib/globals/task.rs index c838505..2db9241 100644 --- a/src/lib/globals/task.rs +++ b/src/lib/globals/task.rs @@ -15,6 +15,11 @@ pub fn create(lua: &Lua) -> LuaResult<()> { let coroutine: LuaTable = lua.globals().raw_get("coroutine")?; let close: LuaFunction = coroutine.raw_get("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( "task", TableBuilder::new(lua)? diff --git a/src/tests/task/wait.luau b/src/tests/task/wait.luau index c92c733..9e6efc9 100644 --- a/src/tests/task/wait.luau +++ b/src/tests/task/wait.luau @@ -41,6 +41,15 @@ task.spawn(function() task.wait(0.1) flag = true 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) -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)")