Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion grunk/dynamic/usertype_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VecElement>() 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 <typename V>
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<T> where T is the usertype itself.
*
Expand Down Expand Up @@ -252,7 +273,10 @@ struct usertype_proxy {
// unique-usertype VecElement (e.g. Handle<T>) - 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<VecElement>);

// convert_one is a generic lambda (not sol::object const&) for two reasons:
// (1) portability - table iteration (from_table below) yields sol::object
Expand Down
42 changes: 42 additions & 0 deletions tests/cpp/dynamic_std_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>("Foo")
.add_constructors([](int i){ return Foo{i}; })
.with_std_vector();

grunk.register_type<HandleBase>("HandleBase")
.add_constructors([](int i){ return Handle<HandleBase>(new HandleBase{i}); })
.with_std_vector<Handle<HandleBase>>();

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<std::vector<Foo>>());
auto fv = foos.as<std::vector<Foo>>();
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<std::vector<Handle<HandleBase>>>());
auto hv = handles.as<std::vector<Handle<HandleBase>>>();
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);
}
Loading