diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2014-11-10 15:56:25 -0200 |
---|---|---|
committer | Diego Nehab <diego.nehab@gmail.com> | 2014-11-10 15:56:25 -0200 |
commit | 5edf093643cceb329392aec9606ab3988579b821 (patch) | |
tree | 1f253acee762c168b5ee8624e02570c706298ee5 | |
parent | 583257c28cdf2204204f93c39f8428e84f06c9f2 (diff) | |
parent | 0b03eec16be0b3a5efe71bcb8887719d1ea87d60 (diff) | |
download | luasocket-5edf093643cceb329392aec9606ab3988579b821.tar.gz luasocket-5edf093643cceb329392aec9606ab3988579b821.tar.bz2 luasocket-5edf093643cceb329392aec9606ab3988579b821.zip |
Merge pull request #114 from siffiejoe/yieldable_protect52
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) { |