From e5b88ead92c07a8948c132922a002e2d2cc76acd Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Mon, 26 Jul 2021 11:52:43 +0200 Subject: [PATCH] Make it possible to pass any number of arguments to the print functions (#1635) --- src/scripting/scriptengine_lua.inl | 61 ++++++++++++++++-------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/scripting/scriptengine_lua.inl b/src/scripting/scriptengine_lua.inl index 8d550bbeca..a8b0fec9e2 100644 --- a/src/scripting/scriptengine_lua.inl +++ b/src/scripting/scriptengine_lua.inl @@ -30,37 +30,42 @@ namespace openspace::luascriptfunctions { int printInternal(ghoul::logging::LogLevel level, lua_State* L) { - ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::printInternal"); - using ghoul::lua::luaTypeToString; - const int type = lua_type(L, 1); - switch (type) { - case LUA_TNONE: - case LUA_TLIGHTUSERDATA: - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TUSERDATA: - case LUA_TTHREAD: - log( - level, - "print", - fmt::format("Function parameter was of type '{}'", luaTypeToString(type)) - ); - break; - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - log(level, "print", std::to_string(ghoul::lua::value(L, 1))); - break; - case LUA_TNUMBER: - log(level, "print", std::to_string(ghoul::lua::value(L, 1))); - break; - case LUA_TSTRING: - log(level, "print", ghoul::lua::value(L, 1)); - break; + const int nArguments = lua_gettop(L); + for (int i = 1; i <= nArguments; i++) { + const int type = lua_type(L, i); + switch (type) { + case LUA_TNONE: + case LUA_TLIGHTUSERDATA: + case LUA_TTABLE: + case LUA_TFUNCTION: + case LUA_TUSERDATA: + case LUA_TTHREAD: + log( + level, + "print", + fmt::format( + "Function parameter was of type '{}'", luaTypeToString(type) + ) + ); + break; + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + log(level, "print", std::to_string(ghoul::lua::value(L, i))); + break; + case LUA_TNUMBER: + log(level, "print", std::to_string(ghoul::lua::value(L, i))); + break; + case LUA_TSTRING: + log(level, "print", ghoul::lua::value(L, i)); + break; + } } - lua_pop(L, 1); + + + lua_pop(L, nArguments); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0;