Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

159 lignes
3.6 KiB

  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. #if defined HAVE_CONFIG_H // LOL BEGIN
  7. # include "config.h"
  8. #endif // LOL END
  9. #include <stdlib.h>
  10. #define lcorolib_c
  11. #define LUA_LIB
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. #include "lualib.h"
  15. static int auxresume (lua_State *L, lua_State *co, int narg) {
  16. int status;
  17. if (!lua_checkstack(co, narg)) {
  18. lua_pushliteral(L, "too many arguments to resume");
  19. return -1; /* error flag */
  20. }
  21. if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) {
  22. lua_pushliteral(L, "cannot resume dead coroutine");
  23. return -1; /* error flag */
  24. }
  25. lua_xmove(L, co, narg);
  26. status = lua_resume(co, L, narg);
  27. if (status == LUA_OK || status == LUA_YIELD) {
  28. int nres = lua_gettop(co);
  29. if (!lua_checkstack(L, nres + 1)) {
  30. lua_pop(co, nres); /* remove results anyway */
  31. lua_pushliteral(L, "too many results to resume");
  32. return -1; /* error flag */
  33. }
  34. lua_xmove(co, L, nres); /* move yielded values */
  35. return nres;
  36. }
  37. else {
  38. lua_xmove(co, L, 1); /* move error message */
  39. return -1; /* error flag */
  40. }
  41. }
  42. static int luaB_coresume (lua_State *L) {
  43. lua_State *co = lua_tothread(L, 1);
  44. int r;
  45. luaL_argcheck(L, co, 1, "coroutine expected");
  46. r = auxresume(L, co, lua_gettop(L) - 1);
  47. if (r < 0) {
  48. lua_pushboolean(L, 0);
  49. lua_insert(L, -2);
  50. return 2; /* return false + error message */
  51. }
  52. else {
  53. lua_pushboolean(L, 1);
  54. lua_insert(L, -(r + 1));
  55. return r + 1; /* return true + `resume' returns */
  56. }
  57. }
  58. static int luaB_auxwrap (lua_State *L) {
  59. lua_State *co = lua_tothread(L, lua_upvalueindex(1));
  60. int r = auxresume(L, co, lua_gettop(L));
  61. if (r < 0) {
  62. if (lua_isstring(L, -1)) { /* error object is a string? */
  63. luaL_where(L, 1); /* add extra info */
  64. lua_insert(L, -2);
  65. lua_concat(L, 2);
  66. }
  67. lua_error(L); /* propagate error */
  68. }
  69. return r;
  70. }
  71. static int luaB_cocreate (lua_State *L) {
  72. lua_State *NL;
  73. luaL_checktype(L, 1, LUA_TFUNCTION);
  74. NL = lua_newthread(L);
  75. lua_pushvalue(L, 1); /* move function to top */
  76. lua_xmove(L, NL, 1); /* move function from L to NL */
  77. return 1;
  78. }
  79. static int luaB_cowrap (lua_State *L) {
  80. luaB_cocreate(L);
  81. lua_pushcclosure(L, luaB_auxwrap, 1);
  82. return 1;
  83. }
  84. static int luaB_yield (lua_State *L) {
  85. return lua_yield(L, lua_gettop(L));
  86. }
  87. static int luaB_costatus (lua_State *L) {
  88. lua_State *co = lua_tothread(L, 1);
  89. luaL_argcheck(L, co, 1, "coroutine expected");
  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_corunning (lua_State *L) {
  114. int ismain = lua_pushthread(L);
  115. lua_pushboolean(L, ismain);
  116. return 2;
  117. }
  118. static const luaL_Reg co_funcs[] = {
  119. {"create", luaB_cocreate},
  120. {"resume", luaB_coresume},
  121. {"running", luaB_corunning},
  122. {"status", luaB_costatus},
  123. {"wrap", luaB_cowrap},
  124. {"yield", luaB_yield},
  125. {NULL, NULL}
  126. };
  127. LUAMOD_API int luaopen_coroutine (lua_State *L) {
  128. luaL_newlib(L, co_funcs);
  129. return 1;
  130. }