2021-10-30 04:25:12 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
// This code is based on Lua 5.x implementation licensed under MIT License; see lua_LICENSE.txt for details
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
#include "lstate.h"
|
|
|
|
#include "lvm.h"
|
|
|
|
|
2022-08-05 06:35:33 +08:00
|
|
|
#define CO_RUN 0 // running
|
|
|
|
#define CO_SUS 1 // suspended
|
|
|
|
#define CO_NOR 2 // 'normal' (it resumed another coroutine)
|
2021-10-30 04:25:12 +08:00
|
|
|
#define CO_DEAD 3
|
|
|
|
|
|
|
|
#define CO_STATUS_ERROR -1
|
|
|
|
#define CO_STATUS_BREAK -2
|
|
|
|
|
|
|
|
static const char* const statnames[] = {"running", "suspended", "normal", "dead"};
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int auxstatus(lua_State* L, lua_State* co)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
if (co == L)
|
|
|
|
return CO_RUN;
|
|
|
|
if (co->status == LUA_YIELD)
|
|
|
|
return CO_SUS;
|
|
|
|
if (co->status == LUA_BREAK)
|
|
|
|
return CO_NOR;
|
2022-08-05 06:35:33 +08:00
|
|
|
if (co->status != 0) // some error occurred
|
2021-10-30 04:25:12 +08:00
|
|
|
return CO_DEAD;
|
2022-08-05 06:35:33 +08:00
|
|
|
if (co->ci != co->base_ci) // does it have frames?
|
2021-10-30 04:25:12 +08:00
|
|
|
return CO_NOR;
|
|
|
|
if (co->top == co->base)
|
|
|
|
return CO_DEAD;
|
2022-08-05 06:35:33 +08:00
|
|
|
return CO_SUS; // initial state
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int costatus(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, 1);
|
|
|
|
luaL_argexpected(L, co, 1, "thread");
|
2021-11-05 23:47:21 +08:00
|
|
|
lua_pushstring(L, statnames[auxstatus(L, co)]);
|
2021-10-30 04:25:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int auxresume(lua_State* L, lua_State* co, int narg)
|
|
|
|
{
|
|
|
|
// error handling for edge cases
|
|
|
|
if (co->status != LUA_YIELD)
|
|
|
|
{
|
2021-11-05 23:47:21 +08:00
|
|
|
int status = auxstatus(L, co);
|
2021-10-30 04:25:12 +08:00
|
|
|
if (status != CO_SUS)
|
|
|
|
{
|
|
|
|
lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
|
|
|
|
return CO_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (narg)
|
|
|
|
{
|
|
|
|
if (!lua_checkstack(co, narg))
|
|
|
|
luaL_error(L, "too many arguments to resume");
|
|
|
|
lua_xmove(L, co, narg);
|
|
|
|
}
|
|
|
|
|
|
|
|
co->singlestep = L->singlestep;
|
|
|
|
|
|
|
|
int status = lua_resume(co, L, narg);
|
|
|
|
if (status == 0 || status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
int nres = cast_int(co->top - co->base);
|
|
|
|
if (nres)
|
|
|
|
{
|
2022-08-05 06:35:33 +08:00
|
|
|
// +1 accounts for true/false status in resumefinish
|
2021-10-30 04:25:12 +08:00
|
|
|
if (nres + 1 > LUA_MINSTACK && !lua_checkstack(L, nres + 1))
|
|
|
|
luaL_error(L, "too many results to resume");
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_xmove(co, L, nres); // move yielded values
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
return nres;
|
|
|
|
}
|
|
|
|
else if (status == LUA_BREAK)
|
|
|
|
{
|
|
|
|
return CO_STATUS_BREAK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_xmove(co, L, 1); // move error message
|
2021-10-30 04:25:12 +08:00
|
|
|
return CO_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int interruptThread(lua_State* L, lua_State* co)
|
|
|
|
{
|
|
|
|
// notify the debugger that the thread was suspended
|
|
|
|
if (L->global->cb.debuginterrupt)
|
|
|
|
luau_callhook(L, L->global->cb.debuginterrupt, co);
|
|
|
|
|
|
|
|
return lua_break(L);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int auxresumecont(lua_State* L, lua_State* co)
|
|
|
|
{
|
|
|
|
if (co->status == 0 || co->status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
int nres = cast_int(co->top - co->base);
|
|
|
|
if (!lua_checkstack(L, nres + 1))
|
|
|
|
luaL_error(L, "too many results to resume");
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_xmove(co, L, nres); // move yielded values
|
2021-10-30 04:25:12 +08:00
|
|
|
return nres;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_rawcheckstack(L, 2);
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_xmove(co, L, 1); // move error message
|
2021-10-30 04:25:12 +08:00
|
|
|
return CO_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int coresumefinish(lua_State* L, int r)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
lua_insert(L, -2);
|
2022-08-05 06:35:33 +08:00
|
|
|
return 2; // return false + error message
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, 1);
|
|
|
|
lua_insert(L, -(r + 1));
|
2022-08-05 06:35:33 +08:00
|
|
|
return r + 1; // return true + `resume' returns
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int coresumey(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, 1);
|
|
|
|
luaL_argexpected(L, co, 1, "thread");
|
|
|
|
int narg = cast_int(L->top - L->base) - 1;
|
|
|
|
int r = auxresume(L, co, narg);
|
|
|
|
|
|
|
|
if (r == CO_STATUS_BREAK)
|
|
|
|
return interruptThread(L, co);
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
return coresumefinish(L, r);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int coresumecont(lua_State* L, int status)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, 1);
|
|
|
|
luaL_argexpected(L, co, 1, "thread");
|
|
|
|
|
|
|
|
// if coroutine still hasn't yielded after the break, break current thread again
|
|
|
|
if (co->status == LUA_BREAK)
|
|
|
|
return interruptThread(L, co);
|
|
|
|
|
|
|
|
int r = auxresumecont(L, co);
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
return coresumefinish(L, r);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int auxwrapfinish(lua_State* L, int r)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
if (lua_isstring(L, -1))
|
2022-08-05 06:35:33 +08:00
|
|
|
{ // error object is a string?
|
|
|
|
luaL_where(L, 1); // add extra info
|
2021-10-30 04:25:12 +08:00
|
|
|
lua_insert(L, -2);
|
|
|
|
lua_concat(L, 2);
|
|
|
|
}
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_error(L); // propagate error
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int auxwrapy(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, lua_upvalueindex(1));
|
|
|
|
int narg = cast_int(L->top - L->base);
|
|
|
|
int r = auxresume(L, co, narg);
|
|
|
|
|
|
|
|
if (r == CO_STATUS_BREAK)
|
|
|
|
return interruptThread(L, co);
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
return auxwrapfinish(L, r);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int auxwrapcont(lua_State* L, int status)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, lua_upvalueindex(1));
|
|
|
|
|
|
|
|
// if coroutine still hasn't yielded after the break, break current thread again
|
|
|
|
if (co->status == LUA_BREAK)
|
|
|
|
return interruptThread(L, co);
|
|
|
|
|
|
|
|
int r = auxresumecont(L, co);
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
return auxwrapfinish(L, r);
|
2021-10-30 04:25:12 +08:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int cocreate(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
|
|
|
lua_State* NL = lua_newthread(L);
|
2021-11-05 23:47:21 +08:00
|
|
|
lua_xpush(L, NL, 1); // push function on top of NL
|
2021-10-30 04:25:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int cowrap(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
2021-11-05 23:47:21 +08:00
|
|
|
cocreate(L);
|
2021-10-30 04:25:12 +08:00
|
|
|
|
2021-11-20 00:10:07 +08:00
|
|
|
lua_pushcclosurek(L, auxwrapy, NULL, 1, auxwrapcont);
|
2021-10-30 04:25:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int coyield(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
int nres = cast_int(L->top - L->base);
|
|
|
|
return lua_yield(L, nres);
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int corunning(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
if (lua_pushthread(L))
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_pushnil(L); // main thread is not a coroutine
|
2021-10-30 04:25:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-05 23:47:21 +08:00
|
|
|
static int coyieldable(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
lua_pushboolean(L, lua_isyieldable(L));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-11-20 00:10:07 +08:00
|
|
|
static int coclose(lua_State* L)
|
|
|
|
{
|
|
|
|
lua_State* co = lua_tothread(L, 1);
|
|
|
|
luaL_argexpected(L, co, 1, "thread");
|
|
|
|
|
|
|
|
int status = auxstatus(L, co);
|
|
|
|
if (status != CO_DEAD && status != CO_SUS)
|
|
|
|
luaL_error(L, "cannot close %s coroutine", statnames[status]);
|
|
|
|
|
|
|
|
if (co->status == LUA_OK || co->status == LUA_YIELD)
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, true);
|
|
|
|
lua_resetthread(co);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
if (lua_gettop(co))
|
2022-08-05 06:35:33 +08:00
|
|
|
lua_xmove(co, L, 1); // move error message
|
2021-11-20 00:10:07 +08:00
|
|
|
lua_resetthread(co);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-30 04:25:12 +08:00
|
|
|
static const luaL_Reg co_funcs[] = {
|
2021-11-05 23:47:21 +08:00
|
|
|
{"create", cocreate},
|
|
|
|
{"running", corunning},
|
|
|
|
{"status", costatus},
|
|
|
|
{"wrap", cowrap},
|
|
|
|
{"yield", coyield},
|
|
|
|
{"isyieldable", coyieldable},
|
2021-11-20 00:10:07 +08:00
|
|
|
{"close", coclose},
|
2021-10-30 04:25:12 +08:00
|
|
|
{NULL, NULL},
|
|
|
|
};
|
|
|
|
|
2021-11-30 00:14:06 +08:00
|
|
|
int luaopen_coroutine(lua_State* L)
|
2021-10-30 04:25:12 +08:00
|
|
|
{
|
|
|
|
luaL_register(L, LUA_COLIBNAME, co_funcs);
|
|
|
|
|
2021-11-20 00:10:07 +08:00
|
|
|
lua_pushcclosurek(L, coresumey, "resume", 0, coresumecont);
|
2021-10-30 04:25:12 +08:00
|
|
|
lua_setfield(L, -2, "resume");
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|