diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-06-22 16:50:40 +0800 |
| commit | cd2b60b101a398cb9356d746364e70eaed1860f1 (patch) | |
| tree | a1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/lcorolib.c | |
| parent | 88c1052e700f38cf3d8ad82d469da4c487760b7e (diff) | |
| download | yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2 yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip | |
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to 'src/lua/lcorolib.c')
| -rw-r--r-- | src/lua/lcorolib.c | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/src/lua/lcorolib.c b/src/lua/lcorolib.c new file mode 100644 index 0000000..7d6e585 --- /dev/null +++ b/src/lua/lcorolib.c | |||
| @@ -0,0 +1,206 @@ | |||
| 1 | /* | ||
| 2 | ** $Id: lcorolib.c $ | ||
| 3 | ** Coroutine Library | ||
| 4 | ** See Copyright Notice in lua.h | ||
| 5 | */ | ||
| 6 | |||
| 7 | #define lcorolib_c | ||
| 8 | #define LUA_LIB | ||
| 9 | |||
| 10 | #include "lprefix.h" | ||
| 11 | |||
| 12 | |||
| 13 | #include <stdlib.h> | ||
| 14 | |||
| 15 | #include "lua.h" | ||
| 16 | |||
| 17 | #include "lauxlib.h" | ||
| 18 | #include "lualib.h" | ||
| 19 | |||
| 20 | |||
| 21 | static lua_State *getco (lua_State *L) { | ||
| 22 | lua_State *co = lua_tothread(L, 1); | ||
| 23 | luaL_argexpected(L, co, 1, "thread"); | ||
| 24 | return co; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | /* | ||
| 29 | ** Resumes a coroutine. Returns the number of results for non-error | ||
| 30 | ** cases or -1 for errors. | ||
| 31 | */ | ||
| 32 | static int auxresume (lua_State *L, lua_State *co, int narg) { | ||
| 33 | int status, nres; | ||
| 34 | if (!lua_checkstack(co, narg)) { | ||
| 35 | lua_pushliteral(L, "too many arguments to resume"); | ||
| 36 | return -1; /* error flag */ | ||
| 37 | } | ||
| 38 | lua_xmove(L, co, narg); | ||
| 39 | status = lua_resume(co, L, narg, &nres); | ||
| 40 | if (status == LUA_OK || status == LUA_YIELD) { | ||
| 41 | if (!lua_checkstack(L, nres + 1)) { | ||
| 42 | lua_pop(co, nres); /* remove results anyway */ | ||
| 43 | lua_pushliteral(L, "too many results to resume"); | ||
| 44 | return -1; /* error flag */ | ||
| 45 | } | ||
| 46 | lua_xmove(co, L, nres); /* move yielded values */ | ||
| 47 | return nres; | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | lua_xmove(co, L, 1); /* move error message */ | ||
| 51 | return -1; /* error flag */ | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | static int luaB_coresume (lua_State *L) { | ||
| 57 | lua_State *co = getco(L); | ||
| 58 | int r; | ||
| 59 | r = auxresume(L, co, lua_gettop(L) - 1); | ||
| 60 | if (r < 0) { | ||
| 61 | lua_pushboolean(L, 0); | ||
| 62 | lua_insert(L, -2); | ||
| 63 | return 2; /* return false + error message */ | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | lua_pushboolean(L, 1); | ||
| 67 | lua_insert(L, -(r + 1)); | ||
| 68 | return r + 1; /* return true + 'resume' returns */ | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | static int luaB_auxwrap (lua_State *L) { | ||
| 74 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); | ||
| 75 | int r = auxresume(L, co, lua_gettop(L)); | ||
| 76 | if (r < 0) { | ||
| 77 | int stat = lua_status(co); | ||
| 78 | if (stat != LUA_OK && stat != LUA_YIELD) | ||
| 79 | lua_resetthread(co); /* close variables in case of errors */ | ||
| 80 | if (lua_type(L, -1) == LUA_TSTRING) { /* error object is a string? */ | ||
| 81 | luaL_where(L, 1); /* add extra info, if available */ | ||
| 82 | lua_insert(L, -2); | ||
| 83 | lua_concat(L, 2); | ||
| 84 | } | ||
| 85 | return lua_error(L); /* propagate error */ | ||
| 86 | } | ||
| 87 | return r; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | static int luaB_cocreate (lua_State *L) { | ||
| 92 | lua_State *NL; | ||
| 93 | luaL_checktype(L, 1, LUA_TFUNCTION); | ||
| 94 | NL = lua_newthread(L); | ||
| 95 | lua_pushvalue(L, 1); /* move function to top */ | ||
| 96 | lua_xmove(L, NL, 1); /* move function from L to NL */ | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | static int luaB_cowrap (lua_State *L) { | ||
| 102 | luaB_cocreate(L); | ||
| 103 | lua_pushcclosure(L, luaB_auxwrap, 1); | ||
| 104 | return 1; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | static int luaB_yield (lua_State *L) { | ||
| 109 | return lua_yield(L, lua_gettop(L)); | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | #define COS_RUN 0 | ||
| 114 | #define COS_DEAD 1 | ||
| 115 | #define COS_YIELD 2 | ||
| 116 | #define COS_NORM 3 | ||
| 117 | |||
| 118 | |||
| 119 | static const char *const statname[] = | ||
| 120 | {"running", "dead", "suspended", "normal"}; | ||
| 121 | |||
| 122 | |||
| 123 | static int auxstatus (lua_State *L, lua_State *co) { | ||
| 124 | if (L == co) return COS_RUN; | ||
| 125 | else { | ||
| 126 | switch (lua_status(co)) { | ||
| 127 | case LUA_YIELD: | ||
| 128 | return COS_YIELD; | ||
| 129 | case LUA_OK: { | ||
| 130 | lua_Debug ar; | ||
| 131 | if (lua_getstack(co, 0, &ar)) /* does it have frames? */ | ||
| 132 | return COS_NORM; /* it is running */ | ||
| 133 | else if (lua_gettop(co) == 0) | ||
| 134 | return COS_DEAD; | ||
| 135 | else | ||
| 136 | return COS_YIELD; /* initial state */ | ||
| 137 | } | ||
| 138 | default: /* some error occurred */ | ||
| 139 | return COS_DEAD; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | static int luaB_costatus (lua_State *L) { | ||
| 146 | lua_State *co = getco(L); | ||
| 147 | lua_pushstring(L, statname[auxstatus(L, co)]); | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | static int luaB_yieldable (lua_State *L) { | ||
| 153 | lua_State *co = lua_isnone(L, 1) ? L : getco(L); | ||
| 154 | lua_pushboolean(L, lua_isyieldable(co)); | ||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | static int luaB_corunning (lua_State *L) { | ||
| 160 | int ismain = lua_pushthread(L); | ||
| 161 | lua_pushboolean(L, ismain); | ||
| 162 | return 2; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | static int luaB_close (lua_State *L) { | ||
| 167 | lua_State *co = getco(L); | ||
| 168 | int status = auxstatus(L, co); | ||
| 169 | switch (status) { | ||
| 170 | case COS_DEAD: case COS_YIELD: { | ||
| 171 | status = lua_resetthread(co); | ||
| 172 | if (status == LUA_OK) { | ||
| 173 | lua_pushboolean(L, 1); | ||
| 174 | return 1; | ||
| 175 | } | ||
| 176 | else { | ||
| 177 | lua_pushboolean(L, 0); | ||
| 178 | lua_xmove(co, L, 1); /* copy error message */ | ||
| 179 | return 2; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | default: /* normal or running coroutine */ | ||
| 183 | return luaL_error(L, "cannot close a %s coroutine", statname[status]); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | static const luaL_Reg co_funcs[] = { | ||
| 189 | {"create", luaB_cocreate}, | ||
| 190 | {"resume", luaB_coresume}, | ||
| 191 | {"running", luaB_corunning}, | ||
| 192 | {"status", luaB_costatus}, | ||
| 193 | {"wrap", luaB_cowrap}, | ||
| 194 | {"yield", luaB_yield}, | ||
| 195 | {"isyieldable", luaB_yieldable}, | ||
| 196 | {"close", luaB_close}, | ||
| 197 | {NULL, NULL} | ||
| 198 | }; | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | LUAMOD_API int luaopen_coroutine (lua_State *L) { | ||
| 203 | luaL_newlib(L, co_funcs); | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | |||
