From b41f92adc91844fb4cbd37c41be16072ce07a1a9 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 27 Jun 2014 21:34:18 -0700 Subject: [PATCH] updated tests fixed small error in state with new system all tests passing GCC 4.9 std=c++11 --- sol/state.hpp | 2 +- sol/userdata_traits.hpp | 2 +- tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/sol/state.hpp b/sol/state.hpp index 61464f97..2375d727 100644 --- a/sol/state.hpp +++ b/sol/state.hpp @@ -154,7 +154,7 @@ public: template state& new_userdata(const std::string& name, Args&&... args) { constructors> ctor{}; - return new_userdata(name, ctor, std::forward(args)...); + return new_userdata(name, ctor, std::forward(args)...); } template diff --git a/sol/userdata_traits.hpp b/sol/userdata_traits.hpp index aad22b12..18d28811 100644 --- a/sol/userdata_traits.hpp +++ b/sol/userdata_traits.hpp @@ -40,7 +40,7 @@ template const std::string userdata_traits::metatable = std::string("sol.").append(detail::demangle(typeid(T))); template -const std::string userdata_traits::metatable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB"); +const std::string userdata_traits::gctable = std::string("sol.").append(detail::demangle(typeid(T))).append(".\xE2\x99\xBB"); } diff --git a/tests.cpp b/tests.cpp index e78a3aa2..3b13444b 100644 --- a/tests.cpp +++ b/tests.cpp @@ -703,3 +703,37 @@ TEST_CASE("userdata/issue-number-thirty-five", "using value types created from l REQUIRE_NOTHROW(lua.script("v = Vec.new(1, 2, 3)\n" "print(v:normalized():length())" )); } + +TEST_CASE("userdata/lua-stored-userdata", "ensure userdata values can be stored without keeping userdata object alive") { + struct Vec { + float x, y, z; + Vec(float x, float y, float z) : x{x}, y{y}, z{z} {} + float length() { + return sqrtf(x*x + y*y + z*z); + } + Vec normalized() { + float invS = 1 / length(); + return {x * invS, y * invS, z * invS}; + } + }; + + sol::state lua; + lua.open_libraries(sol::lib::base); + + { + sol::constructors> ctor; + sol::userdata udata("Vec", ctor, + "normalized", &Vec::normalized, + "length", &Vec::length); + + lua.set_userdata(udata); + // userdata dies, but still usable in lua! + } + + lua.script("collectgarbage()\n" + "v = Vec.new(1, 2, 3)\n" + "print(v:length())"); + + lua.script("v = Vec.new(1, 2, 3)\n" + "print(v:normalized():length())" ); +}