refactor generic lua function caller to Lua ns

develop
myk002 2022-11-02 13:50:52 -07:00
parent 0633d99059
commit 94c6bc8063
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 45 additions and 27 deletions

@ -820,6 +820,29 @@ bool DFHack::Lua::SafeCall(color_ostream &out, lua_State *L, int nargs, int nres
return ok; return ok;
} }
bool DFHack::Lua::CallLuaModuleFunction(color_ostream &out, lua_State *L,
const char *module_name, const char *fn_name,
int nargs, int nres, LuaLambda && args_lambda, LuaLambda && res_lambda,
bool perr){
if (!lua_checkstack(L, 1 + nargs) ||
!Lua::PushModulePublic(out, L, module_name, fn_name)) {
if (perr)
out.printerr("Failed to load %s Lua code\n", module_name);
return false;
}
std::forward<LuaLambda&&>(args_lambda)(L);
if (!Lua::SafeCall(out, L, nargs, nres, perr)) {
if (perr)
out.printerr("Failed Lua call to '%s.%s'\n", module_name, fn_name);
return false;
}
std::forward<LuaLambda&&>(res_lambda)(L);
return true;
}
// Copied from lcorolib.c, with error handling modifications // Copied from lcorolib.c, with error handling modifications
static int resume_helper(lua_State *L, lua_State *co, int narg, int nres) static int resume_helper(lua_State *L, lua_State *co, int narg, int nres)
{ {

@ -24,6 +24,7 @@ distribution.
#pragma once #pragma once
#include <functional>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
@ -218,6 +219,20 @@ namespace DFHack {namespace Lua {
*/ */
DFHACK_EXPORT bool SafeCall(color_ostream &out, lua_State *state, int nargs, int nres, bool perr = true); DFHACK_EXPORT bool SafeCall(color_ostream &out, lua_State *state, int nargs, int nres, bool perr = true);
/**
* Load named module and function and invoke it via SafeCall. Returns true
* on success. If an error is signalled, and perr is true, it is printed and
* popped from the stack.
*/
typedef std::function<void(lua_State *)> LuaLambda;
static auto DEFAULT_LUA_LAMBDA = [](lua_State *){};
DFHACK_EXPORT bool CallLuaModuleFunction(color_ostream &out,
lua_State *state, const char *module_name, const char *fn_name,
int nargs = 0, int nres = 0,
LuaLambda && args_lambda = DEFAULT_LUA_LAMBDA,
LuaLambda && res_lambda = DEFAULT_LUA_LAMBDA,
bool perr = true);
/** /**
* Pops a function from the top of the stack, and pushes a new coroutine. * Pops a function from the top of the stack, and pushes a new coroutine.
*/ */

@ -102,9 +102,10 @@ namespace DFHack {
static df::coord2d screenSize; static df::coord2d screenSize;
template<typename FA, typename FR> static void call_overlay_lua(color_ostream *out, const char *fn_name,
static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs, int nargs = 0, int nres = 0,
int nres, FA && args_lambda, FR && res_lambda) { Lua::LuaLambda && args_lambda = Lua::DEFAULT_LUA_LAMBDA,
Lua::LuaLambda && res_lambda = Lua::DEFAULT_LUA_LAMBDA) {
DEBUG(event).print("calling overlay lua function: '%s'\n", fn_name); DEBUG(event).print("calling overlay lua function: '%s'\n", fn_name);
CoreSuspender guard; CoreSuspender guard;
@ -115,30 +116,9 @@ static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs,
if (!out) if (!out)
out = &Core::getInstance().getConsole(); out = &Core::getInstance().getConsole();
if (!lua_checkstack(L, 1 + nargs) || Lua::CallLuaModuleFunction(*out, L, "plugins.overlay", fn_name, nargs, nres,
!Lua::PushModulePublic( std::forward<Lua::LuaLambda&&>(args_lambda),
*out, L, "plugins.overlay", fn_name)) { std::forward<Lua::LuaLambda&&>(res_lambda));
out->printerr("Failed to load overlay Lua code\n");
return;
}
std::forward<FA&&>(args_lambda)(L);
if (!Lua::SafeCall(*out, L, nargs, nres))
out->printerr("Failed Lua call to '%s'\n", fn_name);
std::forward<FR&&>(res_lambda)(L);
}
static auto DEFAULT_LAMBDA = [](lua_State *){};
template<typename FA>
static void call_overlay_lua(color_ostream *out, const char *fn_name, int nargs,
int nres, FA && args_lambda) {
call_overlay_lua(out, fn_name, nargs, nres, args_lambda, DEFAULT_LAMBDA);
}
static void call_overlay_lua(color_ostream *out, const char *fn_name) {
call_overlay_lua(out, fn_name, 0, 0, DEFAULT_LAMBDA, DEFAULT_LAMBDA);
} }
template<class T> template<class T>