summaryrefslogtreecommitdiff
path: root/compat52.h
diff options
context:
space:
mode:
Diffstat (limited to 'compat52.h')
-rw-r--r--compat52.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/compat52.h b/compat52.h
new file mode 100644
index 0000000..c674f13
--- /dev/null
+++ b/compat52.h
@@ -0,0 +1,50 @@
1
2
3static void luaL_setmetatable(lua_State *L, const char *tname) {
4 luaL_getmetatable(L, tname);
5 lua_setmetatable(L, -2);
6} /* luaL_setmetatable() */
7
8
9static int lua_absindex(lua_State *L, int idx) {
10 return (idx > 0 || idx <= LUA_REGISTRYINDEX)? idx : lua_gettop(L) + idx + 1;
11} /* lua_absindex() */
12
13
14static void *luaL_testudata(lua_State *L, int arg, const char *tname) {
15 void *p = lua_touserdata(L, arg);
16 int eq;
17
18 if (!p || !lua_getmetatable(L, arg))
19 return 0;
20
21 luaL_getmetatable(L, tname);
22 eq = lua_rawequal(L, -2, -1);
23 lua_pop(L, 2);
24
25 return (eq)? p : 0;
26} /* luaL_testudate() */
27
28
29static void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) {
30 int i, t = lua_absindex(L, -1 - nup);
31
32 for (; l->name; l++) {
33 for (i = 0; i < nup; i++)
34 lua_pushvalue(L, -nup);
35 lua_pushcclosure(L, l->func, nup);
36 lua_setfield(L, t, l->name);
37 }
38
39 return lua_pop(L, nup);
40} /* luaL_setfuncs() */
41
42
43#define luaL_newlibtable(L, l) \
44 lua_createtable(L, 0, (sizeof (l) / sizeof *(l)) - 1)
45
46#define luaL_newlib(L, l) \
47 (luaL_newlibtable((L), (l)), luaL_setfuncs((L), (l), 0))
48
49
50