Skip to content

Commit 5edf093

Browse files
committed
Merge pull request #114 from siffiejoe/yieldable_protect52
make socket.protect yieldable on Lua 5.2/5.3
2 parents 583257c + 0b03eec commit 5edf093

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/except.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
#include "except.h"
1111

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+
1221
/*=========================================================================*\
1322
* Internal function prototypes.
1423
\*=========================================================================*/
@@ -73,14 +82,30 @@ static int unwrap(lua_State *L) {
7382
} else return 0;
7483
}
7584

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+
76103
static int protected_(lua_State *L) {
104+
int status;
77105
lua_pushvalue(L, lua_upvalueindex(1));
78106
lua_insert(L, 1);
79-
if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) {
80-
if (unwrap(L)) return 2;
81-
else lua_error(L);
82-
return 0;
83-
} else return lua_gettop(L);
107+
status = lua_pcallk(L, lua_gettop(L) - 1, LUA_MULTRET, 0, 0, protected_cont);
108+
return protected_finish(L, status, 0);
84109
}
85110

86111
static int global_protect(lua_State *L) {

0 commit comments

Comments
 (0)