From ef1c0e7a380bc7a901e3f2242452fbc3c9a503c0 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 8 Jun 2019 21:48:34 -0400 Subject: [PATCH] fix weird nested shit --- include/sol/stack_push.hpp | 5 +- include/sol/stack_reference.hpp | 61 ++++++++++++++++ single/include/sol/forward.hpp | 4 +- single/include/sol/sol.hpp | 70 +++++++++++++++++-- .../runtime_tests/source/containers.table.cpp | 49 +++++++++++++ 5 files changed, 181 insertions(+), 8 deletions(-) diff --git a/include/sol/stack_push.hpp b/include/sol/stack_push.hpp index 8ecb6b95..089642dd 100644 --- a/include/sol/stack_push.hpp +++ b/include/sol/stack_push.hpp @@ -407,12 +407,13 @@ namespace sol { template struct unqualified_pusher> { static int push(lua_State* L, const T& tablecont) { - using inner_t = std::remove_pointer_t>; + using Tu = meta::unwrap_unqualified_t; + using inner_t = std::remove_pointer_t; if constexpr (is_container_v) { return stack::push>(L, tablecont, nested_tag); } else { - return stack::push(L, tablecont); + return stack::push(L, tablecont); } } }; diff --git a/include/sol/stack_reference.hpp b/include/sol/stack_reference.hpp index 15b6ec71..739b634c 100644 --- a/include/sol/stack_reference.hpp +++ b/include/sol/stack_reference.hpp @@ -38,6 +38,67 @@ namespace sol { } } // namespace detail + class stateless_stack_reference { + protected: + int index = 0; + + int registry_index() const noexcept { + return LUA_NOREF; + } + + public: + stateless_stack_reference() noexcept = default; + stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference(){}; + stateless_stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(absolute_index(L, i)) { + } + stateless_stack_reference(lua_State*, absolute_index i) noexcept : stateless_stack_reference(i) { + } + stateless_stack_reference(lua_State*, raw_index i) noexcept : stateless_stack_reference(i) { + } + stateless_stack_reference(absolute_index i) noexcept : index(i) { + } + stateless_stack_reference(raw_index i) noexcept : index(i) { + } + stateless_stack_reference(lua_State*, ref_index) noexcept = delete; + stateless_stack_reference(ref_index) noexcept = delete; + stateless_stack_reference(const reference&) noexcept = delete; + stateless_stack_reference(const stateless_stack_reference&) noexcept = default; + stateless_stack_reference(stateless_stack_reference&& o) noexcept = default; + stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default; + stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default; + + int push(lua_State* L) const noexcept { +#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK + luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value"); +#endif // make sure stack doesn't overflow + lua_pushvalue(L, index); + return 1; + } + + void pop(lua_State* L, int n = 1) const noexcept { + lua_pop(L, n); + } + + int stack_index() const noexcept { + return index; + } + + const void* pointer(lua_State* L) const noexcept { + const void* vp = lua_topointer(L, stack_index()); + return vp; + } + + type get_type(lua_State* L) const noexcept { + int result = lua_type(L, index); + return static_cast(result); + } + + bool valid(lua_State* L) const noexcept { + type t = get_type(L); + return t != type::lua_nil && t != type::none; + } + }; + class stack_reference { private: lua_State* luastate = nullptr; diff --git a/single/include/sol/forward.hpp b/single/include/sol/forward.hpp index 02bfc468..bf8dcfa4 100644 --- a/single/include/sol/forward.hpp +++ b/single/include/sol/forward.hpp @@ -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-06-04 18:14:05.186483 UTC -// This header was generated with sol v3.0.2 (revision cbb0575) +// Generated 2019-06-09 01:46:16.048592 UTC +// This header was generated with sol v3.0.2 (revision d63ba49) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP diff --git a/single/include/sol/sol.hpp b/single/include/sol/sol.hpp index e54e64f9..cdd6a4cf 100644 --- a/single/include/sol/sol.hpp +++ b/single/include/sol/sol.hpp @@ -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-06-04 18:14:04.496190 UTC -// This header was generated with sol v3.0.2 (revision cbb0575) +// Generated 2019-06-09 01:46:15.617622 UTC +// This header was generated with sol v3.0.2 (revision d63ba49) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -8366,6 +8366,67 @@ namespace sol { } } // namespace detail + class stateless_stack_reference { + protected: + int index = 0; + + int registry_index() const noexcept { + return LUA_NOREF; + } + + public: + stateless_stack_reference() noexcept = default; + stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference(){}; + stateless_stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(absolute_index(L, i)) { + } + stateless_stack_reference(lua_State*, absolute_index i) noexcept : stateless_stack_reference(i) { + } + stateless_stack_reference(lua_State*, raw_index i) noexcept : stateless_stack_reference(i) { + } + stateless_stack_reference(absolute_index i) noexcept : index(i) { + } + stateless_stack_reference(raw_index i) noexcept : index(i) { + } + stateless_stack_reference(lua_State*, ref_index) noexcept = delete; + stateless_stack_reference(ref_index) noexcept = delete; + stateless_stack_reference(const reference&) noexcept = delete; + stateless_stack_reference(const stateless_stack_reference&) noexcept = default; + stateless_stack_reference(stateless_stack_reference&& o) noexcept = default; + stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default; + stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default; + + int push(lua_State* L) const noexcept { +#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK + luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value"); +#endif // make sure stack doesn't overflow + lua_pushvalue(L, index); + return 1; + } + + void pop(lua_State* L, int n = 1) const noexcept { + lua_pop(L, n); + } + + int stack_index() const noexcept { + return index; + } + + const void* pointer(lua_State* L) const noexcept { + const void* vp = lua_topointer(L, stack_index()); + return vp; + } + + type get_type(lua_State* L) const noexcept { + int result = lua_type(L, index); + return static_cast(result); + } + + bool valid(lua_State* L) const noexcept { + type t = get_type(L); + return t != type::lua_nil && t != type::none; + } + }; + class stack_reference { private: lua_State* luastate = nullptr; @@ -13155,12 +13216,13 @@ namespace sol { template struct unqualified_pusher> { static int push(lua_State* L, const T& tablecont) { - using inner_t = std::remove_pointer_t>; + using Tu = meta::unwrap_unqualified_t; + using inner_t = std::remove_pointer_t; if constexpr (is_container_v) { return stack::push>(L, tablecont, nested_tag); } else { - return stack::push(L, tablecont); + return stack::push(L, tablecont); } } }; diff --git a/tests/runtime_tests/source/containers.table.cpp b/tests/runtime_tests/source/containers.table.cpp index 933afe59..55218b35 100644 --- a/tests/runtime_tests/source/containers.table.cpp +++ b/tests/runtime_tests/source/containers.table.cpp @@ -220,3 +220,52 @@ end table_check_unordered_values(src, t1ummap.value()); } } + +TEST_CASE("containers/as_table with pointers", "test to make sure pointers are respected in as_table work") { + using EHandle = std::uint32_t; + + struct Entity { + public: + Entity(EHandle handle) : handle_(handle) {} + Entity(const Entity&) = default; + Entity(Entity&&) = default; + Entity& operator=(const Entity&) = default; + Entity& operator=(Entity&&) = default; + + private: + EHandle handle_; + }; + + auto test_func_vec = []() -> std::vector { + return { reinterpret_cast(0x01), reinterpret_cast(0x02), reinterpret_cast(0x03) }; + }; + + sol::state lua; + lua.open_libraries(sol::lib::base); + lua.new_usertype("Entity"); + + lua["f"] = [&test_func_vec]() { return sol::as_table(test_func_vec()); }; + + lua["g"] = [&test_func_vec]() { return sol::as_nested(test_func_vec()); }; + + sol::optional maybe_err0 = lua.safe_script("t = f()"); + sol::optional maybe_err1 = lua.safe_script("u = g()"); + REQUIRE_FALSE(maybe_err0.has_value()); + REQUIRE_FALSE(maybe_err1.has_value()); + + sol::table t = lua["t"]; + Entity* e1 = t[1]; + Entity* e2 = t[2]; + Entity* e3 = t[3]; + REQUIRE(e1 == reinterpret_cast(0x01)); + REQUIRE(e2 == reinterpret_cast(0x02)); + REQUIRE(e3 == reinterpret_cast(0x03)); + + sol::table u = lua["u"]; + Entity* f1 = u[1]; + Entity* f2 = u[2]; + Entity* f3 = u[3]; + REQUIRE(f1 == reinterpret_cast(0x01)); + REQUIRE(f2 == reinterpret_cast(0x02)); + REQUIRE(f3 == reinterpret_cast(0x03)); +}