2
0

lcorolib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ** $Id: lcorolib.c,v 1.6 2014/05/08 13:52:20 roberto Exp roberto $
  3. ** Coroutine Library
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdlib.h>
  7. #define lcorolib_c
  8. #define LUA_LIB
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "lualib.h"
  12. static lua_State *getco (lua_State *L) {
  13. lua_State *co = lua_tothread(L, 1);
  14. luaL_argcheck(L, co, 1, "thread expected");
  15. return co;
  16. }
  17. static int auxresume (lua_State *L, lua_State *co, int narg) {
  18. int status;
  19. if (!lua_checkstack(co, narg)) {
  20. lua_pushliteral(L, "too many arguments to resume");
  21. return -1; /* error flag */
  22. }
  23. if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
  24. lua_pushliteral(L, "cannot resume dead coroutine");
  25. return -1; /* error flag */
  26. }
  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 */
  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. if (lua_isstring(L, -1)) { /* error object is a string? */
  64. luaL_where(L, 1); /* add extra info */
  65. lua_insert(L, -2);
  66. lua_concat(L, 2);
  67. }
  68. return lua_error(L); /* propagate error */
  69. }
  70. return r;
  71. }
  72. static int luaB_cocreate (lua_State *L) {
  73. lua_State *NL;
  74. luaL_checktype(L, 1, LUA_TFUNCTION);
  75. NL = lua_newthread(L);
  76. lua_pushvalue(L, 1); /* move function to top */
  77. lua_xmove(L, NL, 1); /* move function from L to NL */
  78. return 1;
  79. }
  80. static int luaB_cowrap (lua_State *L) {
  81. luaB_cocreate(L);
  82. lua_pushcclosure(L, luaB_auxwrap, 1);
  83. return 1;
  84. }
  85. static int luaB_yield (lua_State *L) {
  86. return lua_yield(L, lua_gettop(L));
  87. }
  88. static int luaB_costatus (lua_State *L) {
  89. lua_State *co = getco(L);
  90. if (L == co) lua_pushliteral(L, "running");
  91. else {
  92. switch (lua_status(co)) {
  93. case LUA_YIELD:
  94. lua_pushliteral(L, "suspended");
  95. break;
  96. case LUA_OK: {
  97. lua_Debug ar;
  98. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  99. lua_pushliteral(L, "normal"); /* it is running */
  100. else if (lua_gettop(co) == 0)
  101. lua_pushliteral(L, "dead");
  102. else
  103. lua_pushliteral(L, "suspended"); /* initial state */
  104. break;
  105. }
  106. default: /* some error occurred */
  107. lua_pushliteral(L, "dead");
  108. break;
  109. }
  110. }
  111. return 1;
  112. }
  113. static int luaB_yieldable (lua_State *L) {
  114. lua_pushboolean(L, lua_isyieldable(L));
  115. return 1;
  116. }
  117. static int luaB_corunning (lua_State *L) {
  118. int ismain = lua_pushthread(L);
  119. lua_pushboolean(L, ismain);
  120. return 2;
  121. }
  122. static const luaL_Reg co_funcs[] = {
  123. {"create", luaB_cocreate},
  124. {"resume", luaB_coresume},
  125. {"running", luaB_corunning},
  126. {"status", luaB_costatus},
  127. {"wrap", luaB_cowrap},
  128. {"yield", luaB_yield},
  129. {"isyieldable", luaB_yieldable},
  130. {NULL, NULL}
  131. };
  132. LUAMOD_API int luaopen_coroutine (lua_State *L) {
  133. luaL_newlib(L, co_funcs);
  134. return 1;
  135. }