2
0

lcorolib.c 4.5 KB

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