Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[oweals/minetest.git] / src / script / lua_api / l_base.cpp
index 052f661bbeb1e63131edba965e24ff2807ea1b32..b401db05a67c41530c0247e9a73fde400e05ad40 100644 (file)
@@ -63,8 +63,8 @@ GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
 std::string ModApiBase::getCurrentModPath(lua_State *L)
 {
        lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
-       const char *current_mod_name = lua_tostring(L, -1);
-       if (!current_mod_name)
+       std::string current_mod_name = readParam<std::string>(L, -1, "");
+       if (current_mod_name.empty())
                return ".";
 
        const ModSpec *mod = getServer(L)->getModSpec(current_mod_name);
@@ -85,20 +85,3 @@ bool ModApiBase::registerFunction(lua_State *L, const char *name,
 
        return true;
 }
-
-bool ModApiBase::isNaN(lua_State *L, int idx)
-{
-       return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
-}
-
-/*
- * Read template functions
- */
-template<>
-float ModApiBase::readParam(lua_State *L, int index)
-{
-       if (isNaN(L, index))
-               throw LuaError("NaN value is not allowed.");
-
-       return (float) luaL_checknumber(L, index);
-}