diff --git a/grunk/dynamic/usertype_proxy.hpp b/grunk/dynamic/usertype_proxy.hpp index 51b7818..2122bb8 100644 --- a/grunk/dynamic/usertype_proxy.hpp +++ b/grunk/dynamic/usertype_proxy.hpp @@ -13,6 +13,27 @@ namespace grunk { +namespace details { + +/** + * @brief Identity function used by usertype_proxy::with_std_vector as its + * element-cast callable. A named function template rather than a lambda on + * purpose: sol2 keys the metatable of a pushed callable on a type name it + * derives from __PRETTY_FUNCTION__, and Clang spells a lambda there as + * "(lambda at file:line:col)" with no enclosing template arguments. A lambda + * defined inside with_std_vector() therefore gets the *same* name + * for every instantiation, so the second type to call with_std_vector() in one + * lua_State silently reuses the first type's metatable (and its __call) - e.g. + * Geom_Curve.as_vec(...) ending up invoking gp_Pnt's cast and rejecting every + * argument. GCC includes the template arguments in that name and MSVC uses a + * unique lambda id, which is why this only ever showed up on Clang (macOS CI). + * A function template's name carries its template argument on every compiler. + */ +template +V identity_cast(V x) { return x; } + +} // namespace details + /** * @brief A proxy class for sol::usertype that allows for more convenient registration of usertypes with metadata. It provides a fluent interface for adding constructors, member functions and data members to the usertype, as well as setting arbitrary properties. It also provides a helper function with_std_vector to add support for std::vector where T is the usertype itself. * @@ -252,7 +273,10 @@ struct usertype_proxy { // unique-usertype VecElement (e.g. Handle) - see this method's own doc // comment above for why a by-reference parameter silently takes a // different, non-base-aware path instead. - sol::protected_function cast_to_element = sol::make_object(lua, [](VecElement x) -> VecElement { return x; }); + // + // Deliberately a named function template, NOT a lambda - see + // details::identity_cast for why a lambda here breaks under Clang. + sol::protected_function cast_to_element = sol::make_object(lua, &details::identity_cast); // convert_one is a generic lambda (not sol::object const&) for two reasons: // (1) portability - table iteration (from_table below) yields sol::object diff --git a/tests/cpp/dynamic_std_vector.cpp b/tests/cpp/dynamic_std_vector.cpp index de1b371..e8c522c 100644 --- a/tests/cpp/dynamic_std_vector.cpp +++ b/tests/cpp/dynamic_std_vector.cpp @@ -302,4 +302,46 @@ l = HandleBase.as_vec(d1, d2, b1) EXPECT_EQ(lv[0].get()->i, 5); EXPECT_EQ(lv[1].get()->i, 7); EXPECT_EQ(lv[2].get()->i, 9); +} + +// Regression test for issue #300: two different types opting into +// with_std_vector() in the SAME grunk::state must each get their own element +// cast. Every test above registers exactly one with_std_vector() type per state, +// which is why this never showed up here - it did under Emscripten (#300) and in +// grunk-occt/grunk-geoml on macOS, where gp_Pnt and Handle(Geom_Curve) share one +// state and Geom_Curve.as_vec(...) rejected every argument. Clang only - see +// details::identity_cast in usertype_proxy.hpp for the cause. +TEST(std_vector, two_types_in_one_state) +{ + grunk::state grunk; + + grunk.register_type("Foo") + .add_constructors([](int i){ return Foo{i}; }) + .with_std_vector(); + + grunk.register_type("HandleBase") + .add_constructors([](int i){ return Handle(new HandleBase{i}); }) + .with_std_vector>(); + + auto env = grunk.create_env(); + env.eval(R"( +foos = Foo.as_vec(Foo.new(1), Foo.new(2)) +handles = HandleBase.as_vec(HandleBase.new(3), HandleBase.new(4)) +)"); + + sol::object foos = env["foos"]; + ASSERT_TRUE(foos.is>()); + auto fv = foos.as>(); + ASSERT_EQ(fv.size(), 2u); + EXPECT_EQ(fv[0].i, 1); + EXPECT_EQ(fv[1].i, 2); + + sol::object handles = env["handles"]; + ASSERT_TRUE(handles.is>>()); + auto hv = handles.as>>(); + ASSERT_EQ(hv.size(), 2u); + ASSERT_FALSE(hv[0].IsNull()); + ASSERT_FALSE(hv[1].IsNull()); + EXPECT_EQ(hv[0].get()->i, 3); + EXPECT_EQ(hv[1].get()->i, 4); } \ No newline at end of file