9
3

lcorolib.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 (!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 (status == LUA_OK || status == LUA_YIELD) {
  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 */
  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 (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 (r < 0) { /* error? */
  63. int stat = lua_status(co);
  64. if (stat != LUA_OK && stat != LUA_YIELD) /* error in the coroutine? */
  65. lua_resetthread(co); /* close its tbc variables */
  66. if (stat != LUA_ERRMEM && /* not a memory error and ... */
  67. lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
  68. luaL_where(L, 1); /* add extra info, if available */
  69. lua_insert(L, -2);
  70. lua_concat(L, 2);
  71. }
  72. return lua_error(L); /* propagate error */
  73. }
  74. return r;
  75. }
  76. static int luaB_cocreate (lua_State *L) {
  77. lua_State *NL;
  78. luaL_checktype(L, 1, LUA_TFUNCTION);
  79. NL = lua_newthread(L);
  80. lua_pushvalue(L, 1); /* move function to top */
  81. lua_xmove(L, NL, 1); /* move function from L to NL */
  82. return 1;
  83. }
  84. static int luaB_cowrap (lua_State *L) {
  85. luaB_cocreate(L);
  86. lua_pushcclosure(L, luaB_auxwrap, 1);
  87. return 1;
  88. }
  89. static int luaB_yield (lua_State *L) {
  90. return lua_yield(L, lua_gettop(L));
  91. }
  92. #define COS_RUN 0
  93. #define COS_DEAD 1
  94. #define COS_YIELD 2
  95. #define COS_NORM 3
  96. static const char *const statname[] =
  97. {"running", "dead", "suspended", "normal"};
  98. static int auxstatus (lua_State *L, lua_State *co) {
  99. if (L == co) return COS_RUN;
  100. else {
  101. switch (lua_status(co)) {
  102. case LUA_YIELD:
  103. return COS_YIELD;
  104. case LUA_OK: {
  105. lua_Debug ar;
  106. if (lua_getstack(co, 0, &ar)) /* does it have frames? */
  107. return COS_NORM; /* it is running */
  108. else if (lua_gettop(co) == 0)
  109. return COS_DEAD;
  110. else
  111. return COS_YIELD; /* initial state */
  112. }
  113. default: /* some error occurred */
  114. return COS_DEAD;
  115. }
  116. }
  117. }
  118. static int luaB_costatus (lua_State *L) {
  119. lua_State *co = getco(L);
  120. lua_pushstring(L, statname[auxstatus(L, co)]);
  121. return 1;
  122. }
  123. static int luaB_yieldable (lua_State *L) {
  124. lua_State *co = lua_isnone(L, 1) ? L : getco(L);
  125. lua_pushboolean(L, lua_isyieldable(co));
  126. return 1;
  127. }
  128. static int luaB_corunning (lua_State *L) {
  129. int ismain = lua_pushthread(L);
  130. lua_pushboolean(L, ismain);
  131. return 2;
  132. }
  133. static int luaB_close (lua_State *L) {
  134. lua_State *co = getco(L);
  135. int status = auxstatus(L, co);
  136. switch (status) {
  137. case COS_DEAD: case COS_YIELD: {
  138. status = lua_resetthread(co);
  139. if (status == LUA_OK) {
  140. lua_pushboolean(L, 1);
  141. return 1;
  142. }
  143. else {
  144. lua_pushboolean(L, 0);
  145. lua_xmove(co, L, 1); /* copy error message */
  146. return 2;
  147. }
  148. }
  149. default: /* normal or running coroutine */
  150. return luaL_error(L, "cannot close a %s coroutine", statname[status]);
  151. }
  152. }
  153. static const luaL_Reg co_funcs[] = {
  154. {"create", luaB_cocreate},
  155. {"resume", luaB_coresume},
  156. {"running", luaB_corunning},
  157. {"status", luaB_costatus},
  158. {"wrap", luaB_cowrap},
  159. {"yield", luaB_yield},
  160. {"isyieldable", luaB_yieldable},
  161. {"close", luaB_close},
  162. {NULL, NULL}
  163. };
  164. LUAMOD_API int luaopen_coroutine (lua_State *L) {
  165. luaL_newlib(L, co_funcs);
  166. return 1;
  167. }