Skip to content

Commit c0f86cc

Browse files
committed
fixes
1 parent d629ee5 commit c0f86cc

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

definitions/task.luau

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ local time = require("./time")
33

44
local task = {}
55

6-
function task.defer()
6+
function task.spawn<T..., U...>(routine: ((T...) -> U...) | thread, ...: T...): thread
77
error("unimplemented")
88
end
99

10-
function task.wait(dur: (number | time.Duration)?): number
10+
function task.defer<T..., U...>(routine: ((T...) -> U...) | thread, ...: T...): thread
1111
error("unimplemented")
1212
end
1313

14-
function task.spawn<T..., U...>(routine: ((T...) -> U...) | thread, ...: T...): thread
14+
function task.resume(thread: thread): thread
1515
error("unimplemented")
1616
end
1717

18-
function task.resume(thread: thread): thread
18+
function task.deferSelf()
19+
error("unimplemented")
20+
end
21+
22+
function task.wait(dur: (number | time.Duration)?): number
1923
error("unimplemented")
2024
end
2125

examples/coroutine_error_isolation.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local task = require("@lute/task")
22

33
local function loop()
44
while true do
5-
task.defer()
5+
task.deferSelf()
66

77
-- It may be useful to remove this print because it gets spammed, or implement another way
88
-- in which you can determinatively tell whether or not the thread is still active despite
@@ -13,7 +13,7 @@ end
1313

1414
local function otherloop()
1515
while true do
16-
task.defer()
16+
task.deferSelf()
1717

1818
if math.random(1, 2) == 1 then
1919
error("oops")

lute/runtime/src/runtime.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ RuntimeStep Runtime::runOnce()
9393

9494
int status = LUA_OK;
9595

96-
int co_status = lua_costatus(GL, L);
97-
9896
// It's possible for a spawned task to be killed by a coroutine.close()
9997
// before it gets processed in the runningThreads queue. This leads to situations where a thread was scheduled to resume
10098
// but has already been killed.
@@ -108,7 +106,7 @@ RuntimeStep Runtime::runOnce()
108106
// 6) We can just step over it, because
109107
// a) if it scheduled a resume, the corresponding pending token will have been cleared
110108
// b) the corresponding ref for the lua state will be freed at the end of Runtime::runOnce()
111-
109+
int co_status = lua_costatus(GL, L);
112110
if (co_status == LUA_COFIN)
113111
{
114112
return StepSuccess{L};

lute/std/libs/task.luau

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function tasklib.await(t: task): any
3333
end
3434

3535
while t.success == nil do
36-
task.defer()
36+
task.deferSelf()
3737
end
3838

3939
if t.success then
@@ -59,7 +59,7 @@ function tasklib.awaitall(...: task): ...unknown
5959
for _, v in tasks do
6060
if v.success == nil then
6161
done = false
62-
task.defer()
62+
task.deferSelf()
6363
end
6464
end
6565
end

lute/task/include/lute/task.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ namespace task
1212
{
1313

1414
int lua_defer(lua_State* L);
15+
int lua_deferSelf(lua_State* L);
1516
int lua_wait(lua_State* L);
1617
int lua_spawn(lua_State* L);
1718
int lute_resume(lua_State* L);
1819
int lua_delay(lua_State* L);
1920

2021
static const luaL_Reg lib[] = {
22+
{"spawn", lua_spawn},
2123
{"defer", lua_defer},
24+
{"resume", lute_resume},
25+
{"deferSelf", lua_deferSelf},
2226
{"wait", lua_wait},
23-
{"spawn", lua_spawn},
2427
{"delay", lua_delay},
25-
26-
{"resume", lute_resume},
27-
2828
{nullptr, nullptr},
2929
};
3030

lute/task/src/task.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ struct LuaThread
9999
, runtime(getRuntime(parent))
100100
{
101101
// At this point, the lua stack should look like:
102-
// [function, (args...]
102+
// [function, args...]
103103
// or
104104
// [thread, args...]
105105

106106
// If passed a thread, we push the 0 or more arguments onto that threads stack and resume it.
107107
// If passed a function, we push the function and the 0 or more arguments onto a newly created thread's stack
108108
int toPush = lua_gettop(parent);
109109
// We only want to resume with the actual number of arguments here, regardless of if the first argument is a thread or a function
110-
int numResumeArgs = numResumeArgs = toPush > 1 ? toPush - 1 : 0;
110+
int numResumeArgs = toPush > 1 ? toPush - 1 : 0;
111111
if (!lua_checkstack(L, 1))
112112
luaL_error(L, "Not enough stack space to create a new thread");
113113
if (lua_isfunction(parent, 1))
@@ -188,14 +188,34 @@ int lua_spawn(lua_State* L)
188188
return newThread.resume();
189189
}
190190

191+
int lua_deferSelf(lua_State* L)
192+
{
193+
194+
if (lua_gettop(L) != 0)
195+
{
196+
luaL_error(L, "task.deferSelf does not take any arguments");
197+
return 0;
198+
}
199+
lua_pushthread(L);
200+
LuaThread newThread{L};
201+
newThread.defer();
202+
return lua_yield(L, 0);
203+
}
204+
191205
int lua_defer(lua_State* L)
192206
{
207+
193208
LuaThread newThread{L};
194209
return newThread.defer();
195210
}
196211

197212
int lute_resume(lua_State* L)
198213
{
214+
if (!lua_isthread(L, 1))
215+
{
216+
luaL_error(L, "Expected a thread as the first argument.");
217+
return 0;
218+
}
199219
LuaThread newThread{L};
200220
return newThread.resume();
201221
}

tests/lute/task.test.luau

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test.suite("LuteTaskSuite", function(suite)
4747

4848
-- task.spawn with function, >0 args
4949
suite:case("taskSpawnFunctionWithArgsCoroutineStatus", function(assert)
50-
local thread = task.spawn(function(a, b, c)
50+
local thread = task.spawn(function(_a, _b, _c)
5151
task.wait(2)
5252
end, "hello", "world", "boom")
5353

@@ -58,7 +58,7 @@ test.suite("LuteTaskSuite", function(suite)
5858

5959
-- task.spawn with thread, >0 args
6060
suite:case("taskSpawnThreadWithArgsCoroutineStatus", function(assert)
61-
local co = coroutine.create(function(a, b, c)
61+
local co = coroutine.create(function(_a, _b, _c)
6262
task.wait(2)
6363
end)
6464
local thread = task.spawn(co, "hello", "world", "boom")
@@ -93,7 +93,7 @@ test.suite("LuteTaskSuite", function(suite)
9393

9494
-- task.defer with function, >0 args
9595
suite:case("taskDeferFunctionWithArgsCoroutineStatus", function(assert)
96-
local thread = task.defer(function(a, b, c)
96+
local thread = task.defer(function(_a, _b, _c)
9797
task.wait(2)
9898
end, "hello", "world", "boom")
9999

@@ -104,7 +104,7 @@ test.suite("LuteTaskSuite", function(suite)
104104

105105
-- task.defer with thread, >0 args
106106
suite:case("taskDeferThreadWithArgsCoroutineStatus", function(assert)
107-
local co = coroutine.create(function(a, b, c)
107+
local co = coroutine.create(function(_a, _b, _c)
108108
task.wait(2)
109109
end)
110110
local thread = task.defer(co, "hello", "world", "boom")
@@ -128,7 +128,7 @@ test.suite("LuteTaskSuite", function(suite)
128128

129129
-- task.resume with thread, >0 args
130130
suite:case("taskResumeThreadWithArgsCoroutineStatus", function(assert)
131-
local co = coroutine.create(function(a, b, c)
131+
local co = coroutine.create(function(_a, _b, _c)
132132
task.wait(2)
133133
end)
134134
local thread = task.resume(co, "hello", "world", "boom")

0 commit comments

Comments
 (0)