lcorolib.c 4.7 KB

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