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
|
#ifndef __LUA_RAPIDJSION_LUACOMPAT_H__
#define __LUA_RAPIDJSION_LUACOMPAT_H__
#include <cmath>
#include <limits>
#include <cstdint>
#include "qcommon/q3_lauxlib.h"
#include "lua.hpp"
namespace luax
{
inline void setfuncs(lua_State* L, const luaL_Reg* funcs)
{ luaL_setfuncs(L, funcs, 0); }
inline size_t rawlen(lua_State* L, int idx)
{ return lua_rawlen(L, idx); }
inline bool isinteger(lua_State* L, int idx, int64_t* out = nullptr)
{
if (lua_isinteger(L, idx)) // but it maybe not detect all integers.
{
if (out)
*out = lua_tointeger(L, idx);
return true;
}
double intpart;
if (std::modf(lua_tonumber(L, idx), &intpart) == 0.0)
{
if (std::numeric_limits<lua_Integer>::min() <= intpart
&& intpart <= std::numeric_limits<lua_Integer>::max())
{
if (out)
*out = static_cast<int64_t>(intpart);
return true;
}
}
return false;
}
inline int typerror(lua_State* L, int narg, const char* tname)
{
const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg));
return luaL_argerror(L, narg, msg);
}
inline bool optboolfield(lua_State* L, int idx, const char* name, bool def)
{
auto v = def;
auto t = lua_type(L, idx);
if (t != LUA_TTABLE && t != LUA_TNONE)
luax::typerror(L, idx, "table");
if (t != LUA_TNONE)
{
lua_getfield(L, idx, name);
if (!lua_isnoneornil(L, -1))
v = lua_toboolean(L, -1) != 0;
lua_pop(L, 1);
}
return v;
}
inline int optintfield(lua_State* L, int idx, const char* name, int def)
{
auto v = def;
lua_getfield(L, idx, name);
if (lua_isnumber(L, -1))
v = static_cast<int>(lua_tointeger(L, -1));
lua_pop(L, 1);
return v;
}
}
#endif
|