]> git.lizzy.rs Git - minetest.git/commitdiff
Reduce code duplication between c_converter.cpp and helper.cpp
authorsfan5 <sfan5@live.de>
Sun, 3 Jul 2022 14:44:08 +0000 (16:44 +0200)
committersfan5 <sfan5@live.de>
Thu, 14 Jul 2022 18:55:45 +0000 (20:55 +0200)
src/script/common/c_converter.cpp
src/script/common/c_converter.h
src/script/common/helper.cpp
src/script/common/helper.h

index 109fa1a14f31d62b4ebd035ff7ce4a9348a81c55..69da35b73541b32bbe4d59f48390cef8292d5050 100644 (file)
@@ -61,6 +61,22 @@ static void set_vector_metatable(lua_State *L)
        lua_setmetatable(L, -2);
 }
 
+// Retrieve an integer vector where all components are optional
+template<class T>
+static bool getv3intfield(lua_State *L, int index,
+               const char *fieldname, T &result)
+{
+       lua_getfield(L, index, fieldname);
+       bool got = false;
+       if (lua_istable(L, -1)) {
+               got |= getintfield(L, -1, "x", result.X);
+               got |= getintfield(L, -1, "y", result.Y);
+               got |= getintfield(L, -1, "z", result.Z);
+       }
+       lua_pop(L, 1);
+       return got;
+}
+
 void push_v3f(lua_State *L, v3f p)
 {
        lua_createtable(L, 0, 3);
index 5fea3c21f6afda56789a4c0bf0ca352b3bdaa46d..2af726d1618d7d336a4b69a41b62e19d8ea7e639 100644 (file)
@@ -61,23 +61,10 @@ bool getintfield(lua_State *L, int table,
        return got;
 }
 
-template<class T>
-bool getv3intfield(lua_State *L, int index,
-               const char *fieldname, T &result)
-{
-       lua_getfield(L, index, fieldname);
-       bool got = false;
-       if (lua_istable(L, -1)) {
-               got |= getintfield(L, -1, "x", result.X);
-               got |= getintfield(L, -1, "y", result.Y);
-               got |= getintfield(L, -1, "z", result.Z);
-       }
-       lua_pop(L, 1);
-       return got;
-}
-
+// Retrieve an v3s16 where all components are optional (falls back to default)
 v3s16              getv3s16field_default(lua_State *L, int table,
                              const char *fieldname, v3s16 default_);
+
 bool               getstringfield(lua_State *L, int table,
                              const char *fieldname, std::string &result);
 size_t             getstringlistfield(lua_State *L, int table,
index fbf24e1b7e021c357750c2b9ffd7be469d8be6ec..72de2b14a16fbd23ddc48f15d3b84aab7f591820 100644 (file)
@@ -17,35 +17,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+extern "C" {
+#include <lauxlib.h>
+}
+
 #include "helper.h"
 #include <cmath>
-#include <sstream>
 #include <irr_v2d.h>
 #include <irr_v3d.h>
+#include "c_converter.h"
 #include "c_types.h"
-#include "c_internal.h"
-
-// imported from c_converter.cpp with pure C++ style
-static inline void check_lua_type(lua_State *L, int index, const char *name, int type)
-{
-       int t = lua_type(L, index);
-       if (t != type) {
-               std::string traceback = script_get_backtrace(L);
-               throw LuaError(std::string("Invalid ") + (name) + " (expected " +
-                               lua_typename(L, (type)) + " got " + lua_typename(L, t) +
-                               ").\n" + traceback);
-       }
-}
-
-// imported from c_converter.cpp
-#define CHECK_POS_COORD(name)                                                            \
-       check_lua_type(L, -1, "position coordinate '" name "'", LUA_TNUMBER)
-#define CHECK_POS_TAB(index) check_lua_type(L, index, "position", LUA_TTABLE)
-
-bool LuaHelper::isNaN(lua_State *L, int idx)
-{
-       return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
-}
 
 /*
  * Read template functions
@@ -59,74 +40,41 @@ bool LuaHelper::readParam(lua_State *L, int index)
 template <>
 s16 LuaHelper::readParam(lua_State *L, int index)
 {
-       return lua_tonumber(L, index);
+       return luaL_checkinteger(L, index);
 }
 
 template <>
 int LuaHelper::readParam(lua_State *L, int index)
 {
-       return luaL_checkint(L, index);
+       return luaL_checkinteger(L, index);
 }
 
 template <>
 float LuaHelper::readParam(lua_State *L, int index)
 {
-       if (isNaN(L, index))
-               throw LuaError("NaN value is not allowed.");
+       lua_Number v = luaL_checknumber(L, index);
+       if (std::isnan(v) && std::isinf(v))
+               throw LuaError("Invalid float value (NaN or infinity)");
 
-       return (float)luaL_checknumber(L, index);
+       return static_cast<float>(v);
 }
 
 template <>
 v2s16 LuaHelper::readParam(lua_State *L, int index)
 {
-       v2s16 p;
-       CHECK_POS_TAB(index);
-       lua_getfield(L, index, "x");
-       CHECK_POS_COORD("x");
-       p.X = readParam<s16>(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "y");
-       CHECK_POS_COORD("y");
-       p.Y = readParam<s16>(L, -1);
-       lua_pop(L, 1);
-       return p;
+       return read_v2s16(L, index);
 }
 
 template <>
 v2f LuaHelper::readParam(lua_State *L, int index)
 {
-       v2f p;
-       CHECK_POS_TAB(index);
-       lua_getfield(L, index, "x");
-       CHECK_POS_COORD("x");
-       p.X = readParam<float>(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "y");
-       CHECK_POS_COORD("y");
-       p.Y = readParam<float>(L, -1);
-       lua_pop(L, 1);
-       return p;
+       return check_v2f(L, index);
 }
 
 template <>
 v3f LuaHelper::readParam(lua_State *L, int index)
 {
-       v3f p;
-       CHECK_POS_TAB(index);
-       lua_getfield(L, index, "x");
-       CHECK_POS_COORD("x");
-       p.X = readParam<float>(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "y");
-       CHECK_POS_COORD("y");
-       p.Y = readParam<float>(L, -1);
-       lua_pop(L, 1);
-       lua_getfield(L, index, "z");
-       CHECK_POS_COORD("z");
-       p.Z = readParam<float>(L, -1);
-       lua_pop(L, 1);
-       return p;
+       return check_v3f(L, index);
 }
 
 template <>
index 6491e73cf3ddf28844c3b345cb84a7f9b5b1b270..fc462b6ef4cc5a3290be93d8bf7d05ee91a408c9 100644 (file)
@@ -21,14 +21,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 extern "C" {
 #include <lua.h>
-#include <lauxlib.h>
 }
 
 class LuaHelper
 {
 protected:
-       static bool isNaN(lua_State *L, int idx);
-
        /**
         * Read a value using a template type T from Lua State L and index
         *