aboutsummaryrefslogtreecommitdiff
path: root/src/compat.cpp
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2024-04-14 18:27:10 +0200
committerBenoit Germain <bnt.germain@gmail.com>2024-04-14 18:27:10 +0200
commit69d40c81d8343a1af7e0fe61fbf20a4cf5880c25 (patch)
treecbf7aa525868040820ce6743f1a30fbb59926407 /src/compat.cpp
parent0d9c9bae120f92274e1c68f7abdebfcf2c24405d (diff)
parent00970610dc8fbd00a11d3b69e4702933a592ce9f (diff)
downloadlanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.tar.gz
lanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.tar.bz2
lanes-69d40c81d8343a1af7e0fe61fbf20a4cf5880c25.zip
Merge branch 'master' of https://github.com/LuaLanes/lanes
Diffstat (limited to 'src/compat.cpp')
-rw-r--r--src/compat.cpp42
1 files changed, 29 insertions, 13 deletions
diff --git a/src/compat.cpp b/src/compat.cpp
index 47fe37e..73d0f6b 100644
--- a/src/compat.cpp
+++ b/src/compat.cpp
@@ -1,6 +1,6 @@
1/* 1/*
2 * ############################################################################################### 2 * ###############################################################################################
3 * ######################################### Lua 5.1/5.2 ######################################### 3 * ####################################### Lua 5.1/5.2/5.3 #######################################
4 * ############################################################################################### 4 * ###############################################################################################
5 */ 5 */
6#include "compat.h" 6#include "compat.h"
@@ -9,8 +9,13 @@
9/* 9/*
10** Copied from Lua 5.2 loadlib.c 10** Copied from Lua 5.2 loadlib.c
11*/ 11*/
12// ################################################################################################
13// ################################################################################################
12#if LUA_VERSION_NUM == 501 14#if LUA_VERSION_NUM == 501
13static int luaL_getsubtable (lua_State *L, int idx, const char *fname) 15// ################################################################################################
16// ################################################################################################
17
18static int luaL_getsubtable(lua_State* L, int idx, const char* fname)
14{ 19{
15 lua_getfield(L, idx, fname); 20 lua_getfield(L, idx, fname);
16 if (lua_istable(L, -1)) 21 if (lua_istable(L, -1))
@@ -26,7 +31,9 @@ static int luaL_getsubtable (lua_State *L, int idx, const char *fname)
26 } 31 }
27} 32}
28 33
29void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb) 34// ################################################################################################
35
36void luaL_requiref(lua_State *L, const char *modname, lua_CFunction openf, int glb)
30{ 37{
31 lua_pushcfunction(L, openf); 38 lua_pushcfunction(L, openf);
32 lua_pushstring(L, modname); /* argument to open function */ 39 lua_pushstring(L, modname); /* argument to open function */
@@ -43,24 +50,30 @@ void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int
43} 50}
44#endif // LUA_VERSION_NUM 51#endif // LUA_VERSION_NUM
45 52
53// ################################################################################################
54// ################################################################################################
46#if LUA_VERSION_NUM < 504 55#if LUA_VERSION_NUM < 504
56// ################################################################################################
57// ################################################################################################
47 58
48void* lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue) 59void* lua_newuserdatauv( lua_State* L, size_t sz, int nuvalue)
49{ 60{
50 ASSERT_L( nuvalue <= 1); 61 ASSERT_L( nuvalue <= 1);
51 return lua_newuserdata( L, sz); 62 return lua_newuserdata(L, sz);
52} 63}
53 64
65// ################################################################################################
66
54// push on stack uservalue #n of full userdata at idx 67// push on stack uservalue #n of full userdata at idx
55int lua_getiuservalue(lua_State* L, int idx, int n) 68int lua_getiuservalue(lua_State* L, int idx, int n)
56{ 69{
57 // full userdata can have only 1 uservalue before 5.4 70 // full userdata can have only 1 uservalue before 5.4
58 if( n > 1) 71 if( n > 1)
59 { 72 {
60 lua_pushnil( L); 73 lua_pushnil(L);
61 return LUA_TNONE; 74 return LUA_TNONE;
62 } 75 }
63 lua_getuservalue( L, idx); 76 lua_getuservalue(L, idx);
64 77
65#if LUA_VERSION_NUM == 501 78#if LUA_VERSION_NUM == 501
66 /* default environment is not a nil (see lua_getfenv) */ 79 /* default environment is not a nil (see lua_getfenv) */
@@ -68,30 +81,33 @@ int lua_getiuservalue(lua_State* L, int idx, int n)
68 if (lua_rawequal(L, -2, -1) || lua_rawequal(L, -2, LUA_GLOBALSINDEX)) 81 if (lua_rawequal(L, -2, -1) || lua_rawequal(L, -2, LUA_GLOBALSINDEX))
69 { 82 {
70 lua_pop(L, 2); 83 lua_pop(L, 2);
71 lua_pushnil( L); 84 lua_pushnil(L);
72 85
73 return LUA_TNONE; 86 return LUA_TNONE;
74 } 87 }
75 lua_pop(L, 1); /* remove package */ 88 lua_pop(L, 1); /* remove package */
76#endif 89#endif
77 90
78 return lua_type( L, -1); 91 return lua_type(L, -1);
79} 92}
80 93
81// pop stack top, sets it a uservalue #n of full userdata at idx 94// ################################################################################################
82int lua_setiuservalue( lua_State* L, int idx, int n) 95
96// 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.
97// Returns 0 if the userdata does not have that value.
98int lua_setiuservalue(lua_State* L, int idx, int n)
83{ 99{
84 if( n > 1 100 if( n > 1
85#if LUA_VERSION_NUM == 501 101#if LUA_VERSION_NUM == 501
86 || lua_type( L, -1) != LUA_TTABLE 102 || lua_type(L, -1) != LUA_TTABLE
87#endif 103#endif
88 ) 104 )
89 { 105 {
90 lua_pop( L, 1); 106 lua_pop(L, 1);
91 return 0; 107 return 0;
92 } 108 }
93 109
94 (void) lua_setuservalue( L, idx); 110 lua_setuservalue(L, idx);
95 return 1; // I guess anything non-0 is ok 111 return 1; // I guess anything non-0 is ok
96} 112}
97 113