Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / client / deps / liblua / lcorolib.c
blob2aedcaea585303456e401b6cb50bb4ce94437723
1 /*
2 ** $Id: lcorolib.c $
3 ** Coroutine Library
4 ** See Copyright Notice in lua.h
5 */
7 #define lcorolib_c
8 #define LUA_LIB
10 #include "lprefix.h"
13 #include <stdlib.h>
15 #include "lua.h"
17 #include "lauxlib.h"
18 #include "lualib.h"
21 static lua_State *getco(lua_State *L) {
22 lua_State *co = lua_tothread(L, 1);
23 luaL_argexpected(L, co, 1, "thread");
24 return co;
29 ** Resumes a coroutine. Returns the number of results for non-error
30 ** cases or -1 for errors.
32 static int auxresume(lua_State *L, lua_State *co, int narg) {
33 int status, nres;
34 if (l_unlikely(!lua_checkstack(co, narg))) {
35 lua_pushliteral(L, "too many arguments to resume");
36 return -1; /* error flag */
38 lua_xmove(L, co, narg);
39 status = lua_resume(co, L, narg, &nres);
40 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
41 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
42 lua_pop(co, nres); /* remove results anyway */
43 lua_pushliteral(L, "too many results to resume");
44 return -1; /* error flag */
46 lua_xmove(co, L, nres); /* move yielded values */
47 return nres;
48 } else {
49 lua_xmove(co, L, 1); /* move error message */
50 return -1; /* error flag */
55 static int luaB_coresume(lua_State *L) {
56 lua_State *co = getco(L);
57 int r;
58 r = auxresume(L, co, lua_gettop(L) - 1);
59 if (l_unlikely(r < 0)) {
60 lua_pushboolean(L, 0);
61 lua_insert(L, -2);
62 return 2; /* return false + error message */
63 } else {
64 lua_pushboolean(L, 1);
65 lua_insert(L, -(r + 1));
66 return r + 1; /* return true + 'resume' returns */
71 static int luaB_auxwrap(lua_State *L) {
72 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
73 int r = auxresume(L, co, lua_gettop(L));
74 if (l_unlikely(r < 0)) { /* error? */
75 int stat = lua_status(co);
76 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
77 stat = lua_closethread(co, L); /* close its tbc variables */
78 lua_assert(stat != LUA_OK);
79 lua_xmove(co, L, 1); /* move error message to the caller */
81 if (stat != LUA_ERRMEM && /* not a memory error and ... */
82 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
83 luaL_where(L, 1); /* add extra info, if available */
84 lua_insert(L, -2);
85 lua_concat(L, 2);
87 return lua_error(L); /* propagate error */
89 return r;
93 static int luaB_cocreate(lua_State *L) {
94 lua_State *NL;
95 luaL_checktype(L, 1, LUA_TFUNCTION);
96 NL = lua_newthread(L);
97 lua_pushvalue(L, 1); /* move function to top */
98 lua_xmove(L, NL, 1); /* move function from L to NL */
99 return 1;
103 static int luaB_cowrap(lua_State *L) {
104 luaB_cocreate(L);
105 lua_pushcclosure(L, luaB_auxwrap, 1);
106 return 1;
110 static int luaB_yield(lua_State *L) {
111 return lua_yield(L, lua_gettop(L));
115 #define COS_RUN 0
116 #define COS_DEAD 1
117 #define COS_YIELD 2
118 #define COS_NORM 3
121 static const char *const statname[] =
122 {"running", "dead", "suspended", "normal"};
125 static int auxstatus(lua_State *L, lua_State *co) {
126 if (L == co) return COS_RUN;
127 else {
128 switch (lua_status(co)) {
129 case LUA_YIELD:
130 return COS_YIELD;
131 case LUA_OK: {
132 lua_Debug ar;
133 if (lua_getstack(co, 0, &ar)) /* does it have frames? */
134 return COS_NORM; /* it is running */
135 else if (lua_gettop(co) == 0)
136 return COS_DEAD;
137 else
138 return COS_YIELD; /* initial state */
140 default: /* some error occurred */
141 return COS_DEAD;
147 static int luaB_costatus(lua_State *L) {
148 lua_State *co = getco(L);
149 lua_pushstring(L, statname[auxstatus(L, co)]);
150 return 1;
154 static int luaB_yieldable(lua_State *L) {
155 lua_State *co = lua_isnone(L, 1) ? L : getco(L);
156 lua_pushboolean(L, lua_isyieldable(co));
157 return 1;
161 static int luaB_corunning(lua_State *L) {
162 int ismain = lua_pushthread(L);
163 lua_pushboolean(L, ismain);
164 return 2;
168 static int luaB_close(lua_State *L) {
169 lua_State *co = getco(L);
170 int status = auxstatus(L, co);
171 switch (status) {
172 case COS_DEAD:
173 case COS_YIELD: {
174 status = lua_closethread(co, L);
175 if (status == LUA_OK) {
176 lua_pushboolean(L, 1);
177 return 1;
178 } else {
179 lua_pushboolean(L, 0);
180 lua_xmove(co, L, 1); /* move error message */
181 return 2;
184 default: /* normal or running coroutine */
185 return luaL_error(L, "cannot close a %s coroutine", statname[status]);
190 static const luaL_Reg co_funcs[] = {
191 {"create", luaB_cocreate},
192 {"resume", luaB_coresume},
193 {"running", luaB_corunning},
194 {"status", luaB_costatus},
195 {"wrap", luaB_cowrap},
196 {"yield", luaB_yield},
197 {"isyieldable", luaB_yieldable},
198 {"close", luaB_close},
199 {NULL, NULL}
204 LUAMOD_API int luaopen_coroutine(lua_State *L) {
205 luaL_newlib(L, co_funcs);
206 return 1;