Tackle bug in indexing on Lua Version < 5.3

Figure out LuaJIT bug for allocations on x64 on Version < 2.1.x
This commit is contained in:
ThePhD 2019-11-13 05:18:02 +01:00
parent 2ca27eede3
commit bc1106cdea
No known key found for this signature in database
GPG Key ID: 1509DB1C0F702BFA
7 changed files with 50 additions and 40 deletions

View File

@ -156,8 +156,9 @@ init:
before_build:
- md build-sol2
- cd build-sol2
- cd
- echo %vcvars_script%
- cd && %vcvars_script%
- %vcvars_script%
- cmake .. -G "%CMAKE_GENERATOR%" %build_compiler% -DCMAKE_BUILD_TYPE=Release "-DSOL2_LUA_VERSION=%SOL2_LUA_VERSION%" -DSOL2_PLATFORM=%PLATFORM% -DSOL2_CI=ON -DSOL2_BUILD_LUA=ON -DBUILD_LUA_AS_DLL=ON -DSOL2_TESTS=ON -DSOL2_EXAMPLES=ON -DSOL2_TESTS_EXAMPLES=ON
build_script:

View File

@ -299,16 +299,16 @@ set(LUA_JIT_POSTBUILD_COMMENTS "Executable - Moving \"${LUA_JIT_SOURCE_LUA_INTER
set(LUA_JIT_POSTBUILD_COMMANDS COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_INTERPRETER}" "${LUA_JIT_DESTINATION_LUA_INTERPRETER}")
if (BUILD_LUA_AS_DLL)
if (MSVC)
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Import Library - Moving \"${LUA_JIT_SOURCE_LUA_IMP_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_IMP_LIB}\"...")
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} Import Library - Moving \"${LUA_JIT_SOURCE_LUA_IMP_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_IMP_LIB}\"...")
set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_IMP_LIB}" "${LUA_JIT_DESTINATION_LUA_IMP_LIB}")
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB_EXP}\" to \"${LUA_JIT_DESTINATION_LUA_LIB_EXP}\"...")
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB_EXP}\" to \"${LUA_JIT_DESTINATION_LUA_LIB_EXP}\"...")
set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} && "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_LIB_EXP}" "${LUA_JIT_DESTINATION_LUA_LIB_EXP}")
endif()
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Dynamic Library - Moving \"${LUA_JIT_SOURCE_LUA_DLL}\" to \"${LUA_JIT_DESTINATION_LUA_DLL}\"...")
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} Dynamic Library - Moving \"${LUA_JIT_SOURCE_LUA_DLL}\" to \"${LUA_JIT_DESTINATION_LUA_DLL}\"...")
set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_DLL}" "${LUA_JIT_DESTINATION_LUA_DLL}")
else()
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} | Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_LIB}\"...")
set(LUA_JIT_POSTBUILD_COMMENTS "${LUA_JIT_POSTBUILD_COMMENTS} Library - Moving \"${LUA_JIT_SOURCE_LUA_LIB}\" to \"${LUA_JIT_DESTINATION_LUA_LIB}\"...")
set(LUA_JIT_POSTBUILD_COMMANDS ${LUA_JIT_POSTBUILD_COMMANDS} COMMAND "${CMAKE_COMMAND}" -E copy "${LUA_JIT_SOURCE_LUA_LIB}" "${LUA_JIT_DESTINATION_LUA_LIB}")
endif()

View File

@ -110,6 +110,9 @@ int main() {
std::cout << "=== memory tracker ===" << std::endl;
#if defined(SOL_LUAJIT) && SOL_LUA_VERSION < 20100 && (UINTPTR_MAX > 0xFFFFFFFF)
std::cout << "LuaJIT in x64 mode on LuaJIT 2.0.X versions does not support using a custom allocator!" << std::endl;
#else
memory_tracker box;
std::cout << "memory at start: " << box.currently_used() << " bytes / " << box.memory_limit() << " bytes" << std::endl;
sol::state lua(&sol::default_at_panic, &memory_tracker::allocate, &box);
@ -127,6 +130,6 @@ int main() {
lua.safe_script("local obj = my_type.new() print(obj.a, obj.b, obj.c) obj.b = true print(obj.a, obj.b, obj.c)");
std::cout << "memory at end: " << box.currently_used() << " bytes / " << box.memory_limit() << " bytes" << std::endl;
#endif
return 0;
}

View File

@ -29,13 +29,15 @@
#include "stack_get.hpp"
#include "stack_check_get.hpp"
namespace sol {
namespace stack {
namespace sol { namespace stack {
template <typename T, bool global, bool raw, typename>
struct field_getter {
static constexpr int default_table_index = meta::conditional_t<meta::is_c_str_v<T> || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>) || (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -1>, std::integral_constant<int, -2>> ::value;
static constexpr int default_table_index = meta::conditional_t < meta::is_c_str_v<T>
#if SOL_LUA_VERSION >= 503
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>)
#endif // integer global keys 5.3 or better
|| (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -1>, std::integral_constant<int, -2>> ::value;
template <typename Key>
void get(lua_State* L, Key&& key, int tableindex = default_table_index) {
@ -61,7 +63,7 @@ namespace stack {
if (lua_getmetatable(L, tableindex) == 0)
push(L, lua_nil);
}
else if constexpr(raw) {
else if constexpr (raw) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
}
@ -69,7 +71,7 @@ namespace stack {
else if constexpr (std::is_void_v<std::remove_pointer_t<T>>) {
lua_rawgetp(L, tableindex, key);
}
#endif // Lua 5.3.x
#endif // Lua 5.2.x+
else {
push(L, std::forward<Key>(key));
lua_rawget(L, tableindex);
@ -89,7 +91,7 @@ namespace stack {
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_geti(L, tableindex, static_cast<lua_Integer>(key));
}
#endif // Lua 5.3.x
#endif // Lua 5.3.x+
else {
push(L, std::forward<Key>(key));
lua_gettable(L, tableindex);
@ -103,7 +105,7 @@ namespace stack {
template <std::size_t... I, typename Keys>
void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
void(detail::swallow { (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
void(detail::swallow{ (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
reference saved(L, -1);
lua_pop(L, static_cast<int>(sizeof...(I)));
saved.push();
@ -143,9 +145,10 @@ namespace stack {
template <typename T, bool global, bool raw, typename>
struct field_setter {
static constexpr int default_table_index = meta::conditional_t<(meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
static constexpr int default_table_index
= meta::conditional_t < (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>) || (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -2>, std::integral_constant<int, -3>> ::value;
std::integral_constant<int, -2>, std::integral_constant<int, -3>> ::value;
template <typename Key, typename Value>
void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) {
@ -190,7 +193,7 @@ namespace stack {
}
}
#if SOL_LUA_VERSION >= 503
else if constexpr(std::is_integral_v<T> && !std::is_same_v<bool, T>) {
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
push(L, std::forward<Value>(value));
lua_seti(L, tableindex, static_cast<lua_Integer>(key));
}
@ -240,7 +243,6 @@ namespace stack {
lua_pop(L, 1);
}
};
}
} // namespace sol::stack
}} // namespace sol::stack
#endif // SOL_STACK_FIELD_HPP

View File

@ -224,8 +224,9 @@ namespace sol {
int push(lua_State* L) const noexcept {
if constexpr (std::is_same_v<meta::unqualified_t<Table>, global_table> || is_stack_table_v<meta::unqualified_t<Table>>) {
auto pp = stack::push_pop<true>(tbl);
int tableindex = pp.index_of(tbl);
int top_index = lua_gettop(L);
stack::get_field<true>(lua_state(), key, -1);
stack::get_field<true>(lua_state(), key, tableindex);
lua_replace(L, top_index + 1);
lua_settop(L, top_index + 1);
}

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2019-11-12 17:52:53.774510 UTC
// This header was generated with sol v3.0.3 (revision ce32549)
// Generated 2019-11-13 04:15:36.931580 UTC
// This header was generated with sol v3.0.3 (revision 2ca27ee)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP

View File

@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file was generated with a script.
// Generated 2019-11-12 17:52:53.422846 UTC
// This header was generated with sol v3.0.3 (revision ce32549)
// Generated 2019-11-13 04:15:36.384800 UTC
// This header was generated with sol v3.0.3 (revision 2ca27ee)
// https://github.com/ThePhD/sol2
#ifndef SOL_SINGLE_INCLUDE_HPP
@ -14254,13 +14254,15 @@ namespace stack {
// beginning of sol/stack_field.hpp
namespace sol {
namespace stack {
namespace sol { namespace stack {
template <typename T, bool global, bool raw, typename>
struct field_getter {
static constexpr int default_table_index = meta::conditional_t<meta::is_c_str_v<T> || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>) || (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -1>, std::integral_constant<int, -2>> ::value;
static constexpr int default_table_index = meta::conditional_t < meta::is_c_str_v<T>
#if SOL_LUA_VERSION >= 503
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>)
#endif // integer global keys 5.3 or better
|| (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -1>, std::integral_constant<int, -2>> ::value;
template <typename Key>
void get(lua_State* L, Key&& key, int tableindex = default_table_index) {
@ -14286,7 +14288,7 @@ namespace stack {
if (lua_getmetatable(L, tableindex) == 0)
push(L, lua_nil);
}
else if constexpr(raw) {
else if constexpr (raw) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
}
@ -14294,7 +14296,7 @@ namespace stack {
else if constexpr (std::is_void_v<std::remove_pointer_t<T>>) {
lua_rawgetp(L, tableindex, key);
}
#endif // Lua 5.3.x
#endif // Lua 5.2.x+
else {
push(L, std::forward<Key>(key));
lua_rawget(L, tableindex);
@ -14314,7 +14316,7 @@ namespace stack {
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_geti(L, tableindex, static_cast<lua_Integer>(key));
}
#endif // Lua 5.3.x
#endif // Lua 5.3.x+
else {
push(L, std::forward<Key>(key));
lua_gettable(L, tableindex);
@ -14328,7 +14330,7 @@ namespace stack {
template <std::size_t... I, typename Keys>
void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
void(detail::swallow { (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
void(detail::swallow{ (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
reference saved(L, -1);
lua_pop(L, static_cast<int>(sizeof...(I)));
saved.push();
@ -14368,9 +14370,10 @@ namespace stack {
template <typename T, bool global, bool raw, typename>
struct field_setter {
static constexpr int default_table_index = meta::conditional_t<(meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
static constexpr int default_table_index
= meta::conditional_t < (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) || (std::is_integral_v<T> && !std::is_same_v<T, bool>)
|| (std::is_integral_v<T> && !std::is_same_v<T, bool>) || (raw && std::is_void_v<std::remove_pointer_t<T>>),
std::integral_constant<int, -2>, std::integral_constant<int, -3>> ::value;
std::integral_constant<int, -2>, std::integral_constant<int, -3>> ::value;
template <typename Key, typename Value>
void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) {
@ -14415,7 +14418,7 @@ namespace stack {
}
}
#if SOL_LUA_VERSION >= 503
else if constexpr(std::is_integral_v<T> && !std::is_same_v<bool, T>) {
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
push(L, std::forward<Value>(value));
lua_seti(L, tableindex, static_cast<lua_Integer>(key));
}
@ -14465,8 +14468,7 @@ namespace stack {
lua_pop(L, 1);
}
};
}
} // namespace sol::stack
}} // namespace sol::stack
// end of sol/stack_field.hpp
@ -22577,8 +22579,9 @@ namespace sol {
int push(lua_State* L) const noexcept {
if constexpr (std::is_same_v<meta::unqualified_t<Table>, global_table> || is_stack_table_v<meta::unqualified_t<Table>>) {
auto pp = stack::push_pop<true>(tbl);
int tableindex = pp.index_of(tbl);
int top_index = lua_gettop(L);
stack::get_field<true>(lua_state(), key, -1);
stack::get_field<true>(lua_state(), key, tableindex);
lua_replace(L, top_index + 1);
lua_settop(L, top_index + 1);
}