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
54 bool LuaHelper::readParam(lua_State *L, int index)
56 return lua_toboolean(L, index) != 0;
60 s16 LuaHelper::readParam(lua_State *L, int index)
62 return lua_tonumber(L, index);
66 int LuaHelper::readParam(lua_State *L, int index)
68 return luaL_checkint(L, index);
72 float LuaHelper::readParam(lua_State *L, int index)
75 throw LuaError("NaN value is not allowed.");
77 return (float)luaL_checknumber(L, index);
81 v2s16 LuaHelper::readParam(lua_State *L, int index)
85 lua_getfield(L, index, "x");
87 p.X = readParam<s16>(L, -1);
89 lua_getfield(L, index, "y");
91 p.Y = readParam<s16>(L, -1);
97 v2f LuaHelper::readParam(lua_State *L, int index)
100 CHECK_POS_TAB(index);
101 lua_getfield(L, index, "x");
102 CHECK_POS_COORD("x");
103 p.X = readParam<float>(L, -1);
105 lua_getfield(L, index, "y");
106 CHECK_POS_COORD("y");
107 p.Y = readParam<float>(L, -1);
113 v3f LuaHelper::readParam(lua_State *L, int index)
116 CHECK_POS_TAB(index);
117 lua_getfield(L, index, "x");
118 CHECK_POS_COORD("x");
119 p.X = readParam<float>(L, -1);
121 lua_getfield(L, index, "y");
122 CHECK_POS_COORD("y");
123 p.Y = readParam<float>(L, -1);
125 lua_getfield(L, index, "z");
126 CHECK_POS_COORD("z");
127 p.Z = readParam<float>(L, -1);
133 std::string LuaHelper::readParam(lua_State *L, int index)
137 const char *str = luaL_checklstring(L, index, &length);
138 result.assign(str, length);