2 ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $
4 ** See Copyright Notice in lua.h
11 #include <sys/lua/lua.h>
13 #include <sys/lua/lauxlib.h>
14 #include <sys/lua/lualib.h>
17 static int auxresume (lua_State
*L
, lua_State
*co
, int narg
) {
19 if (!lua_checkstack(co
, narg
)) {
20 lua_pushliteral(L
, "too many arguments to resume");
21 return -1; /* error flag */
23 if (lua_status(co
) == LUA_OK
&& lua_gettop(co
) == 0) {
24 lua_pushliteral(L
, "cannot resume dead coroutine");
25 return -1; /* error flag */
27 lua_xmove(L
, co
, narg
);
28 status
= lua_resume(co
, L
, narg
);
29 if (status
== LUA_OK
|| status
== LUA_YIELD
) {
30 int nres
= lua_gettop(co
);
31 if (!lua_checkstack(L
, nres
+ 1)) {
32 lua_pop(co
, nres
); /* remove results anyway */
33 lua_pushliteral(L
, "too many results to resume");
34 return -1; /* error flag */
36 lua_xmove(co
, L
, nres
); /* move yielded values */
40 lua_xmove(co
, L
, 1); /* move error message */
41 return -1; /* error flag */
46 static int luaB_coresume (lua_State
*L
) {
47 lua_State
*co
= lua_tothread(L
, 1);
49 luaL_argcheck(L
, co
, 1, "coroutine expected");
50 r
= auxresume(L
, co
, lua_gettop(L
) - 1);
52 lua_pushboolean(L
, 0);
54 return 2; /* return false + error message */
57 lua_pushboolean(L
, 1);
58 lua_insert(L
, -(r
+ 1));
59 return r
+ 1; /* return true + 'resume' returns */
64 static int luaB_auxwrap (lua_State
*L
) {
65 lua_State
*co
= lua_tothread(L
, lua_upvalueindex(1));
66 int r
= auxresume(L
, co
, lua_gettop(L
));
68 if (lua_isstring(L
, -1)) { /* error object is a string? */
69 luaL_where(L
, 1); /* add extra info */
73 return lua_error(L
); /* propagate error */
79 static int luaB_cocreate (lua_State
*L
) {
81 luaL_checktype(L
, 1, LUA_TFUNCTION
);
82 NL
= lua_newthread(L
);
83 lua_pushvalue(L
, 1); /* move function to top */
84 lua_xmove(L
, NL
, 1); /* move function from L to NL */
89 static int luaB_cowrap (lua_State
*L
) {
91 lua_pushcclosure(L
, luaB_auxwrap
, 1);
96 static int luaB_yield (lua_State
*L
) {
97 return lua_yield(L
, lua_gettop(L
));
101 static int luaB_costatus (lua_State
*L
) {
102 lua_State
*co
= lua_tothread(L
, 1);
103 luaL_argcheck(L
, co
, 1, "coroutine expected");
104 if (L
== co
) lua_pushliteral(L
, "running");
106 switch (lua_status(co
)) {
108 lua_pushliteral(L
, "suspended");
112 if (lua_getstack(co
, 0, &ar
) > 0) /* does it have frames? */
113 lua_pushliteral(L
, "normal"); /* it is running */
114 else if (lua_gettop(co
) == 0)
115 lua_pushliteral(L
, "dead");
117 lua_pushliteral(L
, "suspended"); /* initial state */
120 default: /* some error occurred */
121 lua_pushliteral(L
, "dead");
129 static int luaB_corunning (lua_State
*L
) {
130 int ismain
= lua_pushthread(L
);
131 lua_pushboolean(L
, ismain
);
136 static const luaL_Reg co_funcs
[] = {
137 {"create", luaB_cocreate
},
138 {"resume", luaB_coresume
},
139 {"running", luaB_corunning
},
140 {"status", luaB_costatus
},
141 {"wrap", luaB_cowrap
},
142 {"yield", luaB_yield
},
148 LUAMOD_API
int luaopen_coroutine (lua_State
*L
) {
149 luaL_newlib(L
, co_funcs
);
155 EXPORT_SYMBOL(luaopen_coroutine
);