diff --git a/server/src/lua/check.hpp b/server/src/lua/check.hpp index 596559a6..d36f1a97 100644 --- a/server/src/lua/check.hpp +++ b/server/src/lua/check.hpp @@ -26,6 +26,7 @@ #include #include #include +#include "error.hpp" namespace Lua { @@ -39,19 +40,18 @@ T check(lua_State* L, int index) } else if constexpr(std::is_integral_v) { - if constexpr(std::numeric_limits::min() < LUA_MININTEGER || - std::numeric_limits::max() > LUA_MAXINTEGER) - return std::round(luaL_checknumber(L, index)); - const lua_Integer value = luaL_checkinteger(L, index); - if constexpr(std::numeric_limits::min() >= LUA_MININTEGER && - std::numeric_limits::max() <= LUA_MAXINTEGER) + if constexpr(std::is_unsigned_v && sizeof(T) >= sizeof(value)) + { + if(value >= 0) + return static_cast(value); + } + else if constexpr(std::numeric_limits::min() <= LUA_MININTEGER && std::numeric_limits::max() >= LUA_MAXINTEGER) return value; else if(value >= std::numeric_limits::min() && value <= std::numeric_limits::max()) return value; - luaL_argerror(L, index, "out of range"); - abort(); // never happens, luaL_error doesn't return + errorArgumentOutOfRange(L, index); } else if constexpr(std::is_floating_point_v) return luaL_checknumber(L, index); diff --git a/server/src/lua/error.hpp b/server/src/lua/error.hpp new file mode 100644 index 00000000..08d36167 --- /dev/null +++ b/server/src/lua/error.hpp @@ -0,0 +1,34 @@ +/** + * server/src/lua/error.hpp + * + * This file is part of the traintastic source code. + * + * Copyright (C) 2021 Reinder Feenstra + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef TRAINTASTIC_SERVER_LUA_ERROR_HPP +#define TRAINTASTIC_SERVER_LUA_ERROR_HPP + +#include + +namespace Lua { + +[[noreturn]] inline void errorArgumentOutOfRange(lua_State* L, int arg) { luaL_argerror(L, arg, "out of range"); abort(); } + +} + +#endif