Repository navigation
Expand file tree
/
Copy pathlua.h
More file actions
154 lines (136 loc) · 3.83 KB
/
Copy pathlua.h
File metadata and controls
154 lines (136 loc) · 3.83 KB
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
#pragma once
#include <lua.hpp>
#if LUA_VERSION_NUM == 501
# undef lua_setglobal
# undef lua_getglobal
# define LUA_OK 0
inline int lua_absindex(lua_State* L, int idx) {
if (idx > LUA_REGISTRYINDEX && idx < 0)
return lua_gettop(L) + idx + 1;
else
return idx;
}
inline void lua_rawgetp(lua_State* L, int idx, const void* p) {
idx = lua_absindex(L, idx);
lua_pushlightuserdata(L, (void*)(p));
lua_rawget(L, idx);
}
inline void lua_rawsetp(lua_State* L, int idx, const void* p) {
idx = lua_absindex(L, idx);
lua_pushlightuserdata(L, (void*)(p));
lua_insert(L, -2);
lua_rawset(L, idx);
}
inline void lua_setglobal(lua_State* L, const char* s) {
lua_setfield(L, LUA_GLOBALSINDEX, (s));
}
inline void lua_getglobal(lua_State* L, const char* s) {
lua_getfield(L, LUA_GLOBALSINDEX, (s));
}
inline const char* luaL_tolstring(lua_State* L, int idx, size_t* len) {
if (!luaL_callmeta(L, idx, "__tostring")) {
int t = lua_type(L, idx), tt = 0;
char const* name = NULL;
switch (t) {
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
case LUA_TSTRING:
case LUA_TNUMBER:
lua_pushvalue(L, idx);
break;
case LUA_TBOOLEAN:
if (lua_toboolean(L, idx))
lua_pushliteral(L, "true");
else
lua_pushliteral(L, "false");
break;
default:
tt = luaL_getmetafield(L, idx, "__name");
name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t);
lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx));
if (tt != LUA_TNIL)
lua_replace(L, -2);
break;
}
}
else {
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");
}
return lua_tolstring(L, -1, len);
}
inline size_t lua_rawlen(lua_State* L, int idx) {
return lua_objlen(L, idx);
}
#endif
namespace lua {
#if LUA_VERSION_NUM >= 503
# define LUACOMPAT_DEF(name) \
template <typename... Args> \
int name(lua_State* L, Args... args) { return lua_##name(L, args...); }
#else
# define LUACOMPAT_DEF(name) \
template <typename... Args> \
int name(lua_State* L, Args... args) { \
lua_##name(L, args...); \
return lua_type(L, -1); \
}
#endif
LUACOMPAT_DEF(rawgeti)
LUACOMPAT_DEF(rawgetp)
LUACOMPAT_DEF(getglobal)
LUACOMPAT_DEF(getfield)
#if LUA_VERSION_NUM == 501 && !defined(LUAJIT_VERSION)
const char* lua_getlocal(lua_State* L, const lua_Debug* ar, int n);
#else
constexpr auto lua_getlocal = ::lua_getlocal;
#endif
}
#if LUA_VERSION_NUM == 501
inline int lua_getiuservalue(lua_State* L, int idx, int n) {
if (n != 1) {
lua_pushnil(L);
return LUA_TNONE;
}
lua_getfenv(L, idx);
return lua_type(L, -1);
}
#elif LUA_VERSION_NUM == 502
inline int lua_getiuservalue(lua_State* L, int idx, int n) {
if (n != 1) {
lua_pushnil(L);
return LUA_TNONE;
}
lua_getuservalue(L, idx);
return lua_type(L, -1);
}
#elif LUA_VERSION_NUM == 503
inline int lua_getiuservalue(lua_State* L, int idx, int n) {
if (n != 1) {
lua_pushnil(L);
return LUA_TNONE;
}
return lua_getuservalue(L, idx);
}
#endif
#if LUA_VERSION_NUM == 501
inline int lua_setiuservalue(lua_State* L, int idx, int n) {
if (n != 1) {
lua_pop(L, 1);
return 0;
}
luaL_checktype(L, -1, LUA_TTABLE);
lua_setfenv(L, idx);
return 1;
}
#elif LUA_VERSION_NUM == 502 || LUA_VERSION_NUM == 503
inline int lua_setiuservalue(lua_State* L, int idx, int n) {
if (n != 1) {
lua_pop(L, 1);
return 0;
}
lua_setuservalue(L, idx);
return 1;
}
#endif