Remove unnecessary float limits from script API

Leaves a check for NaN and inf.
This commit is contained in:
sfan5 2022-07-03 16:25:52 +02:00
parent 137eef6590
commit 8ff3fadba0

@ -18,8 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
extern "C" { extern "C" {
#include "lua.h" #include <lua.h>
#include "lauxlib.h" #include <lauxlib.h>
} }
#include "util/numeric.h" #include "util/numeric.h"
@ -29,26 +29,27 @@ extern "C" {
#include "common/c_internal.h" #include "common/c_internal.h"
#include "constants.h" #include "constants.h"
#include <set> #include <set>
#include <cmath>
#define CHECK_TYPE(index, name, type) { \ #define CHECK_TYPE(index, name, type) do { \
int t = lua_type(L, (index)); \ int t = lua_type(L, (index)); \
if (t != (type)) { \ if (t != (type)) { \
throw LuaError(std::string("Invalid ") + (name) + \ throw LuaError(std::string("Invalid ") + (name) + \
" (expected " + lua_typename(L, (type)) + \ " (expected " + lua_typename(L, (type)) + \
" got " + lua_typename(L, t) + ")."); \ " got " + lua_typename(L, t) + ")."); \
} \ } \
} } while(0)
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER)
#define CHECK_FLOAT_RANGE(value, name) \ #define CHECK_FLOAT(value, name) do {\
if (value < F1000_MIN || value > F1000_MAX) { \ if (std::isnan(value) || std::isinf(value)) { \
std::ostringstream error_text; \ throw LuaError("Invalid float value for '" name \
error_text << "Invalid float vector dimension range '" name "' " << \ "' (NaN or infinity)"); \
"(expected " << F1000_MIN << " < " name " < " << F1000_MAX << \ } \
" got " << value << ")." << std::endl; \ } while (0)
throw LuaError(error_text.str()); \
} #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "vector coordinate " name, LUA_TNUMBER)
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE) #define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE)
/** /**
@ -145,10 +146,12 @@ v2f check_v2f(lua_State *L, int index)
lua_getfield(L, index, "x"); lua_getfield(L, index, "x");
CHECK_POS_COORD("x"); CHECK_POS_COORD("x");
p.X = lua_tonumber(L, -1); p.X = lua_tonumber(L, -1);
CHECK_FLOAT(p.X, "x");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
CHECK_POS_COORD("y"); CHECK_POS_COORD("y");
p.Y = lua_tonumber(L, -1); p.Y = lua_tonumber(L, -1);
CHECK_FLOAT(p.Y, "y");
lua_pop(L, 1); lua_pop(L, 1);
return p; return p;
} }
@ -176,17 +179,17 @@ v3f check_v3f(lua_State *L, int index)
lua_getfield(L, index, "x"); lua_getfield(L, index, "x");
CHECK_POS_COORD("x"); CHECK_POS_COORD("x");
pos.X = lua_tonumber(L, -1); pos.X = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.X, "x") CHECK_FLOAT(pos.X, "x");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
CHECK_POS_COORD("y"); CHECK_POS_COORD("y");
pos.Y = lua_tonumber(L, -1); pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.Y, "y") CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "z"); lua_getfield(L, index, "z");
CHECK_POS_COORD("z"); CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1); pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.Z, "z") CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1); lua_pop(L, 1);
return pos; return pos;
} }
@ -214,17 +217,17 @@ v3d check_v3d(lua_State *L, int index)
lua_getfield(L, index, "x"); lua_getfield(L, index, "x");
CHECK_POS_COORD("x"); CHECK_POS_COORD("x");
pos.X = lua_tonumber(L, -1); pos.X = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.X, "x") CHECK_FLOAT(pos.X, "x");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
CHECK_POS_COORD("y"); CHECK_POS_COORD("y");
pos.Y = lua_tonumber(L, -1); pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.Y, "y") CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "z"); lua_getfield(L, index, "z");
CHECK_POS_COORD("z"); CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1); pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT_RANGE(pos.Z, "z") CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1); lua_pop(L, 1);
return pos; return pos;
} }