|
9 | 9 |
|
10 | 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 | 22 | * Internal function prototypes.
|
14 | 23 | \*=========================================================================*/
|
@@ -73,14 +82,30 @@ static int unwrap(lua_State *L) {
|
73 | 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 | 103 | static int protected_(lua_State *L) {
|
| 104 | + int status; |
77 | 105 | lua_pushvalue(L, lua_upvalueindex(1));
|
78 | 106 | 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); |
84 | 109 | }
|
85 | 110 |
|
86 | 111 | static int global_protect(lua_State *L) {
|
|
0 commit comments