aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c78
1 files changed, 1 insertions, 77 deletions
diff --git a/ltablib.c b/ltablib.c
index 292a8f7a..9d4d5910 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: $ 2** $Id: ltablib.c,v 1.1 2002/04/09 20:19:06 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -214,81 +214,6 @@ static int luaB_sort (lua_State *L) {
214/* }====================================================== */ 214/* }====================================================== */
215 215
216 216
217/*
218** {======================================================
219** Coroutine library
220** =======================================================
221*/
222
223
224static int luaB_resume (lua_State *L) {
225 lua_State *co = (lua_State *)lua_getfrombox(L, lua_upvalueindex(1));
226 lua_settop(L, 0);
227 if (lua_resume(L, co) != 0)
228 lua_error(L, "error running co-routine");
229 return lua_gettop(L);
230}
231
232
233
234static int gc_coroutine (lua_State *L) {
235 lua_State *co = (lua_State *)lua_getfrombox(L, 1);
236 lua_closethread(L, co);
237 return 0;
238}
239
240
241static int luaB_coroutine (lua_State *L) {
242 lua_State *NL;
243 int ref;
244 int i;
245 int n = lua_gettop(L);
246 luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
247 "Lua function expected");
248 NL = lua_newthread(L);
249 if (NL == NULL) lua_error(L, "unable to create new thread");
250 /* move function and arguments from L to NL */
251 for (i=0; i<n; i++) {
252 ref = lua_ref(L, 1);
253 lua_getref(NL, ref);
254 lua_insert(NL, 1);
255 lua_unref(L, ref);
256 }
257 lua_cobegin(NL, n-1);
258 lua_newpointerbox(L, NL);
259 lua_pushliteral(L, "Coroutine");
260 lua_rawget(L, LUA_REGISTRYINDEX);
261 lua_setmetatable(L, -2);
262 lua_pushcclosure(L, luaB_resume, 1);
263 return 1;
264}
265
266
267static int luaB_yield (lua_State *L) {
268 return lua_yield(L, lua_gettop(L));
269}
270
271static const luaL_reg co_funcs[] = {
272 {"create", luaB_coroutine},
273 {"yield", luaB_yield},
274 {NULL, NULL}
275};
276
277
278static void co_open (lua_State *L) {
279 luaL_opennamedlib(L, "co", co_funcs, 0);
280 /* create metatable for coroutines */
281 lua_pushliteral(L, "Coroutine");
282 lua_newtable(L);
283 lua_pushliteral(L, "__gc");
284 lua_pushcfunction(L, gc_coroutine);
285 lua_rawset(L, -3);
286 lua_rawset(L, LUA_REGISTRYINDEX);
287}
288
289/* }====================================================== */
290
291
292static const luaL_reg tab_funcs[] = { 217static const luaL_reg tab_funcs[] = {
293 {"foreach", luaB_foreach}, 218 {"foreach", luaB_foreach},
294 {"foreachi", luaB_foreachi}, 219 {"foreachi", luaB_foreachi},
@@ -302,7 +227,6 @@ static const luaL_reg tab_funcs[] = {
302 227
303LUALIB_API int lua_tablibopen (lua_State *L) { 228LUALIB_API int lua_tablibopen (lua_State *L) {
304 luaL_opennamedlib(L, "tab", tab_funcs, 0); 229 luaL_opennamedlib(L, "tab", tab_funcs, 0);
305 co_open(L);
306 return 0; 230 return 0;
307} 231}
308 232