-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlua_util.h
223 lines (176 loc) · 6.66 KB
/
lua_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Copyright (c) The Grit Game Engine authors 2016
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef lua_util_h_
#define lua_util_h_
#include <string>
#include <sstream>
#include <limits>
#include <vector>
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include "exception.h"
#include "math_util.h"
#include "intrinsics.h"
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#ifdef NO_LUA_STACK_CHECKS
#define STACK_BASE_(name)
#define STACK_CHECK_N_(name,N)
#else
#define STACK_BASE_(name) int name = lua_gettop(L)
#define STACK_CHECK_N_(name,N) APP_ASSERT(lua_gettop(L)==name+N)
#endif
#define STACK_BASE STACK_BASE_(____stack)
#define STACK_CHECK_N(N) STACK_CHECK_N_(____stack,N)
#define STACK_CHECK_(name) STACK_CHECK_N_(name,0)
#define STACK_CHECK STACK_CHECK_N(0)
#define ADD_MT_MACRO(name,tag) do {\
luaL_newmetatable(L, tag); \
luaL_register(L, NULL, name##_meta_table); \
lua_pop(L,1); } while(0)
//this darn compatibility in newer lua...
#define luaL_reg luaL_Reg
#define lua_open luaL_newstate
#define luaL_getn lua_rawlen
inline void luaL_typerror (lua_State *L, int narg, const char *tname) {
const char *tostr = lua_tostring(L, narg);
EXCEPT << tname << " expected, got " << luaL_typename(L, narg) << " called " << tostr << ENDL;
}
// convert anything to a std::string
template<class T>
static inline std::string str (const T &v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
template<class T> T* check_ptr (lua_State *L, int index, const char *tag)
{ return *static_cast<T**>(luaL_checkudata(L, index, tag)); }
bool is_ptr (lua_State *L, int index, const char *tag);
std::string type_name (lua_State *L, int index);
void check_is_function (lua_State *L, int index);
static inline Vector2 check_v2 (lua_State *L, int idx)
{ Vector2 v; lua_checkvector2(L, idx, &v.x, &v.y); return v; }
static inline void push_v2 (lua_State *L, const Vector2 &v)
{ lua_pushvector2(L, v.x, v.y); }
static inline Vector3 check_v3 (lua_State *L, int idx)
{ Vector3 v; lua_checkvector3(L, idx, &v.x, &v.y, &v.z); return v; }
static inline void push_v3 (lua_State *L, const Vector3 &v)
{ lua_pushvector3(L, v.x, v.y, v.z); }
static inline Vector4 check_v4 (lua_State *L, int idx)
{ Vector4 v; lua_checkvector4(L, idx, &v.x, &v.y, &v.z, &v.w); return v; }
static inline void push_v4 (lua_State *L, const Vector4 &v)
{ lua_pushvector4(L, v.x, v.y, v.z, v.w); }
static inline Quaternion check_quat (lua_State *L, int idx)
{ Quaternion v; lua_checkquat(L, idx, &v.w, &v.x, &v.y, &v.z); return v; }
static inline void push_quat (lua_State *L, const Quaternion &v)
{ lua_pushquat(L, v.w, v.x, v.y, v.z); }
static inline void push_string (lua_State *L, const std::string &v)
{ lua_pushstring(L, v.c_str()); }
NORETURN1 void my_lua_error(lua_State *l, const std::string &msg) NORETURN2;
NORETURN1 void my_lua_error(lua_State *l, const std::string &msg, unsigned long level) NORETURN2;
NORETURN1 void my_lua_error(lua_State *l, const char *) NORETURN2;
NORETURN1 void my_lua_error(lua_State *l, const char *, unsigned long level) NORETURN2;
void check_args(lua_State *l, int expected);
void check_args_min(lua_State *l, int expected);
void check_args_max(lua_State *l, int expected);
bool check_bool (lua_State *l, int stack_index);
bool has_tag(lua_State *l, int index, const char* tag);
lua_Number check_int (lua_State *l, int stack_index, lua_Number min, lua_Number max);
float check_float (lua_State *l, int stack_index);
const char* check_string (lua_State *l, int stack_index);
template <typename T>
T check_t (lua_State *l, int stack_index,
T min = std::numeric_limits<T>::min(),
T max = std::numeric_limits<T>::max())
{
return (T) check_int(l, stack_index, min, max);
}
inline bool table_fetch_bool (lua_State *L, const char *f, bool def)
{
bool r;
lua_getfield(L,-1,f);
if (lua_isnil(L,-1)) {
r = def;
} else if (lua_type(L,-1)==LUA_TBOOLEAN) {
r = 0!=lua_toboolean(L,-1);
} else {
my_lua_error(L, std::string(f)+" should be a boolean.");
}
lua_pop(L,1);
return r;
}
template<class T> T table_fetch_num (lua_State *L, const char *f, const T &def)
{
T r;
lua_getfield(L,-1,f);
if (lua_isnil(L,-1)) {
r = def;
} else if (lua_type(L,-1)==LUA_TNUMBER) {
r = check_t<T>(L, -1);
} else {
my_lua_error(L, std::string(f)+" should be a number.");
}
lua_pop(L,1);
return r;
}
inline float table_fetch_real (lua_State *L, const char *f, float def)
{
float r;
lua_getfield(L,-1,f);
if (lua_isnil(L,-1)) {
r = def;
} else if (lua_type(L,-1)==LUA_TNUMBER) {
r = (float)lua_tonumber(L,-1);
} else {
my_lua_error(L, std::string(f)+" should be a number.");
}
lua_pop(L,1);
return r;
}
int my_lua_error_handler_cerr(lua_State *l, lua_State *coro, int levelhack);
int my_lua_error_handler_cerr(lua_State *l);
int my_do_nothing_lua_error_handler(lua_State *l);
void lua_alloc_stats_get (size_t &counter, size_t &mallocs,
size_t &reallocs, size_t &frees);
void lua_alloc_stats_set (size_t mallocs, size_t reallocs, size_t frees);
void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize);
void check_stack (lua_State *l, int size);
bool is_userdata (lua_State *L, int ud, const char *tname);
void push_cfunction (lua_State *L, int (*func)(lua_State*));
void func_map_leak_all (void);
void register_lua_globals (lua_State *L, const luaL_reg *globals);
struct stack_frame {
std::string file;
int line;
std::string func_name;
int gap;
};
std::vector<struct stack_frame> traceback(lua_State *L1, int level);
#endif
// vim: shiftwidth=8:tabstop=8:expandtab