From b61a225d2b083fb5573fa71ea00da5490eca9675 Mon Sep 17 00:00:00 2001 From: "M. Liebmann" Date: Mon, 17 Mar 2014 21:44:17 +0100 Subject: [PATCH] CLuaInstance: Optional specification of parameters for runScript() - Add parameters as std::vector and possibility of return of status and error messages. (THX Martii) Example: void runScript(const char *fileName, std::vector *argv, std::string *result_code, std::string *result_string, std::string *error_string); - Add Parameters as const char*, last parameter to NULL is imperative. Example: void runScript(const char *fileName, const char *arg0, const char *arg1, ..., NULL); --- src/gui/luainstance.cpp | 45 ++++++++++++++++++++++++++++++++++++++++- src/gui/luainstance.h | 8 +++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/gui/luainstance.cpp b/src/gui/luainstance.cpp index 31871e117..13083b6a2 100644 --- a/src/gui/luainstance.cpp +++ b/src/gui/luainstance.cpp @@ -327,7 +327,7 @@ void CLuaInstance::functionDeprecated(lua_State *L, const char* oldFunc, const c lua_setglobal(lua, #NAME); /* Run the given script. */ -void CLuaInstance::runScript(const char *fileName) +void CLuaInstance::runScript(const char *fileName, std::vector *argv, std::string *result_code, std::string *result_string, std::string *error_string) { // luaL_dofile(lua, fileName); /* run the script */ @@ -335,17 +335,60 @@ void CLuaInstance::runScript(const char *fileName) if (status) { fprintf(stderr, "[CLuaInstance::%s] Can't load file: %s\n", __func__, lua_tostring(lua, -1)); ShowMsg2UTF("Lua script error:", lua_tostring(lua, -1), CMsgBox::mbrBack, CMsgBox::mbBack); + if (error_string) + *error_string = std::string(lua_tostring(lua, -1)); return; } set_lua_variables(lua); + if (argv && (!argv->empty())) { + lua_createtable(lua, argv->size(), 0); + int n = 0; + for(std::vector::iterator it = argv->begin(); it != argv->end(); ++it) { + lua_pushstring(lua, it->c_str()); + lua_rawseti(lua, -2, n++); + } + lua_setglobal(lua, "arg"); + } status = lua_pcall(lua, 0, LUA_MULTRET, 0); + if (result_code) + *result_code = to_string(status); + if (result_string && lua_isstring(lua, -1)) + *result_string = std::string(lua_tostring(lua, -1)); if (status) { fprintf(stderr, "[CLuaInstance::%s] error in script: %s\n", __func__, lua_tostring(lua, -1)); ShowMsg2UTF("Lua script error:", lua_tostring(lua, -1), CMsgBox::mbrBack, CMsgBox::mbBack); + if (error_string) + *error_string = std::string(lua_tostring(lua, -1)); } } +// Example: runScript(fileName, "Arg1", "Arg2", "Arg3", ..., NULL); +// Type of all parameters: const char* +// The last parameter to NULL is imperative. +void CLuaInstance::runScript(const char *fileName, const char *arg0, ...) +{ + int i = 0; + std::vector args; + args.push_back(arg0); + va_list list; + va_start(list, arg0); + const char* temp = va_arg(list, const char*); + while (temp != NULL) { + if (i >= 64) { + fprintf(stderr, "CLuaInstance::runScript: too many arguments!\n"); + args.clear(); + return; + } + args.push_back(temp); + temp = va_arg(list, const char*); + i++; + } + va_end(list); + runScript(fileName, &args); + args.clear(); +} + const luaL_Reg CLuaInstance::methods[] = { { "PaintBox", CLuaInstance::PaintBox }, diff --git a/src/gui/luainstance.h b/src/gui/luainstance.h index 245a74459..035dea282 100644 --- a/src/gui/luainstance.h +++ b/src/gui/luainstance.h @@ -30,6 +30,7 @@ extern "C" { #include #include #include +#include /* this is stored as userdata in the lua_State */ struct CLuaData @@ -164,7 +165,12 @@ class CLuaInstance public: CLuaInstance(); ~CLuaInstance(); - void runScript(const char *fileName); + void runScript(const char *fileName, std::vector *argv = NULL, std::string *result_code = NULL, std::string *result_string = NULL, std::string *error_string = NULL); + + // Example: runScript(fileName, "Arg1", "Arg2", "Arg3", ..., NULL); + // Type of all parameters: const char* + // The last parameter to NULL is imperative. + void runScript(const char *fileName, const char *arg0, ...); private: lua_State* lua;