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
|
#if LUA_VERSION_NUM < 502
#define LUA_OK 0
static void luaL_setmetatable(lua_State *L, const char *tname) {
luaL_getmetatable(L, tname);
lua_setmetatable(L, -2);
} /* luaL_setmetatable() */
static int lua_absindex(lua_State *L, int idx) {
return (idx > 0 || idx <= LUA_REGISTRYINDEX)? idx : lua_gettop(L) + idx + 1;
} /* lua_absindex() */
static void *luaL_testudata(lua_State *L, int arg, const char *tname) {
void *p = lua_touserdata(L, arg);
int eq;
if (!p || !lua_getmetatable(L, arg))
return 0;
luaL_getmetatable(L, tname);
eq = lua_rawequal(L, -2, -1);
lua_pop(L, 2);
return (eq)? p : 0;
} /* luaL_testudate() */
static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
int i, t = lua_absindex(L, -1 - nup);
for (; l->name; l++) {
for (i = 0; i < nup; i++)
lua_pushvalue(L, -nup);
lua_pushcclosure(L, l->func, nup);
lua_setfield(L, t, l->name);
}
return lua_pop(L, nup);
} /* luaL_setfuncs() */
#define luaL_newlibtable(L, l) \
lua_createtable(L, 0, (sizeof (l) / sizeof *(l)) - 1)
#define luaL_newlib(L, l) \
(luaL_newlibtable((L), (l)), luaL_setfuncs((L), (l), 0))
static void luaL_requiref(lua_State *L, const char *modname, lua_CFunction openf, int glb) {
lua_pushcfunction(L, openf);
lua_pushstring(L, modname);
lua_call(L, 1, 1);
lua_getglobal(L, "package");
lua_getfield(L, -1, "loaded");
lua_pushvalue(L, -3);
lua_setfield(L, -2, modname);
lua_pop(L, 2);
if (glb) {
lua_pushvalue(L, -1);
lua_setglobal(L, modname);
}
} /* luaL_requiref() */
#define lua_resume(L, from, nargs) lua_resume((L), (nargs))
static void lua_rawgetp(lua_State *L, int index, const void *p) {
index = lua_absindex(L, index);
lua_pushlightuserdata(L, (void *)p);
lua_rawget(L, index);
} /* lua_rawgetp() */
static void lua_rawsetp(lua_State *L, int index, const void *p) {
index = lua_absindex(L, index);
lua_pushlightuserdata(L, (void *)p);
lua_pushvalue(L, -2);
lua_rawset(L, index);
lua_pop(L, 1);
} /* lua_rawsetp() */
#ifndef LUA_UNSIGNED
#define LUA_UNSIGNED unsigned
#endif
typedef LUA_UNSIGNED lua_Unsigned;
static void lua_pushunsigned(lua_State *L, lua_Unsigned n) {
lua_pushnumber(L, (lua_Number)n);
} /* lua_pushunsigned() */
static lua_Unsigned luaL_checkunsigned(lua_State *L, int arg) {
return (lua_Unsigned)luaL_checknumber(L, arg);
} /* luaL_checkunsigned() */
static lua_Unsigned luaL_optunsigned(lua_State *L, int arg, lua_Unsigned def) {
return (lua_Unsigned)luaL_optnumber(L, arg, (lua_Number)def);
} /* luaL_optunsigned() */
/*
* Lua 5.1 userdata is a simple FILE *, while LuaJIT is a struct with the
* first member a FILE *, similar to Lua 5.2.
*/
typedef struct luaL_Stream {
FILE *f;
} luaL_Stream;
#endif /* LUA_VERSION_NUM < 502 */
|