]> git.lizzy.rs Git - minetest.git/blob - src/script/common/helper.cpp
Fix ObjectRef errors due to lua_isnil() (#10564)
[minetest.git] / src / script / common / helper.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
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.
9
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.
14
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.
18 */
19
20 #include "helper.h"
21 #include <cmath>
22 #include <sstream>
23 #include <irr_v2d.h>
24 #include <irr_v3d.h>
25 #include "c_types.h"
26 #include "c_internal.h"
27
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)
30 {
31         int t = lua_type(L, index);
32         if (t != type) {
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) +
36                                 ").\n" + traceback);
37         }
38 }
39
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)
44
45 bool LuaHelper::isNaN(lua_State *L, int idx)
46 {
47         return lua_type(L, idx) == LUA_TNUMBER && std::isnan(lua_tonumber(L, idx));
48 }
49
50 /*
51  * Read template functions
52  */
53 template <> bool LuaHelper::readParam(lua_State *L, int index)
54 {
55         return lua_toboolean(L, index) != 0;
56 }
57
58 template <> s16 LuaHelper::readParam(lua_State *L, int index)
59 {
60         return lua_tonumber(L, index);
61 }
62
63 template <> int LuaHelper::readParam(lua_State *L, int index)
64 {
65         return luaL_checkint(L, index);
66 }
67
68 template <> float LuaHelper::readParam(lua_State *L, int index)
69 {
70         if (isNaN(L, index))
71                 throw LuaError("NaN value is not allowed.");
72
73         return (float)luaL_checknumber(L, index);
74 }
75
76 template <> v2s16 LuaHelper::readParam(lua_State *L, int index)
77 {
78         v2s16 p;
79         CHECK_POS_TAB(index);
80         lua_getfield(L, index, "x");
81         CHECK_POS_COORD("x");
82         p.X = readParam<s16>(L, -1);
83         lua_pop(L, 1);
84         lua_getfield(L, index, "y");
85         CHECK_POS_COORD("y");
86         p.Y = readParam<s16>(L, -1);
87         lua_pop(L, 1);
88         return p;
89 }
90
91 template <> v2f LuaHelper::readParam(lua_State *L, int index)
92 {
93         v2f p;
94         CHECK_POS_TAB(index);
95         lua_getfield(L, index, "x");
96         CHECK_POS_COORD("x");
97         p.X = readParam<float>(L, -1);
98         lua_pop(L, 1);
99         lua_getfield(L, index, "y");
100         CHECK_POS_COORD("y");
101         p.Y = readParam<float>(L, -1);
102         lua_pop(L, 1);
103         return p;
104 }
105
106 template <> v3f LuaHelper::readParam(lua_State *L, int index)
107 {
108         v3f p;
109         CHECK_POS_TAB(index);
110         lua_getfield(L, index, "x");
111         CHECK_POS_COORD("x");
112         p.X = readParam<float>(L, -1);
113         lua_pop(L, 1);
114         lua_getfield(L, index, "y");
115         CHECK_POS_COORD("y");
116         p.Y = readParam<float>(L, -1);
117         lua_pop(L, 1);
118         lua_getfield(L, index, "z");
119         CHECK_POS_COORD("z");
120         p.Z = readParam<float>(L, -1);
121         lua_pop(L, 1);
122         return p;
123 }
124
125 template <> std::string LuaHelper::readParam(lua_State *L, int index)
126 {
127         size_t length;
128         std::string result;
129         const char *str = luaL_checklstring(L, index, &length);
130         result.assign(str, length);
131         return result;
132 }