diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2014-11-10 18:49:40 +0100 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2014-11-10 18:49:40 +0100 |
| commit | 0b03eec16be0b3a5efe71bcb8887719d1ea87d60 (patch) | |
| tree | d1a2839b605623df8a4100b81158d9d38089aab5 | |
| parent | 6dcecd8f45fd3d0143b33548553c1d1f7dabc0b0 (diff) | |
| download | luasocket-0b03eec16be0b3a5efe71bcb8887719d1ea87d60.tar.gz luasocket-0b03eec16be0b3a5efe71bcb8887719d1ea87d60.tar.bz2 luasocket-0b03eec16be0b3a5efe71bcb8887719d1ea87d60.zip | |
make socket.protect yieldable on Lua 5.2/5.3
| -rw-r--r-- | src/except.c | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/src/except.c b/src/except.c index 002e701..4faa208 100644 --- a/src/except.c +++ b/src/except.c | |||
| @@ -9,6 +9,15 @@ | |||
| 9 | 9 | ||
| 10 | #include "except.h" | 10 | #include "except.h" |
| 11 | 11 | ||
| 12 | #if LUA_VERSION_NUM < 502 | ||
| 13 | #define lua_pcallk(L, na, nr, err, ctx, cont) \ | ||
| 14 | ((void)ctx,(void)cont,lua_pcall(L, na, nr, err)) | ||
| 15 | #endif | ||
| 16 | |||
| 17 | #if LUA_VERSION_NUM < 503 | ||
| 18 | typedef int lua_KContext; | ||
| 19 | #endif | ||
| 20 | |||
| 12 | /*=========================================================================*\ | 21 | /*=========================================================================*\ |
| 13 | * Internal function prototypes. | 22 | * Internal function prototypes. |
| 14 | \*=========================================================================*/ | 23 | \*=========================================================================*/ |
| @@ -73,14 +82,30 @@ static int unwrap(lua_State *L) { | |||
| 73 | } else return 0; | 82 | } else return 0; |
| 74 | } | 83 | } |
| 75 | 84 | ||
| 85 | static int protected_finish(lua_State *L, int status, lua_KContext ctx) { | ||
| 86 | (void)ctx; | ||
| 87 | if (status != 0 && status != LUA_YIELD) { | ||
| 88 | if (unwrap(L)) return 2; | ||
| 89 | else return lua_error(L); | ||
| 90 | } else return lua_gettop(L); | ||
| 91 | } | ||
| 92 | |||
| 93 | #if LUA_VERSION_NUM == 502 | ||
| 94 | static int protected_cont(lua_State *L) { | ||
| 95 | int ctx = 0; | ||
| 96 | int status = lua_getctx(L, &ctx); | ||
| 97 | return protected_finish(L, status, ctx); | ||
| 98 | } | ||
| 99 | #else | ||
| 100 | #define protected_cont protected_finish | ||
| 101 | #endif | ||
| 102 | |||
| 76 | static int protected_(lua_State *L) { | 103 | static int protected_(lua_State *L) { |
| 104 | int status; | ||
| 77 | lua_pushvalue(L, lua_upvalueindex(1)); | 105 | lua_pushvalue(L, lua_upvalueindex(1)); |
| 78 | lua_insert(L, 1); | 106 | lua_insert(L, 1); |
| 79 | if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) { | 107 | status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont); |
| 80 | if (unwrap(L)) return 2; | 108 | return protected_finish(L, status, 0); |
| 81 | else lua_error(L); | ||
| 82 | return 0; | ||
| 83 | } else return lua_gettop(L); | ||
| 84 | } | 109 | } |
| 85 | 110 | ||
| 86 | static int global_protect(lua_State *L) { | 111 | static int global_protect(lua_State *L) { |
