9
3

lcorolib.c 3.7 KB

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