@@ -32,6 +32,53 @@ TEST_F(StackTests, VoidStackOverflow)
3232 ASSERT_TRUE (luabridge::Stack<void >::push (L));
3333}
3434
35+ TEST_F (StackTests, ReferenceAndPointerToNonUserdataTypes)
36+ {
37+ // Stack<T&>, Stack<const T&>, Stack<T*> and Stack<const T*> all forward to
38+ // `Helper::template isInstance<T>(...)`, which requires the selected
39+ // StackOpSelector to declare isInstance as a template. The non-userdata
40+ // specialisations declared it as a plain static function, so instantiating
41+ // isInstance for a reference or pointer to any non-userdata type was a hard
42+ // compile error ("'isInstance' following the 'template' keyword does not
43+ // refer to a template"). That made luabridge::overload<const T&>(...)
44+ // unusable for int, double, std::string and every other non-userdata type.
45+ {
46+ ASSERT_TRUE (luabridge::push (L, 42 ));
47+
48+ EXPECT_TRUE (luabridge::isInstance<int &>(L, -1 ));
49+ EXPECT_TRUE (luabridge::isInstance<const int &>(L, -1 ));
50+ EXPECT_TRUE (luabridge::isInstance<int *>(L, -1 ));
51+ EXPECT_TRUE (luabridge::isInstance<const int *>(L, -1 ));
52+
53+ EXPECT_TRUE (luabridge::isInstance<double &>(L, -1 ));
54+ EXPECT_TRUE (luabridge::isInstance<const double &>(L, -1 ));
55+
56+ lua_pop (L, 1 );
57+ }
58+
59+ {
60+ ASSERT_TRUE (luabridge::push (L, std::string (" abc" )));
61+
62+ EXPECT_TRUE (luabridge::isInstance<std::string&>(L, -1 ));
63+ EXPECT_TRUE (luabridge::isInstance<const std::string&>(L, -1 ));
64+
65+ EXPECT_FALSE (luabridge::isInstance<int &>(L, -1 ));
66+ EXPECT_FALSE (luabridge::isInstance<const int &>(L, -1 ));
67+
68+ // The userdata specialisations reach isInstance through the same path
69+ // and must keep working.
70+ EXPECT_FALSE (luabridge::isInstance<Unregistered&>(L, -1 ));
71+ EXPECT_FALSE (luabridge::isInstance<const Unregistered&>(L, -1 ));
72+
73+ // An explicit U is forwarded rather than silently replaced by T, which
74+ // mirrors how the userdata selectors forward Userdata::isInstance<U>.
75+ EXPECT_TRUE (
76+ (luabridge::detail::StackOpSelector<const int &, false >::isInstance<std::string>(L, -1 )));
77+
78+ lua_pop (L, 1 );
79+ }
80+ }
81+
3582TEST_F (StackTests, NullptrType)
3683{
3784 {
0 commit comments