diff options
Diffstat (limited to 'src/compat.c')
-rw-r--r-- | src/compat.c | 44 |
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 | ||
12 | static 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 | |||
28 | void 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 | |||