aboutsummaryrefslogtreecommitdiff
path: root/src/compat.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat.cpp')
-rw-r--r--src/compat.cpp50
1 files changed, 44 insertions, 6 deletions
diff --git a/src/compat.cpp b/src/compat.cpp
index 7e90142..c833480 100644
--- a/src/compat.cpp
+++ b/src/compat.cpp
@@ -5,10 +5,10 @@
5 5
6// ################################################################################################# 6// #################################################################################################
7 7
8UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) 8UserValueCount luaW_getalluservalues(lua_State* const L_, StackIndex const idx_)
9{ 9{
10 STACK_CHECK_START_REL(L_, 0); 10 STACK_CHECK_START_REL(L_, 0);
11 StackIndex const _idx{ luaG_absindex(L_, idx_) }; 11 StackIndex const _idx{ luaW_absindex(L_, idx_) };
12 UserValueIndex _nuv{ 0 }; 12 UserValueIndex _nuv{ 0 };
13 do { 13 do {
14 // we don't know how many uservalues we are going to extract, there might be a lot... 14 // we don't know how many uservalues we are going to extract, there might be a lot...
@@ -24,15 +24,15 @@ UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_)
24// ################################################################################################# 24// #################################################################################################
25 25
26// a small helper to obtain a module's table from the registry instead of relying on the presence of _G["<name>"] 26// a small helper to obtain a module's table from the registry instead of relying on the presence of _G["<name>"]
27LuaType luaG_getmodule(lua_State* const L_, std::string_view const& name_) 27LuaType luaW_getmodule(lua_State* const L_, std::string_view const& name_)
28{ 28{
29 STACK_CHECK_START_REL(L_, 0); 29 STACK_CHECK_START_REL(L_, 0);
30 LuaType _type{ luaG_getfield(L_, kIdxRegistry, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil 30 LuaType _type{ luaW_getfield(L_, kIdxRegistry, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil
31 if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil 31 if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil
32 STACK_CHECK(L_, 1); 32 STACK_CHECK(L_, 1);
33 return _type; 33 return _type;
34 } 34 }
35 _type = luaG_getfield(L_, kIdxTop, name_); // L_: _R._LOADED {module}|nil 35 _type = luaW_getfield(L_, kIdxTop, name_); // L_: _R._LOADED {module}|nil
36 lua_remove(L_, -2); // L_: {module}|nil 36 lua_remove(L_, -2); // L_: {module}|nil
37 STACK_CHECK(L_, 1); 37 STACK_CHECK(L_, 1);
38 return _type; 38 return _type;
@@ -52,7 +52,7 @@ int luaL_getsubtable(lua_State* const L_, StackIndex const idx_, char const* fna
52 return 1; /* table already there */ 52 return 1; /* table already there */
53 } else { 53 } else {
54 lua_pop(L_, 1); /* remove previous result */ 54 lua_pop(L_, 1); /* remove previous result */
55 StackIndex const _absidx{ luaG_absindex(L_, idx_) }; 55 StackIndex const _absidx{ luaW_absindex(L_, idx_) };
56 lua_newtable(L_); 56 lua_newtable(L_);
57 lua_pushvalue(L_, -1); /* copy to be left at top */ 57 lua_pushvalue(L_, -1); /* copy to be left at top */
58 lua_setfield(L_, _absidx, fname_); /* assign new table to field */ 58 lua_setfield(L_, _absidx, fname_); /* assign new table to field */
@@ -149,3 +149,41 @@ int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex
149} 149}
150 150
151#endif // LUA_VERSION_NUM 151#endif // LUA_VERSION_NUM
152
153// #################################################################################################
154
155#if LUA_VERSION_NUM < 505
156
157#include <time.h>
158
159/* Size for the buffer, in bytes */
160#define BUFSEEDB (sizeof(void*) + sizeof(time_t))
161
162/* Size for the buffer in int's, rounded up */
163#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int))
164
165/*
166** Copy the contents of variable 'v' into the buffer pointed by 'b'.
167** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.)
168*/
169#define addbuff(b, v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v))
170
171// Copied from Lua 5.5 lauxlib.c
172unsigned int luaL_makeseed(lua_State*)
173{
174 unsigned int buff[BUFSEED];
175 unsigned int res;
176 unsigned int i;
177 time_t t = time(nullptr);
178 char* b = (char*) buff;
179 addbuff(b, b); /* local variable's address */
180 addbuff(b, t); /* time */
181 /* fill (rare but possible) remain of the buffer with zeros */
182 memset(b, 0, sizeof(buff) - BUFSEEDB);
183 res = buff[0];
184 for (i = 1; i < BUFSEED; i++)
185 res ^= (res >> 3) + (res << 7) + buff[i];
186 return res;
187}
188
189#endif // LUA_VERSION_NUM < 505