2
0

lcorolib.c 5.2 KB

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