@@ -2184,3 +2184,126 @@ TEST_F(LuaRefTests, PreservesLuaTableMutations)
21842184 ASSERT_TRUE (secondResult);
21852185 EXPECT_EQ (200 , *secondResult);
21862186}
2187+
2188+ TEST_F (LuaRefTests, GetPointerDefaultConstructed)
2189+ {
2190+ luabridge::LuaRef ref (L);
2191+ EXPECT_EQ (nullptr , ref.getPointer ());
2192+ }
2193+
2194+ TEST_F (LuaRefTests, GetPointerNil)
2195+ {
2196+ runLua (" result = nil" );
2197+ EXPECT_EQ (nullptr , result ().getPointer ());
2198+
2199+ luabridge::LuaRef nilRef (L, luabridge::LuaNil ());
2200+ EXPECT_EQ (nullptr , nilRef.getPointer ());
2201+ }
2202+
2203+ TEST_F (LuaRefTests, GetPointerBoolean)
2204+ {
2205+ runLua (" result = true" );
2206+ EXPECT_EQ (nullptr , result ().getPointer ());
2207+
2208+ runLua (" result = false" );
2209+ EXPECT_EQ (nullptr , result ().getPointer ());
2210+ }
2211+
2212+ TEST_F (LuaRefTests, GetPointerNumber)
2213+ {
2214+ runLua (" result = 42" );
2215+ EXPECT_EQ (nullptr , result ().getPointer ());
2216+
2217+ runLua (" result = 3.14" );
2218+ EXPECT_EQ (nullptr , result ().getPointer ());
2219+ }
2220+
2221+ TEST_F (LuaRefTests, GetPointerString)
2222+ {
2223+ runLua (" result = 'hello'" );
2224+
2225+ #if LUA_VERSION_NUM >= 504 && !LUABRIDGE_ON_LUAU && !LUABRIDGE_ON_LUAJIT
2226+ // Lua 5.4+ returns a pointer for strings via lua_topointer
2227+ EXPECT_NE (nullptr , result ().getPointer ());
2228+ #else
2229+ EXPECT_EQ (nullptr , result ().getPointer ());
2230+ #endif
2231+ }
2232+
2233+ TEST_F (LuaRefTests, GetPointerTable)
2234+ {
2235+ runLua (" result = {}" );
2236+ EXPECT_NE (nullptr , result ().getPointer ());
2237+ }
2238+
2239+ TEST_F (LuaRefTests, GetPointerTableIdentity)
2240+ {
2241+ runLua (" result = {}" );
2242+ const void * p1 = result ().getPointer ();
2243+ const void * p2 = result ().getPointer ();
2244+ EXPECT_NE (nullptr , p1);
2245+ EXPECT_EQ (p1, p2);
2246+ }
2247+
2248+ TEST_F (LuaRefTests, GetPointerDistinctTables)
2249+ {
2250+ runLua (" t1 = {}; t2 = {}" );
2251+ auto t1 = luabridge::getGlobal (L, " t1" );
2252+ auto t2 = luabridge::getGlobal (L, " t2" );
2253+
2254+ const void * p1 = t1.getPointer ();
2255+ const void * p2 = t2.getPointer ();
2256+ EXPECT_NE (nullptr , p1);
2257+ EXPECT_NE (nullptr , p2);
2258+ EXPECT_NE (p1, p2);
2259+ }
2260+
2261+ TEST_F (LuaRefTests, GetPointerFunction)
2262+ {
2263+ runLua (" result = function() end" );
2264+ EXPECT_NE (nullptr , result ().getPointer ());
2265+ }
2266+
2267+ TEST_F (LuaRefTests, GetPointerLightUserdata)
2268+ {
2269+ int dummy = 0 ;
2270+ lua_pushlightuserdata (L, &dummy);
2271+ auto ref = luabridge::LuaRef::fromStack (L);
2272+ EXPECT_EQ (&dummy, ref.getPointer ());
2273+ }
2274+
2275+ TEST_F (LuaRefTests, GetPointerLightUserdataNull)
2276+ {
2277+ lua_pushlightuserdata (L, nullptr );
2278+ auto ref = luabridge::LuaRef::fromStack (L);
2279+ EXPECT_EQ (nullptr , ref.getPointer ());
2280+ }
2281+
2282+ TEST_F (LuaRefTests, GetPointerUserdata)
2283+ {
2284+ void * p = lua_newuserdata (L, 100 );
2285+ auto ref = luabridge::LuaRef::fromStack (L);
2286+ EXPECT_NE (nullptr , ref.getPointer ());
2287+ EXPECT_EQ (p, ref.getPointer ());
2288+ }
2289+
2290+ TEST_F (LuaRefTests, GetPointerThread)
2291+ {
2292+ lua_State* threadL = lua_newthread (L);
2293+ lua_pushthread (threadL);
2294+ auto ref = luabridge::LuaRef::fromStack (L);
2295+ EXPECT_NE (nullptr , ref.getPointer ());
2296+ EXPECT_EQ (threadL, ref.getPointer ());
2297+ }
2298+
2299+ #if LUABRIDGE_SAFE_STACK_CHECKS
2300+ TEST_F (LuaRefTests, GetPointerStackOverflow)
2301+ {
2302+ runLua (" result = {}" );
2303+ auto ref = result ();
2304+ exhaustStackSpace ();
2305+ const void * p = ref.getPointer ();
2306+ lua_settop (L, 0 );
2307+ EXPECT_EQ (nullptr , p);
2308+ }
2309+ #endif
0 commit comments