From f1965a4364746ef64001cdf46b4f2186c9304678 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 15 Jul 2016 10:13:38 -0400 Subject: [PATCH] const qq --- sol/stack_push.hpp | 6 +++--- test_usertypes.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index 405d4535..bcefe3da 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -63,13 +63,13 @@ namespace sol { return stack::push(L, nil); T** pref = static_cast(lua_newuserdata(L, sizeof(T*))); *pref = obj; - luaL_getmetatable(L, &k[0]); + luaL_newmetatable(L, &k[0]); lua_setmetatable(L, -2); return 1; } static int push(lua_State* L, T* obj) { - return push_keyed(L, usertype_traits::metatable, obj); + return push_keyed(L, usertype_traits*>::metatable, obj); } }; @@ -287,7 +287,7 @@ namespace sol { std::allocator alloc; alloc.construct(data, std::forward(args)...); if (with_meta) { - const auto name = &usertype_traits::user_gc_metatable[0]; + const auto name = &usertype_traits>::user_gc_metatable[0]; lua_CFunction cdel = stack_detail::alloc_destroy; // Make sure we have a plain GC set for this data if (luaL_newmetatable(L, name) != 0) { diff --git a/test_usertypes.cpp b/test_usertypes.cpp index c41fba67..a2d08771 100644 --- a/test_usertypes.cpp +++ b/test_usertypes.cpp @@ -1139,3 +1139,37 @@ TEST_CASE("usertype/double-deleter-guards", "usertype metatables internally must lua = sol::state(); }); } + +TEST_CASE("usertype/const-correctness", "usertype metatable names should reasonably ignore const attributes") { + struct Vec { + int x, y, z; + }; + + sol::state lua; + lua.open_libraries(sol::lib::base); + lua.new_usertype("Vec", "x", &Vec::x, "y", &Vec::y, "z", &Vec::z); + + Vec vec; + vec.x = 1; + vec.y = 2; + vec.z = -3; + + std::vector foo; + foo.push_back(vec); + + std::vector bar; + bar.push_back(&vec); + + lua.script(R"( +func = function(vecs) + for i, vec in ipairs(vecs) do + print(i, ":", vec.x, vec.y, vec.z) + end +end +)"); + + REQUIRE_NOTHROW({ + lua["func"](foo); + lua["func"](bar); + }); +}