Comparison

util-src/compat.c @ 9561:cfc7b2f7251e

util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
author Matthew Wild <mwild1@gmail.com>
date Fri, 26 Oct 2018 19:29:08 +0100
child 12976:a187600ec7d6
comparison
equal deleted inserted replaced
9560:cb92e1c8b6db 9561:cfc7b2f7251e
1
2 #include <lua.h>
3 #include <lauxlib.h>
4
5
6 static int lc_xpcall (lua_State *L) {
7 int ret;
8 int n_arg = lua_gettop(L);
9 /* f, msgh, p1, p2... */
10 luaL_argcheck(L, n_arg >= 2, 2, "value expected");
11 lua_pushvalue(L, 1); /* f to top */
12 lua_pushvalue(L, 2); /* msgh to top */
13 lua_replace(L, 1); /* msgh to 1 */
14 lua_replace(L, 2); /* f to 2 */
15 /* msgh, f, p1, p2... */
16 ret = lua_pcall(L, n_arg - 2, LUA_MULTRET, 1);
17 lua_pushboolean(L, ret == 0);
18 lua_replace(L, 1);
19 return lua_gettop(L);
20 }
21
22 int luaopen_util_compat(lua_State *L) {
23 lua_createtable(L, 0, 2);
24 {
25 lua_pushcfunction(L, lc_xpcall);
26 lua_setfield(L, -2, "xpcall");
27 }
28 return 1;
29 }