aboutsummaryrefslogtreecommitdiff
path: root/src/compat.c
diff options
context:
space:
mode:
authorBenoit Germain <bnt period germain arrobase gmail period com>2014-04-03 16:23:52 +0200
committerBenoit Germain <bnt period germain arrobase gmail period com>2014-04-03 16:23:52 +0200
commitd8cfb248939a9b6e1cbc9bb4c05b216daf9a0e3a (patch)
tree980974d5ab0e9b3b10dbc588d3a8954ecadccc07 /src/compat.c
parentaa9bfcf2dd49f55f11b27e7c21d5b75d81ccfc7e (diff)
downloadlanes-d8cfb248939a9b6e1cbc9bb4c05b216daf9a0e3a.tar.gz
lanes-d8cfb248939a9b6e1cbc9bb4c05b216daf9a0e3a.tar.bz2
lanes-d8cfb248939a9b6e1cbc9bb4c05b216daf9a0e3a.zip
moved compatibility code in a separate file
Diffstat (limited to 'src/compat.c')
-rw-r--r--src/compat.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/compat.c b/src/compat.c
new file mode 100644
index 0000000..1a66f38
--- /dev/null
+++ b/src/compat.c
@@ -0,0 +1,44 @@
1/*
2 * ###############################################################################################
3 * ######################################### Lua 5.1/5.2 #########################################
4 * ###############################################################################################
5 */
6#include "compat.h"
7
8/*
9** Copied from Lua 5.2 loadlib.c
10*/
11#if LUA_VERSION_NUM == 501
12static int luaL_getsubtable (lua_State *L, int idx, const char *fname)
13{
14 lua_getfield(L, idx, fname);
15 if (lua_istable(L, -1))
16 return 1; /* table already there */
17 else
18 {
19 lua_pop(L, 1); /* remove previous result */
20 idx = lua_absindex(L, idx);
21 lua_newtable(L);
22 lua_pushvalue(L, -1); /* copy to be left at top */
23 lua_setfield(L, idx, fname); /* assign new table to field */
24 return 0; /* false, because did not find table there */
25 }
26}
27
28void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb)
29{
30 lua_pushcfunction(L, openf);
31 lua_pushstring(L, modname); /* argument to open function */
32 lua_call(L, 1, 1); /* open module */
33 luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
34 lua_pushvalue(L, -2); /* make copy of module (call result) */
35 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
36 lua_pop(L, 1); /* remove _LOADED table */
37 if (glb)
38 {
39 lua_pushvalue(L, -1); /* copy of 'mod' */
40 lua_setglobal(L, modname); /* _G[modname] = module */
41 }
42}
43#endif // LUA_VERSION_NUM
44