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.
26 #include "c_internal.h"
28 // imported from c_converter.cpp with pure C++ style
29 static inline void check_lua_type(lua_State *L, int index, const char *name, int type)
31 int t = lua_type(L, index);
33 std::string traceback = script_get_backtrace(L);
34 throw LuaError(std::string("Invalid ") + (name) + " (expected " +
35 lua_typename(L, (type)) + " got " + lua_typename(L, t) +
40 // imported from c_converter.cpp
41 #define CHECK_POS_COORD(name) \
42 check_lua_type(L, -1, "position coordinate '" name "'", LUA_TNUMBER)
43 #define CHECK_POS_TAB(index) check_lua_type(L, index, "position", LUA_TTABLE)
45 bool LuaHelper::isNaN(lua_State *L, int idx)
47 return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
51 * Read template functions
53 template <> bool LuaHelper::readParam(lua_State *L, int index)
55 return lua_toboolean(L, index) != 0;
58 template <> s16 LuaHelper::readParam(lua_State *L, int index)
60 return lua_tonumber(L, index);
63 template <> int LuaHelper::readParam(lua_State *L, int index)
65 return luaL_checkint(L, index);
68 template <> float LuaHelper::readParam(lua_State *L, int index)
71 throw LuaError("NaN value is not allowed.");
73 return (float)luaL_checknumber(L, index);
76 template <> v2s16 LuaHelper::readParam(lua_State *L, int index)
80 lua_getfield(L, index, "x");
82 p.X = readParam<s16>(L, -1);
84 lua_getfield(L, index, "y");
86 p.Y = readParam<s16>(L, -1);
91 template <> v2f LuaHelper::readParam(lua_State *L, int index)
95 lua_getfield(L, index, "x");
97 p.X = readParam<float>(L, -1);
99 lua_getfield(L, index, "y");
100 CHECK_POS_COORD("y");
101 p.Y = readParam<float>(L, -1);
106 template <> v3f LuaHelper::readParam(lua_State *L, int index)
109 CHECK_POS_TAB(index);
110 lua_getfield(L, index, "x");
111 CHECK_POS_COORD("x");
112 p.X = readParam<float>(L, -1);
114 lua_getfield(L, index, "y");
115 CHECK_POS_COORD("y");
116 p.Y = readParam<float>(L, -1);
118 lua_getfield(L, index, "z");
119 CHECK_POS_COORD("z");
120 p.Z = readParam<float>(L, -1);
125 template <> std::string LuaHelper::readParam(lua_State *L, int index)
129 const char *str = luaL_checklstring(L, index, &length);
130 result.assign(str, length);