util-src/compat.c
author Kim Alvefur <zash@zash.se>
Wed, 27 Mar 2024 19:33:11 +0100
changeset 13471 c2a476f4712a
parent 12980 a187600ec7d6
permissions -rw-r--r--
util.startup: Fix exiting on pidfile trouble prosody.shutdown() relies on prosody.main_thread, which has not been set yet at this point. Doing a clean shutdown might actually be harmful in case it tears down things set up by the conflicting Prosody, such as the very pidfile we were looking at. Thanks again SigmaTel71 for noticing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9564
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
#include <lua.h>
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
#include <lauxlib.h>
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
static int lc_xpcall (lua_State *L) {
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
  int ret;
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
  int n_arg = lua_gettop(L);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
  /* f, msgh, p1, p2... */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
  luaL_argcheck(L, n_arg >= 2, 2, "value expected");
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
  lua_pushvalue(L, 1);  /* f to top */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
  lua_pushvalue(L, 2);  /* msgh to top */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
  lua_replace(L, 1); /* msgh to 1 */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
  lua_replace(L, 2); /* f to 2 */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
  /* msgh, f, p1, p2... */
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
  ret = lua_pcall(L, n_arg - 2, LUA_MULTRET, 1);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
  lua_pushboolean(L, ret == 0);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
  lua_replace(L, 1);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
  return lua_gettop(L);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
}
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
12980
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 9564
diff changeset
    22
int luaopen_prosody_util_compat(lua_State *L) {
9564
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	lua_createtable(L, 0, 2);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	{
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		lua_pushcfunction(L, lc_xpcall);
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		lua_setfield(L, -2, "xpcall");
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
	}
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	return 1;
cfc7b2f7251e util.xpcall, util.compat: Add non-hacky multi-argument xpcall() for Lua 5.1
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
}
12980
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 9564
diff changeset
    30
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 9564
diff changeset
    31
int luaopen_util_compat(lua_State *L) {
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 9564
diff changeset
    32
	return luaopen_prosody_util_compat(L);
a187600ec7d6 util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents: 9564
diff changeset
    33
}