From 8a27648b7713c62a91d619376482bab282adae16 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 27 Jun 2018 11:34:40 -0400 Subject: [PATCH] add `pointer()` method --- single/sol/sol.hpp | 34 ++++++++++++++++++++++++++++++++-- single/sol/sol_forward.hpp | 4 ++-- sol/reference.hpp | 6 ++++++ sol/stack_get_unqualified.hpp | 8 ++++++++ sol/stack_push.hpp | 8 ++++++++ sol/stack_reference.hpp | 5 +++++ sol/types.hpp | 3 +++ tests/test_utility.cpp | 21 +++++++++++++++++++++ 8 files changed, 85 insertions(+), 4 deletions(-) diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 8bcaf8b5..17353016 100644 --- a/single/sol/sol.hpp +++ b/single/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 2018-06-27 11:15:40.545352 UTC -// This header was generated with sol v2.20.4 (revision 3935dc4) +// Generated 2018-06-27 15:33:51.932186 UTC +// This header was generated with sol v2.20.4 (revision 60ee53a) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -5530,6 +5530,9 @@ namespace sol { template <> struct lua_type_of : std::integral_constant {}; + template <> + struct lua_type_of : std::integral_constant {}; + template <> struct lua_type_of : std::integral_constant {}; @@ -6513,6 +6516,11 @@ namespace sol { return index; } + const void* pointer() const noexcept { + const void* vp = lua_topointer(lua_state(), stack_index()); + return vp; + } + type get_type() const noexcept { int result = lua_type(lua_state(), index); return static_cast(result); @@ -6957,6 +6965,12 @@ namespace sol { return !(ref == LUA_NOREF || ref == LUA_REFNIL); } + const void* pointer() const noexcept { + int si = push(); + const void* vp = lua_topointer(lua_state(), -si); + return vp; + } + explicit operator bool() const noexcept { return valid(); } @@ -9843,6 +9857,14 @@ namespace stack { } }; + template <> + struct getter { + static const void* get(lua_State* L, int index, record& tracking) { + tracking.use(1); + return lua_touserdata(L, index); + } + }; + template struct getter> { static T* get_no_lua_nil(lua_State* L, int index, record& tracking) { @@ -10673,6 +10695,14 @@ namespace stack { } }; + template <> + struct pusher { + static int push(lua_State* L, const void* userdata) { + lua_pushlightuserdata(L, const_cast(userdata)); + return 1; + } + }; + template <> struct pusher { static int push(lua_State* L, lightuserdata_value userdata) { diff --git a/single/sol/sol_forward.hpp b/single/sol/sol_forward.hpp index 0318b20e..fe2766a1 100644 --- a/single/sol/sol_forward.hpp +++ b/single/sol/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 2018-06-27 11:15:40.807656 UTC -// This header was generated with sol v2.20.4 (revision 3935dc4) +// Generated 2018-06-27 15:33:52.214419 UTC +// This header was generated with sol v2.20.4 (revision 60ee53a) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP diff --git a/sol/reference.hpp b/sol/reference.hpp index 8fc35ead..0693a434 100644 --- a/sol/reference.hpp +++ b/sol/reference.hpp @@ -429,6 +429,12 @@ namespace sol { return !(ref == LUA_NOREF || ref == LUA_REFNIL); } + const void* pointer() const noexcept { + int si = push(); + const void* vp = lua_topointer(lua_state(), -si); + return vp; + } + explicit operator bool() const noexcept { return valid(); } diff --git a/sol/stack_get_unqualified.hpp b/sol/stack_get_unqualified.hpp index 2a266a8f..30ac05b4 100644 --- a/sol/stack_get_unqualified.hpp +++ b/sol/stack_get_unqualified.hpp @@ -739,6 +739,14 @@ namespace stack { } }; + template <> + struct getter { + static const void* get(lua_State* L, int index, record& tracking) { + tracking.use(1); + return lua_touserdata(L, index); + } + }; + template struct getter> { static T* get_no_lua_nil(lua_State* L, int index, record& tracking) { diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index 16225005..7c92a727 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -467,6 +467,14 @@ namespace stack { } }; + template <> + struct pusher { + static int push(lua_State* L, const void* userdata) { + lua_pushlightuserdata(L, const_cast(userdata)); + return 1; + } + }; + template <> struct pusher { static int push(lua_State* L, lightuserdata_value userdata) { diff --git a/sol/stack_reference.hpp b/sol/stack_reference.hpp index 16a22836..7fdb7da6 100644 --- a/sol/stack_reference.hpp +++ b/sol/stack_reference.hpp @@ -113,6 +113,11 @@ namespace sol { return index; } + const void* pointer() const noexcept { + const void* vp = lua_topointer(lua_state(), stack_index()); + return vp; + } + type get_type() const noexcept { int result = lua_type(lua_state(), index); return static_cast(result); diff --git a/sol/types.hpp b/sol/types.hpp index e06ffba3..8357fa55 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -950,6 +950,9 @@ namespace sol { template <> struct lua_type_of : std::integral_constant {}; + template <> + struct lua_type_of : std::integral_constant {}; + template <> struct lua_type_of : std::integral_constant {}; diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index 438c2502..6bae2e04 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -134,6 +134,27 @@ TEST_CASE("utility/thread", "fire up lots of threads at the same time to make su }()); } +TEST_CASE("utility/pointer", "check we can get pointer value from references") { + sol::state lua; + lua.set_function("f", [](bool aorb, sol::reference a, sol::stack_reference b) { + if (aorb) { + return a.pointer(); + } + return b.pointer(); + }); + auto result0 = lua.safe_script("v0 = 'hi'", sol::script_pass_on_error); + REQUIRE(result0.valid()); + auto result1 = lua.safe_script("v1 = f(true, v0)", sol::script_pass_on_error); + REQUIRE(result1.valid()); + auto result2 = lua.safe_script("v2 = f(false, nil, v0)", sol::script_pass_on_error); + REQUIRE(result2.valid()); + const void* ap = lua["v1"]; + const void* bp = lua["v2"]; + REQUIRE(ap != nullptr); + REQUIRE(bp != nullptr); + REQUIRE(ap == bp); +} + TEST_CASE("utility/this_state", "Ensure this_state argument can be gotten anywhere in the function.") { struct bark { int with_state(sol::this_state l, int a, int b) {