interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lcorolib.c

226 lines · 5305 bytes Raw Blame History
1/*
2** $Id: lcorolib.c $
3** Coroutine Library
4** See Copyright Notice in lua.h
5*/
6
7#define lcorolib_c
8#define LUA_LIB
9
10#include "lprefix.h"
11
12
13#include <stdlib.h>
14
15#include "lua.h"
16
17#include "lauxlib.h"
18#include "lualib.h"
19#include "llimits.h"
20
21
22static lua_State *getco (lua_State *L) {
23 lua_State *co = lua_tothread(L, 1);
24 luaL_argexpected(L, co, 1, "thread");
25 return co;
26}
27
28
29/*
30** Resumes a coroutine. Returns the number of results for non-error
31** cases or -1 for errors.
32*/
33static int auxresume (lua_State *L, lua_State *co, int narg) {
34 int status, nres;
35 if (l_unlikely(!lua_checkstack(co, narg))) {
36 lua_pushliteral(L, "too many arguments to resume");
37 return -1; /* error flag */
38 }
39 lua_xmove(L, co, narg);
40 status = lua_resume(co, L, narg, &nres);
41 if (l_likely(status == LUA_OK || status == LUA_YIELD)) {
42 if (l_unlikely(!lua_checkstack(L, nres + 1))) {
43 lua_pop(co, nres); /* remove results anyway */
44 lua_pushliteral(L, "too many results to resume");
45 return -1; /* error flag */
46 }
47 lua_xmove(co, L, nres); /* move yielded values */
48 return nres;
49 }
50 else {
51 lua_xmove(co, L, 1); /* move error message */
52 return -1; /* error flag */
53 }
54}
55
56
57static int luaB_coresume (lua_State *L) {
58 lua_State *co = getco(L);
59 int r;
60 r = auxresume(L, co, lua_gettop(L) - 1);
61 if (l_unlikely(r < 0)) {
62 lua_pushboolean(L, 0);
63 lua_insert(L, -2);
64 return 2; /* return false + error message */
65 }
66 else {
67 lua_pushboolean(L, 1);
68 lua_insert(L, -(r + 1));
69 return r + 1; /* return true + 'resume' returns */
70 }
71}
72
73
74static int luaB_auxwrap (lua_State *L) {
75 lua_State *co = lua_tothread(L, lua_upvalueindex(1));
76 int r = auxresume(L, co, lua_gettop(L));
77 if (l_unlikely(r < 0)) { /* error? */
78 int stat = lua_status(co);
79 if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
80 stat = lua_closethread(co, L); /* close its tbc variables */
81 if (stat == LUA_OK)
82 return 0; /* nothing to return */
83 lua_xmove(co, L, 1); /* move error message to the caller */
84 }
85 if (stat != LUA_ERRMEM && /* not a memory error and ... */
86 lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
87 luaL_where(L, 1); /* add extra info, if available */
88 lua_insert(L, -2);
89 lua_concat(L, 2);
90 }
91 return lua_error(L); /* propagate error */
92 }
93 return r;
94}
95
96
97static int luaB_cocreate (lua_State *L) {
98 lua_State *NL;
99 luaL_checktype(L, 1, LUA_TFUNCTION);
100 NL = lua_newthread(L);
101 lua_pushvalue(L, 1); /* move function to top */
102 lua_xmove(L, NL, 1); /* move function from L to NL */
103 return 1;
104}
105
106
107static int luaB_cowrap (lua_State *L) {
108 luaB_cocreate(L);
109 lua_pushcclosure(L, luaB_auxwrap, 1);
110 return 1;
111}
112
113
114static int luaB_yield (lua_State *L) {
115 return lua_yield(L, lua_gettop(L));
116}
117
118
119#define COS_RUN 0
120#define COS_DEAD 1
121#define COS_YIELD 2
122#define COS_NORM 3
123
124
125static const char *const statname[] =
126 {"running", "dead", "suspended", "normal"};
127
128
129static int auxstatus (lua_State *L, lua_State *co) {
130 if (L == co) return COS_RUN;
131 else {
132 switch (lua_status(co)) {
133 case LUA_YIELD:
134 return COS_YIELD;
135 case LUA_OK: {
136 lua_Debug ar;
137 if (lua_getstack(co, 0, &ar)) /* does it have frames? */
138 return COS_NORM; /* it is running */
139 else if (lua_gettop(co) == 0)
140 return COS_DEAD;
141 else
142 return COS_YIELD; /* initial state */
143 }
144 default: /* some error occurred */
145 return COS_DEAD;
146 }
147 }
148}
149
150
151static int luaB_costatus (lua_State *L) {
152 lua_State *co = getco(L);
153 lua_pushstring(L, statname[auxstatus(L, co)]);
154 return 1;
155}
156
157
158static lua_State *getoptco (lua_State *L) {
159 return (lua_isnone(L, 1) ? L : getco(L));
160}
161
162
163static int luaB_yieldable (lua_State *L) {
164 lua_State *co = getoptco(L);
165 lua_pushboolean(L, lua_isyieldable(co));
166 return 1;
167}
168
169
170static int luaB_corunning (lua_State *L) {
171 int ismain = lua_pushthread(L);
172 lua_pushboolean(L, ismain);
173 return 2;
174}
175
176
177static int luaB_close (lua_State *L) {
178 lua_State *co = getoptco(L);
179 int status = auxstatus(L, co);
180 switch (status) {
181 case COS_DEAD: case COS_YIELD: {
182 status = lua_closethread(co, L);
183 if (status == LUA_OK) {
184 lua_pushboolean(L, 1);
185 return 1;
186 }
187 else {
188 lua_pushboolean(L, 0);
189 lua_xmove(co, L, 1); /* move error message */
190 return 2;
191 }
192 }
193 case COS_NORM:
194 return luaL_error(L, "cannot close a %s coroutine", statname[status]);
195 case COS_RUN:
196 lua_geti(L, LUA_REGISTRYINDEX, LUA_RIDX_MAINTHREAD); /* get main */
197 if (lua_tothread(L, -1) == co)
198 return luaL_error(L, "cannot close main thread");
199 lua_closethread(co, L); /* close itself */
200 /* previous call does not return *//* FALLTHROUGH */
201 default:
202 lua_assert(0);
203 return 0;
204 }
205}
206
207
208static const luaL_Reg co_funcs[] = {
209 {"create", luaB_cocreate},
210 {"resume", luaB_coresume},
211 {"running", luaB_corunning},
212 {"status", luaB_costatus},
213 {"wrap", luaB_cowrap},
214 {"yield", luaB_yield},
215 {"isyieldable", luaB_yieldable},
216 {"close", luaB_close},
217 {NULL, NULL}
218};
219
220
221
222LUAMOD_API int luaopen_coroutine (lua_State *L) {
223 luaL_newlib(L, co_funcs);
224 return 1;
225}
226