diff options
Diffstat (limited to 'src/compat.cpp')
-rw-r--r-- | src/compat.cpp | 86 |
1 files changed, 40 insertions, 46 deletions
diff --git a/src/compat.cpp b/src/compat.cpp index 712aa23..41a206a 100644 --- a/src/compat.cpp +++ b/src/compat.cpp | |||
@@ -12,37 +12,35 @@ | |||
12 | // ################################################################################################# | 12 | // ################################################################################################# |
13 | 13 | ||
14 | // Copied from Lua 5.2 loadlib.c | 14 | // Copied from Lua 5.2 loadlib.c |
15 | static int luaL_getsubtable(lua_State* L, int idx, const char* fname) | 15 | static int luaL_getsubtable(lua_State* L_, int idx, const char* fname) |
16 | { | 16 | { |
17 | lua_getfield(L, idx, fname); | 17 | lua_getfield(L_, idx, fname); |
18 | if (lua_istable(L, -1)) | 18 | if (lua_istable(L_, -1)) |
19 | return 1; /* table already there */ | 19 | return 1; /* table already there */ |
20 | else | 20 | else { |
21 | { | 21 | lua_pop(L_, 1); /* remove previous result */ |
22 | lua_pop(L, 1); /* remove previous result */ | 22 | idx = lua_absindex(L_, idx); |
23 | idx = lua_absindex(L, idx); | 23 | lua_newtable(L_); |
24 | lua_newtable(L); | 24 | lua_pushvalue(L_, -1); /* copy to be left at top */ |
25 | lua_pushvalue(L, -1); /* copy to be left at top */ | 25 | lua_setfield(L_, idx, fname); /* assign new table to field */ |
26 | lua_setfield(L, idx, fname); /* assign new table to field */ | 26 | return 0; /* false, because did not find table there */ |
27 | return 0; /* false, because did not find table there */ | ||
28 | } | 27 | } |
29 | } | 28 | } |
30 | 29 | ||
31 | // ################################################################################################# | 30 | // ################################################################################################# |
32 | 31 | ||
33 | void luaL_requiref(lua_State *L, const char *modname, lua_CFunction openf, int glb) | 32 | void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb) |
34 | { | 33 | { |
35 | lua_pushcfunction(L, openf); | 34 | lua_pushcfunction(L_, openf); |
36 | lua_pushstring(L, modname); /* argument to open function */ | 35 | lua_pushstring(L_, modname); /* argument to open function */ |
37 | lua_call(L, 1, 1); /* open module */ | 36 | lua_call(L_, 1, 1); /* open module */ |
38 | luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); | 37 | luaL_getsubtable(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
39 | lua_pushvalue(L, -2); /* make copy of module (call result) */ | 38 | lua_pushvalue(L_, -2); /* make copy of module (call result) */ |
40 | lua_setfield(L, -2, modname); /* _LOADED[modname] = module */ | 39 | lua_setfield(L_, -2, modname); /* _LOADED[modname] = module */ |
41 | lua_pop(L, 1); /* remove _LOADED table */ | 40 | lua_pop(L_, 1); /* remove _LOADED table */ |
42 | if (glb) | 41 | if (glb) { |
43 | { | 42 | lua_pushvalue(L_, -1); /* copy of 'mod' */ |
44 | lua_pushvalue(L, -1); /* copy of 'mod' */ | 43 | lua_setglobal(L_, modname); /* _G[modname] = module */ |
45 | lua_setglobal(L, modname); /* _G[modname] = module */ | ||
46 | } | 44 | } |
47 | } | 45 | } |
48 | #endif // LUA_VERSION_NUM | 46 | #endif // LUA_VERSION_NUM |
@@ -53,60 +51,56 @@ void luaL_requiref(lua_State *L, const char *modname, lua_CFunction openf, int g | |||
53 | // ################################################################################################# | 51 | // ################################################################################################# |
54 | // ################################################################################################# | 52 | // ################################################################################################# |
55 | 53 | ||
56 | void* lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue) | 54 | void* lua_newuserdatauv(lua_State* L_, size_t sz, int nuvalue) |
57 | { | 55 | { |
58 | LUA_ASSERT(L, nuvalue <= 1); | 56 | LUA_ASSERT(L_, nuvalue <= 1); |
59 | return lua_newuserdata(L, sz); | 57 | return lua_newuserdata(L_, sz); |
60 | } | 58 | } |
61 | 59 | ||
62 | // ################################################################################################# | 60 | // ################################################################################################# |
63 | 61 | ||
64 | // push on stack uservalue #n of full userdata at idx | 62 | // push on stack uservalue #n of full userdata at idx |
65 | int lua_getiuservalue(lua_State* L, int idx, int n) | 63 | int lua_getiuservalue(lua_State* L_, int idx, int n) |
66 | { | 64 | { |
67 | // full userdata can have only 1 uservalue before 5.4 | 65 | // full userdata can have only 1 uservalue before 5.4 |
68 | if (n > 1) | 66 | if (n > 1) { |
69 | { | 67 | lua_pushnil(L_); |
70 | lua_pushnil(L); | ||
71 | return LUA_TNONE; | 68 | return LUA_TNONE; |
72 | } | 69 | } |
73 | lua_getuservalue(L, idx); | 70 | lua_getuservalue(L_, idx); |
74 | 71 | ||
75 | #if LUA_VERSION_NUM == 501 | 72 | #if LUA_VERSION_NUM == 501 |
76 | /* default environment is not a nil (see lua_getfenv) */ | 73 | /* default environment is not a nil (see lua_getfenv) */ |
77 | lua_getglobal(L, "package"); | 74 | lua_getglobal(L_, "package"); |
78 | if (lua_rawequal(L, -2, -1) || lua_rawequal(L, -2, LUA_GLOBALSINDEX)) | 75 | if (lua_rawequal(L_, -2, -1) || lua_rawequal(L_, -2, LUA_GLOBALSINDEX)) { |
79 | { | 76 | lua_pop(L_, 2); |
80 | lua_pop(L, 2); | 77 | lua_pushnil(L_); |
81 | lua_pushnil(L); | ||
82 | 78 | ||
83 | return LUA_TNONE; | 79 | return LUA_TNONE; |
84 | } | 80 | } |
85 | lua_pop(L, 1); /* remove package */ | 81 | lua_pop(L_, 1); /* remove package */ |
86 | #endif | 82 | #endif |
87 | 83 | ||
88 | return lua_type(L, -1); | 84 | return lua_type(L_, -1); |
89 | } | 85 | } |
90 | 86 | ||
91 | // ################################################################################################# | 87 | // ################################################################################################# |
92 | 88 | ||
93 | // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. | 89 | // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. |
94 | // Returns 0 if the userdata does not have that value. | 90 | // Returns 0 if the userdata does not have that value. |
95 | int lua_setiuservalue(lua_State* L, int idx, int n) | 91 | int lua_setiuservalue(lua_State* L_, int idx, int n) |
96 | { | 92 | { |
97 | if (n > 1 | 93 | if (n > 1 |
98 | #if LUA_VERSION_NUM == 501 | 94 | #if LUA_VERSION_NUM == 501 |
99 | || lua_type(L, -1) != LUA_TTABLE | 95 | || lua_type(L_, -1) != LUA_TTABLE |
100 | #endif | 96 | #endif |
101 | ) | 97 | ) { |
102 | { | 98 | lua_pop(L_, 1); |
103 | lua_pop(L, 1); | ||
104 | return 0; | 99 | return 0; |
105 | } | 100 | } |
106 | 101 | ||
107 | lua_setuservalue(L, idx); | 102 | lua_setuservalue(L_, idx); |
108 | return 1; // I guess anything non-0 is ok | 103 | return 1; // I guess anything non-0 is ok |
109 | } | 104 | } |
110 | 105 | ||
111 | #endif // LUA_VERSION_NUM | 106 | #endif // LUA_VERSION_NUM |
112 | |||