diff options
Diffstat (limited to 'src/compat.cpp')
-rw-r--r-- | src/compat.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/compat.cpp b/src/compat.cpp new file mode 100644 index 0000000..19159a9 --- /dev/null +++ b/src/compat.cpp | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | * ############################################################################################### | ||
3 | * ######################################### Lua 5.1/5.2 ######################################### | ||
4 | * ############################################################################################### | ||
5 | */ | ||
6 | #include "compat.h" | ||
7 | #include "macros_and_utils.h" | ||
8 | |||
9 | /* | ||
10 | ** Copied from Lua 5.2 loadlib.c | ||
11 | */ | ||
12 | #if LUA_VERSION_NUM == 501 | ||
13 | static int luaL_getsubtable (lua_State *L, int idx, const char *fname) | ||
14 | { | ||
15 | lua_getfield(L, idx, fname); | ||
16 | if (lua_istable(L, -1)) | ||
17 | return 1; /* table already there */ | ||
18 | else | ||
19 | { | ||
20 | lua_pop(L, 1); /* remove previous result */ | ||
21 | idx = lua_absindex(L, idx); | ||
22 | lua_newtable(L); | ||
23 | lua_pushvalue(L, -1); /* copy to be left at top */ | ||
24 | lua_setfield(L, idx, fname); /* assign new table to field */ | ||
25 | return 0; /* false, because did not find table there */ | ||
26 | } | ||
27 | } | ||
28 | |||
29 | void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) | ||
30 | { | ||
31 | lua_pushcfunction(L, openf); | ||
32 | lua_pushstring(L, modname); /* argument to open function */ | ||
33 | lua_call(L, 1, 1); /* open module */ | ||
34 | luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); | ||
35 | lua_pushvalue(L, -2); /* make copy of module (call result) */ | ||
36 | lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ | ||
37 | lua_pop(L, 1); /* remove _LOADED table */ | ||
38 | if (glb) | ||
39 | { | ||
40 | lua_pushvalue(L, -1); /* copy of 'mod' */ | ||
41 | lua_setglobal(L, modname); /* _G[modname] = module */ | ||
42 | } | ||
43 | } | ||
44 | #endif // LUA_VERSION_NUM | ||
45 | |||
46 | #if LUA_VERSION_NUM < 504 | ||
47 | |||
48 | void* lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue) | ||
49 | { | ||
50 | ASSERT_L( nuvalue <= 1); | ||
51 | return lua_newuserdata( L, sz); | ||
52 | } | ||
53 | |||
54 | int lua_getiuservalue( lua_State* L, int idx, int n) | ||
55 | { | ||
56 | if( n > 1) | ||
57 | { | ||
58 | lua_pushnil( L); | ||
59 | return LUA_TNONE; | ||
60 | } | ||
61 | lua_getuservalue( L, idx); | ||
62 | |||
63 | #if LUA_VERSION_NUM == 501 | ||
64 | /* default environment is not a nil (see lua_getfenv) */ | ||
65 | lua_getglobal(L, "package"); | ||
66 | if (lua_rawequal(L, -2, -1) || lua_rawequal(L, -2, LUA_GLOBALSINDEX)) | ||
67 | { | ||
68 | lua_pop(L, 2); | ||
69 | lua_pushnil( L); | ||
70 | |||
71 | return LUA_TNONE; | ||
72 | } | ||
73 | lua_pop(L, 1); /* remove package */ | ||
74 | #endif | ||
75 | |||
76 | return lua_type( L, -1); | ||
77 | } | ||
78 | |||
79 | int lua_setiuservalue( lua_State* L, int idx, int n) | ||
80 | { | ||
81 | if( n > 1 | ||
82 | #if LUA_VERSION_NUM == 501 | ||
83 | || lua_type( L, -1) != LUA_TTABLE | ||
84 | #endif | ||
85 | ) | ||
86 | { | ||
87 | lua_pop( L, 1); | ||
88 | return 0; | ||
89 | } | ||
90 | |||
91 | (void) lua_setuservalue( L, idx); | ||
92 | return 1; // I guess anything non-0 is ok | ||
93 | } | ||
94 | |||
95 | #endif // LUA_VERSION_NUM | ||
96 | |||