3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "c_internal.h"
27 // imported from c_converter.cpp with pure C++ style
28 static inline void check_lua_type(lua_State *L, int index, const char *name, int type)
30 int t = lua_type(L, index);
32 std::string traceback = script_get_backtrace(L);
33 throw LuaError(std::string("Invalid ") + (name) + " (expected " +
34 lua_typename(L, (type)) + " got " + lua_typename(L, t) +
39 // imported from c_converter.cpp
40 #define CHECK_POS_COORD(name) \
41 check_lua_type(L, -1, "position coordinate '" name "'", LUA_TNUMBER)
42 #define CHECK_POS_TAB(index) check_lua_type(L, index, "position", LUA_TTABLE)
44 bool LuaHelper::isNaN(lua_State *L, int idx)
46 return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
50 * Read template functions
52 template <> bool LuaHelper::readParam(lua_State *L, int index)
54 return lua_toboolean(L, index) != 0;
57 template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &default_value)
59 if (lua_isnil(L, index))
62 return lua_toboolean(L, index) != 0;
65 template <> s16 LuaHelper::readParam(lua_State *L, int index)
67 return lua_tonumber(L, index);
70 template <> float LuaHelper::readParam(lua_State *L, int index)
73 throw LuaError("NaN value is not allowed.");
75 return (float)luaL_checknumber(L, index);
78 template <> v2s16 LuaHelper::readParam(lua_State *L, int index)
82 lua_getfield(L, index, "x");
84 p.X = readParam<s16>(L, -1);
86 lua_getfield(L, index, "y");
88 p.Y = readParam<s16>(L, -1);
93 template <> v2f LuaHelper::readParam(lua_State *L, int index)
97 lua_getfield(L, index, "x");
99 p.X = readParam<float>(L, -1);
101 lua_getfield(L, index, "y");
102 CHECK_POS_COORD("y");
103 p.Y = readParam<float>(L, -1);
108 template <> std::string LuaHelper::readParam(lua_State *L, int index)
111 const char *str = luaL_checkstring(L, index);
117 std::string LuaHelper::readParam(
118 lua_State *L, int index, const std::string &default_value)
121 const char *str = lua_tostring(L, index);
125 result = default_value;