lcorolib.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. ** $Id: lcorolib.c,v 1.4 2012/04/27 18:59:04 roberto Exp $
  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 int auxresume (lua_State *L, lua_State *co, int narg) {
  13. int status;
  14. if (!lua_checkstack(co, narg)) {
  15. lua_pushliteral(L, "too many arguments to resume");
  16. return -1; /* error flag */
  17. }
  18. if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
  19. lua_pushliteral(L, "cannot resume dead coroutine");
  20. return -1; /* error flag */
  21. }
  22. lua_xmove(L, co, narg);
  23. status = lua_resume(co, L, narg);
  24. if (status == LUA_OK || status == LUA_YIELD) {
  25. int nres = lua_gettop(co);
  26. if (!lua_checkstack(L, nres + 1)) {
  27. lua_pop(co, nres); /* remove results anyway */
  28. lua_pushliteral(L, "too many results to resume");
  29. return -1; /* error flag */
  30. }
  31. lua_xmove(co, L, nres); /* move yielded values */
  32. return nres;
  33. }
  34. else {
  35. lua_xmove(co, L, 1); /* move error message */
  36. return -1; /* error flag */
  37. }
  38. }
  39. static int luaB_coresume (lua_State *L) {
  40. lua_State *co = lua_tothread(L, 1);
  41. int r;
  42. luaL_argcheck(L, co, 1, "coroutine expected");
  43. r = auxresume(L, co, lua_gettop(L) - 1);
  44. if (r < 0) {
  45. lua_pushboolean(L, 0);
  46. lua_insert(L, -2);
  47. return 2; /* return false + error message */
  48. }
  49. else {
  50. lua_pushboolean(L, 1);
  51. lua_insert(L, -(r + 1));
  52. return r + 1; /* return true + `resume' returns */
  53. }
  54. }
  55. static int luaB_auxwrap (lua_State *L) {
  56. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  57. int r = auxresume(L, co, lua_gettop(L));
  58. if (r < 0) {
  59. if (lua_isstring(L, -1)) { /* error object is a string? */
  60. luaL_where(L, 1); /* add extra info */
  61. lua_insert(L, -2);
  62. lua_concat(L, 2);
  63. }
  64. lua_error(L); /* propagate error */
  65. }
  66. return r;
  67. }
  68. static int luaB_cocreate (lua_State *L) {
  69. lua_State *NL;
  70. luaL_checktype(L, 1, LUA_TFUNCTION);
  71. NL = lua_newthread(L);
  72. lua_pushvalue(L, 1); /* move function to top */
  73. lua_xmove(L, NL, 1); /* move function from L to NL */
  74. return 1;
  75. }
  76. static int luaB_cowrap (lua_State *L) {
  77. luaB_cocreate(L);
  78. lua_pushcclosure(L, luaB_auxwrap, 1);
  79. return 1;
  80. }
  81. static int luaB_yield (lua_State *L) {
  82. return lua_yield(L, lua_gettop(L));
  83. }
  84. static int luaB_costatus (lua_State *L) {
  85. lua_State *co = lua_tothread(L, 1);
  86. luaL_argcheck(L, co, 1, "coroutine expected");
  87. if (L == co) lua_pushliteral(L, "running");
  88. else {
  89. switch (lua_status(co)) {
  90. case LUA_YIELD:
  91. lua_pushliteral(L, "suspended");
  92. break;
  93. case LUA_OK: {
  94. lua_Debug ar;
  95. if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
  96. lua_pushliteral(L, "normal"); /* it is running */
  97. else if (lua_gettop(co) == 0)
  98. lua_pushliteral(L, "dead");
  99. else
  100. lua_pushliteral(L, "suspended"); /* initial state */
  101. break;
  102. }
  103. default: /* some error occurred */
  104. lua_pushliteral(L, "dead");
  105. break;
  106. }
  107. }
  108. return 1;
  109. }
  110. static int luaB_corunning (lua_State *L) {
  111. int ismain = lua_pushthread(L);
  112. lua_pushboolean(L, ismain);
  113. return 2;
  114. }
  115. static const luaL_Reg co_funcs[] = {
  116. {"create", luaB_cocreate},
  117. {"resume", luaB_coresume},
  118. {"running", luaB_corunning},
  119. {"status", luaB_costatus},
  120. {"wrap", luaB_cowrap},
  121. {"yield", luaB_yield},
  122. {NULL, NULL}
  123. };
  124. LUAMOD_API int luaopen_coroutine (lua_State *L) {
  125. luaL_newlib(L, co_funcs);
  126. return 1;
  127. }