From 6d7608037094a972626fd52860f0961e44a09fe3 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Thu, 15 Feb 2018 17:56:12 -0500 Subject: [PATCH] Simplify script precondition argument checks --- ext/ghoul | 2 +- include/openspace/scripting/script_helper.h | 35 ---------- .../base/rendering/screenspacedashboard.cpp | 14 ++-- .../globebrowsing/globebrowsingmodule_lua.inl | 65 +++++-------------- modules/iswa/util/iswamanager.cpp | 1 - src/CMakeLists.txt | 1 - src/engine/moduleengine_lua.inl | 13 ++-- src/engine/wrapper/windowwrapper.cpp | 5 +- src/interaction/keybindingmanager_lua.inl | 38 ++++------- src/interaction/navigationhandler_lua.inl | 37 ++++------- src/mission/missionmanager_lua.inl | 34 ++++------ src/network/parallelconnection.cpp | 1 - src/network/parallelconnection_lua.inl | 12 ++-- src/rendering/dashboard_lua.inl | 10 +-- src/rendering/renderengine_lua.inl | 36 +++------- src/scene/assetloader.cpp | 50 +++++++------- src/scene/assetmanager.cpp | 2 - src/scene/assetmanager_lua.inl | 27 ++++---- src/scene/scene.cpp | 1 - src/scene/scene_lua.inl | 26 +++----- src/scripting/scriptengine_lua.inl | 60 ++++++----------- src/scripting/scriptscheduler_lua.inl | 22 +++---- src/scripting/systemcapabilitiesbinding.cpp | 11 ++-- src/util/spicemanager_lua.inl | 11 +--- src/util/time_lua.inl | 11 +--- 25 files changed, 171 insertions(+), 354 deletions(-) delete mode 100644 include/openspace/scripting/script_helper.h diff --git a/ext/ghoul b/ext/ghoul index 96d695afba..dcbf53a697 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 96d695afba4cf8c06f11d64377228c7d665c3cee +Subproject commit dcbf53a6972bcfddf3136fc89258db8c4f22dbd3 diff --git a/include/openspace/scripting/script_helper.h b/include/openspace/scripting/script_helper.h deleted file mode 100644 index b6ad1cf90d..0000000000 --- a/include/openspace/scripting/script_helper.h +++ /dev/null @@ -1,35 +0,0 @@ -/***************************************************************************************** - * * - * OpenSpace * - * * - * Copyright (c) 2014-2018 * - * * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this * - * software and associated documentation files (the "Software"), to deal in the Software * - * without restriction, including without limitation the rights to use, copy, modify, * - * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * - * permit persons to whom the Software is furnished to do so, subject to the following * - * conditions: * - * * - * The above copyright notice and this permission notice shall be included in all copies * - * or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * - * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * - * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * - * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * - * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * - ****************************************************************************************/ - -#ifndef __OPENSPACE_CORE___SCRIPT_HELPER___H__ -#define __OPENSPACE_CORE___SCRIPT_HELPER___H__ - -#define SCRIPT_CHECK_ARGUMENTS(__category__, __stack__, __reqArg__, __realArg__) \ - if (__realArg__ != __reqArg__) { \ - LERRORC(__category__, ghoul::lua::errorLocation(__stack__) << "Expected " << \ - __reqArg__ << " arguments, got " << __realArg__); \ - return 0; \ - } - -#endif // __OPENSPACE_CORE___SCRIPT_HELPER___H__ diff --git a/modules/base/rendering/screenspacedashboard.cpp b/modules/base/rendering/screenspacedashboard.cpp index 107fe9bbb7..df3bf45500 100644 --- a/modules/base/rendering/screenspacedashboard.cpp +++ b/modules/base/rendering/screenspacedashboard.cpp @@ -61,10 +61,7 @@ namespace luascriptfunctions { * addDashboardItemToScreenSpace(string, table): */ int addDashboardItemToScreenSpace(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 2) { - return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::addDashboardItemToScreenSpace"); std::string name = luaL_checkstring(L, -2); int type = lua_type(L, -1); @@ -97,6 +94,8 @@ int addDashboardItemToScreenSpace(lua_State* L) { } dash->dashboard().addDashboardItem(DashboardItem::createFromDictionary(d)); + + lua_settop(L, 0); return 0; } } @@ -106,12 +105,9 @@ int addDashboardItemToScreenSpace(lua_State* L) { * removeDashboardItemsFromScreenSpace(string): */ int removeDashboardItemsFromScreenSpace(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::removeDashboardItemsFromScreenSpace"); - std::string name = luaL_checkstring(L, -1); + std::string name = ghoul::lua::checkStringAndPop(L); std::shared_ptr ssr = OsEng.renderEngine().screenSpaceRenderable(name); diff --git a/modules/globebrowsing/globebrowsingmodule_lua.inl b/modules/globebrowsing/globebrowsingmodule_lua.inl index e4a14583d0..c35807d5c8 100644 --- a/modules/globebrowsing/globebrowsingmodule_lua.inl +++ b/modules/globebrowsing/globebrowsingmodule_lua.inl @@ -41,17 +41,14 @@ namespace openspace::globebrowsing::luascriptfunctions { *Adds a layer to the specified globe. */ int addLayer(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::addLayer"); + using ghoul::lua::errorLocation; // Argument locations const int GlobeLocation = -3; const int LayerGroupLocation = -2; - int nArguments = lua_gettop(L); - if (nArguments != 3) { - return luaL_error(L, "Expected %i arguments, got %i", 3, nArguments); - } - // String arguments const std::string GlobeName = luaL_checkstring(L, GlobeLocation); const std::string LayerGroupName = luaL_checkstring(L, LayerGroupLocation); @@ -99,6 +96,8 @@ int addLayer(lua_State* L) { Deletes a layer from the specified globe. */ int deleteLayer(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::deleteLayer"); + using ghoul::lua::errorLocation; // Argument locations @@ -106,11 +105,6 @@ int deleteLayer(lua_State* L) { const int LayerGroupLocation = -2; const int NameLocation = -1; - int nArguments = lua_gettop(L); - if (nArguments != 3) { - return luaL_error(L, "Expected %i arguments, got %i", 3, nArguments); - } - // String arguments const std::string GlobeName = luaL_checkstring(L, GlobeLocation); const std::string LayerGroupName = luaL_checkstring(L, LayerGroupLocation); @@ -143,13 +137,9 @@ int deleteLayer(lua_State* L) { } int goToChunk(lua_State* L) { - using ghoul::lua::luaTypeToString; + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::goToChunk"); - // Check arguments - int nArguments = lua_gettop(L); - if (nArguments != 3) { - return luaL_error(L, "Expected %i arguments, got %i", 3, nArguments); - } + using ghoul::lua::luaTypeToString; int x = static_cast(lua_tonumber(L, 1)); int y = static_cast(lua_tonumber(L, 2)); @@ -163,13 +153,10 @@ int goToChunk(lua_State* L) { } int goToGeo(lua_State* L) { + int nArguments = ghoul::lua::checkArgumentsAndThrow(L, 2, 3, "lua::goToGeo"); + using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 2 && nArguments != 3) { - return luaL_error(L, "Expected 2 or 3 arguments."); - } - double latitude = lua_tonumber(L, 1); double longitude = lua_tonumber(L, 2); @@ -188,10 +175,8 @@ int goToGeo(lua_State* L) { } int getGeoPosition(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) { - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::getGeoPosition"); + GlobeBrowsingModule* module = OsEng.moduleEngine().module(); RenderableGlobe* globe = module->castFocusNodeRenderableToGlobe(); if (!globe) { @@ -224,16 +209,11 @@ int getGeoPosition(lua_State* L) { #ifdef GLOBEBROWSING_USE_GDAL int loadWMSCapabilities(lua_State* L) { - int nArguments = lua_gettop(L); + ghoul::lua::checkArgumentsAndThrow(L, 3, "lua::loadWMSCapabilities"); - if (nArguments != 3) { - return luaL_error(L, "Expected %i arguments, got %i", 3, nArguments); - } - - std::string name = lua_tostring(L, -3); - std::string globe = lua_tostring(L, -2); - std::string url = lua_tostring(L, -1); - lua_settop(L, 0); + std::string url = ghoul::lua::checkStringAndPop(L); + std::string globe = ghoul::lua::checkStringAndPop(L); + std::string name = ghoul::lua::checkStringAndPop(L); OsEng.moduleEngine().module()->loadWMSCapabilities( std::move(name), @@ -246,15 +226,10 @@ int loadWMSCapabilities(lua_State* L) { } int removeWMSServer(lua_State* L) { - int nArguments = lua_gettop(L); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::removeWMSServer"); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + std::string name = ghoul::lua::checkStringAndPop(L); - std::string name = lua_tostring(L, -1); - - lua_settop(L, 0); OsEng.moduleEngine().module()->removeWMSServer(name); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); @@ -262,13 +237,9 @@ int removeWMSServer(lua_State* L) { } int capabilities(lua_State* L) { - int nArguments = lua_gettop(L); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::capabilities"); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } - - std::string name = lua_tostring(L, -1); + std::string name = ghoul::lua::checkStringAndPop(L); GlobeBrowsingModule::Capabilities cap = OsEng.moduleEngine().module()->capabilities(name); diff --git a/modules/iswa/util/iswamanager.cpp b/modules/iswa/util/iswamanager.cpp index 48bda3fc38..df829853f4 100644 --- a/modules/iswa/util/iswamanager.cpp +++ b/modules/iswa/util/iswamanager.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #include #include diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 758585038b..58dde2a0ea 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -323,7 +323,6 @@ set(OPENSPACE_HEADER ${OPENSPACE_BASE_DIR}/include/openspace/scene/scenelicensewriter.h ${OPENSPACE_BASE_DIR}/include/openspace/scene/scenegraphnode.h ${OPENSPACE_BASE_DIR}/include/openspace/scripting/lualibrary.h - ${OPENSPACE_BASE_DIR}/include/openspace/scripting/script_helper.h ${OPENSPACE_BASE_DIR}/include/openspace/scripting/scriptengine.h ${OPENSPACE_BASE_DIR}/include/openspace/scripting/scriptscheduler.h ${OPENSPACE_BASE_DIR}/include/openspace/scripting/systemcapabilitiesbinding.h diff --git a/src/engine/moduleengine_lua.inl b/src/engine/moduleengine_lua.inl index 8176fabf44..e2db06bcc0 100644 --- a/src/engine/moduleengine_lua.inl +++ b/src/engine/moduleengine_lua.inl @@ -33,13 +33,12 @@ namespace openspace::luascriptfunctions { * Checks whether the passed OpenSpaceModule is loaded or not */ int isLoaded(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::isLoaded"); const int type = lua_type(L, -1); - if (type != LUA_TSTRING) + if (type != LUA_TSTRING) { return luaL_error(L, "Expected argument of type 'string'"); + } std::string moduleName = lua_tostring(L, -1); std::vector modules = OsEng.moduleEngine().modules(); @@ -52,10 +51,12 @@ int isLoaded(lua_State* L) { } ); - if (it != modules.end()) + if (it != modules.end()) { lua_pushboolean(L, 1); - else + } + else { lua_pushboolean(L, 0); + } ghoul_assert(lua_gettop(L) == 1, "Incorrect number of items left on stack"); return 1; diff --git a/src/engine/wrapper/windowwrapper.cpp b/src/engine/wrapper/windowwrapper.cpp index c77d7286fc..3cc0d7fcad 100644 --- a/src/engine/wrapper/windowwrapper.cpp +++ b/src/engine/wrapper/windowwrapper.cpp @@ -34,10 +34,7 @@ namespace luascriptfunctions { int setSynchronization(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::setSynchronization"); bool b = lua_toboolean(L, -1) != 0; OsEng.windowWrapper().setSynchronization(b); diff --git a/src/interaction/keybindingmanager_lua.inl b/src/interaction/keybindingmanager_lua.inl index ec1f0d0096..6dfece8285 100644 --- a/src/interaction/keybindingmanager_lua.inl +++ b/src/interaction/keybindingmanager_lua.inl @@ -34,10 +34,7 @@ namespace openspace::luascriptfunctions { int bindKey(lua_State* L) { using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 2 && nArguments != 3) { - return luaL_error(L, "Expected %i or %i arguments, got %i", 2, 3, nArguments); - } + int nArguments = ghoul::lua::checkArgumentsAndThrow(L, 2, 3, "lua::bindKey"); int KeyLocation = nArguments == 3 ? -3 : -2; int CommandLocation = nArguments == 3 ? -2 : -1; @@ -69,7 +66,7 @@ int bindKey(lua_State* L) { std::move(documentation) ); - lua_settop(L, 0); + lua_pop(L, nArguments); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } @@ -82,10 +79,8 @@ int bindKey(lua_State* L) { int bindKeyLocal(lua_State* L) { using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 2 && nArguments != 3) { - return luaL_error(L, "Expected %i or %i arguments, got %i", 2, 3, nArguments); - } + int nArguments = ghoul::lua::checkArgumentsAndThrow(L, 2, 3, "lua::bindKeyLocal"); + int KeyLocation = nArguments == 3 ? -3 : -2; int CommandLocation = nArguments == 3 ? -2 : -1; @@ -115,9 +110,9 @@ int bindKeyLocal(lua_State* L) { iKey.modifier, std::move(command), std::move(documentation) - ); + ); - lua_settop(L, 0); + lua_pop(L, nArguments); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } @@ -129,13 +124,9 @@ int bindKeyLocal(lua_State* L) { * local or remote key binds */ int getKeyBindings(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::getKeyBindings"); - std::string key = luaL_checkstring(L, -1); - lua_settop(L, 0); + std::string key = ghoul::lua::checkStringAndPop(L); using KeyInformation = interaction::KeyBindingManager::KeyInformation; @@ -169,13 +160,9 @@ int getKeyBindings(lua_State* L) { * Clears the keybinding of the key named as argument */ int clearKey(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::clearKey"); - std::string key = luaL_checkstring(L, -1); - lua_settop(L, 0); + std::string key = ghoul::lua::checkStringAndPop(L); OsEng.keyBindingManager().removeKeyBinding(key); @@ -189,10 +176,7 @@ int clearKey(lua_State* L) { * Clears all key bindings */ int clearKeys(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) { - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::clearKeys"); OsEng.keyBindingManager().resetKeyBindings(); diff --git a/src/interaction/navigationhandler_lua.inl b/src/interaction/navigationhandler_lua.inl index 173fa84dee..9839d56fe1 100644 --- a/src/interaction/navigationhandler_lua.inl +++ b/src/interaction/navigationhandler_lua.inl @@ -25,30 +25,24 @@ namespace openspace::luascriptfunctions { int restoreCameraStateFromFile(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::restoreCameraStateFromFile"); + using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } - - std::string cameraStateFilePath = luaL_checkstring(L, -1); - lua_settop(L, 0); + std::string cameraStateFilePath = ghoul::lua::checkStringAndPop(L); if (cameraStateFilePath.empty()) { return luaL_error(L, "filepath string is empty"); } OsEng.navigationHandler().restoreCameraStateFromFile(cameraStateFilePath); + ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } int setCameraState(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::setCameraState"); try { ghoul::Dictionary dictionary; @@ -59,40 +53,37 @@ int setCameraState(lua_State* L) { return luaL_error(L, "Could not set camera state: %s", e.what()); } + // @CLEANUP: When luaDictionaryFromState doesn't leak space anymore, remove the next + // line ---abock(2018-02-15) lua_settop(L, 0); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } int saveCameraStateToFile(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::saveCameraStateToFile"); + using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } - - std::string cameraStateFilePath = luaL_checkstring(L, -1); - lua_settop(L, 0); + std::string cameraStateFilePath = ghoul::lua::checkStringAndPop(L); if (cameraStateFilePath.empty()) { return luaL_error(L, "filepath string is empty"); } OsEng.navigationHandler().saveCameraStateToFile(cameraStateFilePath); + ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } int resetCameraDirection(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::resetCameraDirection"); + using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - if (nArguments != 0) { - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - } - OsEng.navigationHandler().resetCameraDirection(); + ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } diff --git a/src/mission/missionmanager_lua.inl b/src/mission/missionmanager_lua.inl index fac99a845d..24b998e230 100644 --- a/src/mission/missionmanager_lua.inl +++ b/src/mission/missionmanager_lua.inl @@ -25,15 +25,13 @@ namespace openspace::luascriptfunctions { int loadMission(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::loadMission"); - std::string missionFileName = luaL_checkstring(L, -1); + std::string missionFileName = ghoul::lua::checkStringAndPop(L); if (missionFileName.empty()) { return luaL_error(L, "Filepath is empty"); } + std::string name = MissionManager::ref().loadMission(absPath(missionFileName)); lua_pushstring(L, name.c_str()); @@ -42,14 +40,11 @@ int loadMission(lua_State* L) { } int unloadMission(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::unloadMission"); - std::string missionName = luaL_checkstring(L, -1); + std::string missionName = ghoul::lua::checkStringAndPop(L); if (missionName.empty()) { - return luaL_error(L, "Missing name is empty"); + return luaL_error(L, "Mission name is empty"); } if (!MissionManager::ref().hasMission(missionName)) { @@ -63,16 +58,13 @@ int unloadMission(lua_State* L) { } int hasMission(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::hasMission"); - std::string missionName = luaL_checkstring(L, -1); + std::string missionName = ghoul::lua::checkStringAndPop(L); if (missionName.empty()) { return luaL_error(L, "Missing name is empty"); } - + bool hasMission = MissionManager::ref().hasMission(missionName); lua_pushboolean(L, hasMission); @@ -82,15 +74,13 @@ int hasMission(lua_State* L) { } int setCurrentMission(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::setCurrentMission"); - std::string missionName = luaL_checkstring(L, -1); + std::string missionName = ghoul::lua::checkStringAndPop(L); if (missionName.empty()) { return luaL_error(L, "Mission name is empty"); } + MissionManager::ref().setCurrentMission(missionName); ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); diff --git a/src/network/parallelconnection.cpp b/src/network/parallelconnection.cpp index a71a21b898..b8672de32b 100644 --- a/src/network/parallelconnection.cpp +++ b/src/network/parallelconnection.cpp @@ -82,7 +82,6 @@ using SocketResultType = size_t; #include #include #include -#include #include #include diff --git a/src/network/parallelconnection_lua.inl b/src/network/parallelconnection_lua.inl index 02ddb5aba4..df8ae51841 100644 --- a/src/network/parallelconnection_lua.inl +++ b/src/network/parallelconnection_lua.inl @@ -25,8 +25,7 @@ namespace openspace::luascriptfunctions { int connect(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("connect", L, 0, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::connect"); if (OsEng.windowWrapper().isMaster()) { OsEng.parallelConnection().clientConnect(); @@ -37,8 +36,7 @@ int connect(lua_State* L) { } int disconnect(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("disconnect", L, 0, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::disconnect"); if (OsEng.windowWrapper().isMaster()) { OsEng.parallelConnection().signalDisconnect(); @@ -49,8 +47,7 @@ int disconnect(lua_State* L) { } int requestHostship(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("requestHostship", L, 0, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::requestHostship"); if (OsEng.windowWrapper().isMaster()) { OsEng.parallelConnection().requestHostship(); @@ -61,8 +58,7 @@ int requestHostship(lua_State* L) { } int resignHostship(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("resignHostship", L, 0, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::resignHostship"); if (OsEng.windowWrapper().isMaster()) { OsEng.parallelConnection().resignHostship(); diff --git a/src/rendering/dashboard_lua.inl b/src/rendering/dashboard_lua.inl index bc8f29eb58..90de001788 100644 --- a/src/rendering/dashboard_lua.inl +++ b/src/rendering/dashboard_lua.inl @@ -33,10 +33,7 @@ namespace openspace::luascriptfunctions { * addDashboardItem(table): */ int addDashboardItem(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::addDashboardItem"); int type = lua_type(L, -1); if (type == LUA_TTABLE) { @@ -66,10 +63,7 @@ int addDashboardItem(lua_State* L) { * removeDashboardItems(): */ int removeDashboardItems(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments > 1) { - return luaL_error(L, "Expected %i or %i arguments, got %i", 0, 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::removeDashboardItems"); OsEng.dashboard().removeDashboardItems(); diff --git a/src/rendering/renderengine_lua.inl b/src/rendering/renderengine_lua.inl index 463bfdb592..c41da879b7 100644 --- a/src/rendering/renderengine_lua.inl +++ b/src/rendering/renderengine_lua.inl @@ -30,10 +30,7 @@ namespace openspace::luascriptfunctions { * Set renderer */ int setRenderer(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::setRenderer"); const int type = lua_type(L, -1); if (type != LUA_TSTRING) { @@ -52,10 +49,7 @@ int setRenderer(lua_State* L) { * Toggle a global fade over (float) seconds */ int toggleFade(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::toggleFade"); double t = luaL_checknumber(L, -1); @@ -74,10 +68,7 @@ int toggleFade(lua_State* L) { * start a global fadein over (float) seconds */ int fadeIn(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::fadeIn"); double t = luaL_checknumber(L, -1); @@ -92,10 +83,7 @@ int fadeIn(lua_State* L) { * start a global fadeout over (float) seconds */ int fadeOut(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::fadeOut"); double t = luaL_checknumber(L, -1); @@ -106,12 +94,9 @@ int fadeOut(lua_State* L) { } int addScreenSpaceRenderable(lua_State* L) { - using ghoul::lua::errorLocation; + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::addScreenSpaceRenderable"); - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + using ghoul::lua::errorLocation; ghoul::Dictionary d; try { @@ -132,14 +117,11 @@ int addScreenSpaceRenderable(lua_State* L) { } int removeScreenSpaceRenderable(lua_State* L) { + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::removeScreenSpaceRenderable"); + using ghoul::lua::errorLocation; - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } - - std::string name = luaL_checkstring(L, -1); + std::string name = ghoul::lua::checkStringAndPop(L); std::shared_ptr s = OsEng.renderEngine().screenSpaceRenderable( name diff --git a/src/scene/assetloader.cpp b/src/scene/assetloader.cpp index 97f35d7bcf..b69063f9ee 100644 --- a/src/scene/assetloader.cpp +++ b/src/scene/assetloader.cpp @@ -25,7 +25,6 @@ #include #include -#include #include #include @@ -389,39 +388,43 @@ std::shared_ptr AssetLoader::getAsset(std::string name) { } int AssetLoader::onInitializeLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("onInitialize", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::onInitialize"); + int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onInitializationFunctionRefs[asset].push_back(referenceIndex); + lua_settop(*_luaState, 0); return 0; } int AssetLoader::onDeinitializeLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("onDeinitialize", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::onDeinitialize"); + int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDeinitializationFunctionRefs[asset].push_back(referenceIndex); + lua_settop(*_luaState, 0); return 0; } int AssetLoader::onInitializeDependencyLua(Asset* dependant, Asset* dependency) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("onInitialize", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::onInitializeDependency"); + int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDependencyInitializationFunctionRefs[dependant][dependency] .push_back(referenceIndex); + lua_settop(*_luaState, 0); return 0; } int AssetLoader::onDeinitializeDependencyLua(Asset* dependant, Asset* dependency) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("onDeinitialize", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::onDeinitializeDependency"); + int referenceIndex = luaL_ref(*_luaState, LUA_REGISTRYINDEX); _onDependencyDeinitializationFunctionRefs[dependant][dependency] .push_back(referenceIndex); + lua_settop(*_luaState, 0); return 0; } @@ -496,7 +499,7 @@ void AssetLoader::callOnInitialize(Asset* asset) { if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { throw ghoul::lua::LuaRuntimeException( "When initializing " + asset->assetFilePath() + ": " + - luaL_checkstring(*_luaState, -1) + ghoul::lua::checkStringAndPop(*_luaState) ); } // Clean up lua stack, in case the pcall left anything there. @@ -511,7 +514,7 @@ void AssetLoader::callOnDeinitialize(Asset * asset) { if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { throw ghoul::lua::LuaRuntimeException( "When deinitializing " + asset->assetFilePath() + ": " + - luaL_checkstring(*_luaState, -1) + ghoul::lua::checkStringAndPop(*_luaState) ); } // Clean up lua stack, in case the pcall left anything there. @@ -525,7 +528,7 @@ void AssetLoader::callOnDependencyInitialize(Asset* asset, Asset* dependant) { if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { throw ghoul::lua::LuaRuntimeException( "When initializing dependency " + dependant->assetFilePath() + " -> " + - asset->assetFilePath() + ": " + luaL_checkstring(*_luaState, -1) + asset->assetFilePath() + ": " + ghoul::lua::checkStringAndPop(*_luaState) ); } // Clean up lua stack, in case the pcall left anything there. @@ -543,7 +546,7 @@ void AssetLoader::callOnDependencyDeinitialize(Asset* asset, Asset* dependant) { if (lua_pcall(*_luaState, 0, 0, 0) != LUA_OK) { throw ghoul::lua::LuaRuntimeException( "When deinitializing dependency " + dependant->assetFilePath() + " -> " + - asset->assetFilePath() + ": " + luaL_checkstring(*_luaState, -1) + asset->assetFilePath() + ": " + ghoul::lua::checkStringAndPop(*_luaState) ); } // Clean up lua stack, in case the pcall left anything there. @@ -552,13 +555,11 @@ void AssetLoader::callOnDependencyDeinitialize(Asset* asset, Asset* dependant) { } int AssetLoader::localResourceLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("localResource", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::localResourceLua"); - std::string resourceName = luaL_checkstring(*_luaState, -1); + std::string resourceName = ghoul::lua::checkStringAndPop(*_luaState); std::string resolved = asset->resolveLocalResource(resourceName); - lua_settop(*_luaState, 0); lua_pushstring(*_luaState, resolved.c_str()); ghoul_assert(lua_gettop(*_luaState) == 1, "Incorrect number of items left on stack"); @@ -566,8 +567,7 @@ int AssetLoader::localResourceLua(Asset* asset) { } int AssetLoader::syncedResourceLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("syncedResource", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::syncedResourceLua"); ghoul::Dictionary d; ghoul::lua::luaDictionaryFromState(*_luaState, d); @@ -608,8 +608,7 @@ void AssetLoader::setCurrentAsset(std::shared_ptr asset) { } int AssetLoader::requireLua(Asset* dependant) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("require", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::require"); std::string assetName = luaL_checkstring(*_luaState, 1); lua_settop(*_luaState, 0); @@ -647,8 +646,7 @@ int AssetLoader::requireLua(Asset* dependant) { } int AssetLoader::requestLua(Asset* parent) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("request", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::request"); std::string assetName = luaL_checkstring(*_luaState, 1); lua_settop(*_luaState, 0); @@ -674,8 +672,7 @@ int AssetLoader::requestLua(Asset* parent) { } int AssetLoader::existsLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("exists", *_luaState, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 1, "lua::exists"); std::string assetName = luaL_checkstring(*_luaState, 1); @@ -689,8 +686,7 @@ int AssetLoader::existsLua(Asset* asset) { } int AssetLoader::exportAssetLua(Asset* asset) { - int nArguments = lua_gettop(*_luaState); - SCRIPT_CHECK_ARGUMENTS("exportAsset", *_luaState, 2, nArguments); + ghoul::lua::checkArgumentsAndThrow(*_luaState, 2, "lua::exportAsset"); std::string exportName = luaL_checkstring(*_luaState, 1); diff --git a/src/scene/assetmanager.cpp b/src/scene/assetmanager.cpp index 7f8669ecdd..e91abf9b1a 100644 --- a/src/scene/assetmanager.cpp +++ b/src/scene/assetmanager.cpp @@ -24,8 +24,6 @@ #include -#include - #include #include #include diff --git a/src/scene/assetmanager_lua.inl b/src/scene/assetmanager_lua.inl index 5b79682208..a02c6ad58a 100644 --- a/src/scene/assetmanager_lua.inl +++ b/src/scene/assetmanager_lua.inl @@ -25,34 +25,39 @@ namespace openspace::luascriptfunctions::asset { int add(lua_State* state) { - AssetManager *assetManager = + ghoul::lua::checkArgumentsAndThrow(state, 1, "lua::add"); + + AssetManager* assetManager = reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("add", state, 1, nArguments); - std::string assetName = luaL_checkstring(state, -1); + std::string assetName = ghoul::lua::checkStringAndPop(state); assetManager->add(assetName); + return 0; } int remove(lua_State* state) { - AssetManager *assetManager = + ghoul::lua::checkArgumentsAndThrow(state, 1, "lua::remove"); + + + AssetManager* assetManager = reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("remove", state, 1, nArguments); - std::string assetName = luaL_checkstring(state, -1); + std::string assetName = ghoul::lua::checkStringAndPop(state); assetManager->remove(assetName); + return 0; } int removeAll(lua_State* state) { - AssetManager *assetManager = + ghoul::lua::checkArgumentsAndThrow(state, 0, "lua::removeAll"); + + + AssetManager* assetManager = reinterpret_cast(lua_touserdata(state, lua_upvalueindex(1))); - int nArguments = lua_gettop(state); - SCRIPT_CHECK_ARGUMENTS("removeAll", state, 0, nArguments); assetManager->removeAll(); + return 0; } diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index b53e3247bb..4fbfdc25fc 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -34,7 +34,6 @@ #include #include #include -#include #include #include diff --git a/src/scene/scene_lua.inl b/src/scene/scene_lua.inl index 1d3050f0b9..2f0e22050d 100644 --- a/src/scene/scene_lua.inl +++ b/src/scene/scene_lua.inl @@ -169,8 +169,7 @@ int property_setValueSingle(lua_State* L) { using ghoul::lua::errorLocation; using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("property_setValueSingle", L, 2, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::property_setValueSingle"); std::string uri = luaL_checkstring(L, -2); const int type = lua_type(L, -1); @@ -226,8 +225,8 @@ int property_setValue(lua_State* L) { using ghoul::lua::errorLocation; using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("property_setGroup", L, 2, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::property_setGroup"); + std::string regex = luaL_checkstring(L, -2); std::string groupName; @@ -271,8 +270,8 @@ int property_setValueRegex(lua_State* L) { using ghoul::lua::errorLocation; using ghoul::lua::luaTypeToString; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("property_setValueRegex<", L, 2, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::property_setValueRegex"); + std::string regex = luaL_checkstring(L, -2); std::string groupName; @@ -309,8 +308,7 @@ int property_getValue(lua_State* L) { static const std::string _loggerCat = "property_getValue"; using ghoul::lua::errorLocation; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("property_getValue", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::property_getValue"); std::string uri = luaL_checkstring(L, -1); lua_settop(L, 0); @@ -338,8 +336,7 @@ int property_getValue(lua_State* L) { * be passed to the setPropertyValue method. */ int loadScene(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("loadScene", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::loadScene"); std::string sceneFile = luaL_checkstring(L, -1); OsEng.scheduleLoadSingleAsset(sceneFile); @@ -351,8 +348,7 @@ int loadScene(lua_State* L) { int addSceneGraphNode(lua_State* L) { using ghoul::lua::errorLocation; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("addSceneGraphNode", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::addSceneGraphNode"); ghoul::Dictionary d; try { @@ -391,8 +387,7 @@ int addSceneGraphNode(lua_State* L) { int removeSceneGraphNode(lua_State* L) { using ghoul::lua::errorLocation; - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("removeSceneGraphNode", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::removeSceneGraphNode"); std::string nodeName = luaL_checkstring(L, -1); SceneGraphNode* node = OsEng.renderEngine().scene()->sceneGraphNode(nodeName); @@ -421,8 +416,7 @@ int removeSceneGraphNode(lua_State* L) { int hasSceneGraphNode(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("hasSceneGraphNode", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::hasSceneGraphNode"); std::string nodeName = luaL_checkstring(L, -1); SceneGraphNode* node = OsEng.renderEngine().scene()->sceneGraphNode(nodeName); diff --git a/src/scripting/scriptengine_lua.inl b/src/scripting/scriptengine_lua.inl index c7e948f4be..91a61df7ae 100644 --- a/src/scripting/scriptengine_lua.inl +++ b/src/scripting/scriptengine_lua.inl @@ -31,10 +31,7 @@ namespace { // Defining a common walk function that works off a pointer-to-member function template int walkCommon(lua_State* L, Func func) { - const int nArguments = lua_gettop(L); - if (nArguments < 1 || nArguments > 3) { - return luaL_error(L, "Expected %i-%i arguments, got %i", 1, 3, nArguments); - } + int nArguments = ghoul::lua::checkArgumentsAndThrow(L, { 1, 3 }, "lua::walkCommon"); std::vector result; if (nArguments == 1) { @@ -87,13 +84,9 @@ int walkCommon(lua_State* L, Func func) { } // namespace int printInternal(ghoul::logging::LogLevel level, lua_State* L) { - using ghoul::lua::luaTypeToString; - const std::string _loggerCat = "print"; + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::printInternal"); - const int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + using ghoul::lua::luaTypeToString; const int type = lua_type(L, -1); switch (type) { @@ -195,13 +188,9 @@ int printFatal(lua_State* L) { * tokens and returns the absolute path. */ int absolutePath(lua_State* L) { - const int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %d arguments, got %d", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::absolutePath"); - std::string path = luaL_checkstring(L, -1); - lua_settop(L, 0); + std::string path = ghoul::lua::checkStringAndPop(L); path = absPath(path); //path = FileSys.convertPathSeparator(path, '/'); @@ -216,19 +205,17 @@ int absolutePath(lua_State* L) { * argument. If the path token already exists, it will be silently overridden. */ int setPathToken(lua_State* L) { - const int nArguments = lua_gettop(L); - if (nArguments != 2) { - return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 2, "lua::setPathToken"); + + const std::string path = ghoul::lua::checkStringAndPop(L); + const std::string pathToken = ghoul::lua::checkStringAndPop(L); - const std::string path = luaL_checkstring(L, -1); - const std::string pathToken = luaL_checkstring(L, -2); - lua_settop(L, 0); FileSys.registerPathToken( pathToken, path, ghoul::filesystem::FileSystem::Override::Yes ); + return 0; } @@ -238,16 +225,13 @@ int setPathToken(lua_State* L) { * Checks whether the provided file exists */ int fileExists(lua_State* L) { - const int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::fileExists"); - const std::string file = luaL_checkstring(L, -1); + const std::string file = ghoul::lua::checkStringAndPop(L); const bool e = FileSys.fileExists(absPath(file)); - lua_settop(L, 0); lua_pushboolean(L, (e ? 1 : 0)); + return 1; } @@ -257,15 +241,12 @@ int fileExists(lua_State* L) { * Checks whether the provided file exists */ int directoryExists(lua_State* L) { - const int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::directoryExists"); - const std::string file = luaL_checkstring(L, -1); + + const std::string file = ghoul::lua::checkStringAndPop(L); const bool e = FileSys.directoryExists(absPath(file)); - lua_settop(L, 0); lua_pushboolean(L, (e ? 1 : 0)); return 1; } @@ -319,16 +300,13 @@ int walkDirectoryFolder(lua_State* L) { * 'C:\\OpenSpace\\foobar'." */ int directoryForPath(lua_State* L) { - const int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::directoryForPath"); - const std::string file = luaL_checkstring(L, -1); + const std::string file = ghoul::lua::checkStringAndPop(L); const std::string path = ghoul::filesystem::File(file).directoryName(); - lua_settop(L, 0); lua_pushstring(L, path.c_str()); + return 1; } diff --git a/src/scripting/scriptscheduler_lua.inl b/src/scripting/scriptscheduler_lua.inl index 489108ab67..ab32cbf98c 100644 --- a/src/scripting/scriptscheduler_lua.inl +++ b/src/scripting/scriptscheduler_lua.inl @@ -25,12 +25,9 @@ namespace openspace::luascriptfunctions { int loadFile(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::loadFile"); - std::string missionFileName = luaL_checkstring(L, -1); + std::string missionFileName = ghoul::lua::checkStringAndPop(L); if (missionFileName.empty()) { return luaL_error(L, "filepath string is empty"); } @@ -44,7 +41,12 @@ int loadFile(lua_State* L) { } int loadScheduledScript(lua_State* L) { - int nArguments = lua_gettop(L); + int nArguments = ghoul::lua::checkArgumentsAndThrow( + L, + { 2, 4 }, + "lua::loadScheduledScript" + ); + if (nArguments == 2) { OsEng.scriptScheduler().loadScripts({ { @@ -81,19 +83,13 @@ int loadScheduledScript(lua_State* L) { } }); } - else { - return luaL_error(L, "Expected %i-%i arguments, got %i", 2, 4, nArguments); - } ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } int clear(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) { - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::clear"); OsEng.scriptScheduler().clearSchedule(); diff --git a/src/scripting/systemcapabilitiesbinding.cpp b/src/scripting/systemcapabilitiesbinding.cpp index 66c9bf3ee0..5c237bafd1 100644 --- a/src/scripting/systemcapabilitiesbinding.cpp +++ b/src/scripting/systemcapabilitiesbinding.cpp @@ -24,7 +24,6 @@ #include -#include #include #include @@ -84,10 +83,9 @@ int extensions(lua_State* L) { namespace luascripting::opengl { int hasOpenGLVersion(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("hasVersion", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::hasOpenGLVersion"); - std::vector v = ghoul::tokenizeString(luaL_checkstring(L, -1)); + std::vector v = ghoul::tokenizeString(ghoul::lua::checkStringAndPop(L)); if (v.size() != 2 && v.size() != 3) { LERRORC("hasVersion", ghoul::lua::errorLocation(L) << "Malformed version string"); return 0; @@ -144,10 +142,9 @@ int extensions(lua_State* L) { } int isExtensionSupported(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("hasVersion", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::hasExtension"); - std::string extension = luaL_checkstring(L, -1); + std::string extension = ghoul::lua::checkStringAndPop(L); lua_pushboolean(L, OpenGLCap.isExtensionSupported(extension)); return 1; diff --git a/src/util/spicemanager_lua.inl b/src/util/spicemanager_lua.inl index f93c4a32f1..330c167475 100644 --- a/src/util/spicemanager_lua.inl +++ b/src/util/spicemanager_lua.inl @@ -22,8 +22,6 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -#include - namespace openspace::luascriptfunctions { /** @@ -34,8 +32,7 @@ namespace openspace::luascriptfunctions { */ int loadKernel(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("loadKernel", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::loadKernel"); bool isString = (lua_isstring(L, -1) == 1); if (!isString) { @@ -43,13 +40,12 @@ int loadKernel(lua_State* L) { return 0; } - std::string argument = lua_tostring(L, -1); + std::string argument = ghoul::lua::checkStringAndPop(L); if (!FileSys.fileExists(argument)) { return luaL_error(L, "Kernel file '%s' did not exist", argument.c_str()); } unsigned int result = SpiceManager::ref().loadKernel(argument); - lua_settop(L, 0); lua_pushnumber(L, result); ghoul_assert(lua_gettop(L) == 1, "Incorrect number of items left on stack"); @@ -62,8 +58,7 @@ int loadKernel(lua_State* L) { * automatically resolved. */ int unloadKernel(lua_State* L) { - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS("unloadKernel", L, 1, nArguments); + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::loadKernel"); bool isString = (lua_isstring(L, -1) == 1); bool isNumber = (lua_isnumber(L, -1) == 1); diff --git a/src/util/time_lua.inl b/src/util/time_lua.inl index 9c9c36d65a..396a86f552 100644 --- a/src/util/time_lua.inl +++ b/src/util/time_lua.inl @@ -78,12 +78,10 @@ int time_deltaTime(lua_State* L) { * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards */ int time_togglePause(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) { - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::time_togglePause"); OsEng.timeManager().time().togglePause(); + ghoul_assert(lua_gettop(L) == 0, "Incorrect number of items left on stack"); return 0; } @@ -94,10 +92,7 @@ int time_togglePause(lua_State* L) { * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards */ int time_setPause(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) { - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - } + ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::time_setPause"); bool pause = lua_toboolean(L, -1) == 1; OsEng.timeManager().time().setPause(pause);