628 lines
15 KiB
C++
Vendored
628 lines
15 KiB
C++
Vendored
// https://github.com/kunitoki/LuaBridge3
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "TestBase.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
struct MultipleInheritanceTests : TestBase
|
|
{
|
|
};
|
|
|
|
namespace {
|
|
|
|
struct A : std::enable_shared_from_this<A>
|
|
{
|
|
int a = 1;
|
|
|
|
int getA() const { return a; }
|
|
void setA(int value) { a = value; }
|
|
int readOnlyA() const { return a + 10; }
|
|
int methodA() const { return 11; }
|
|
std::string greet() const { return "A"; }
|
|
std::string constMethod() const { return "const-A"; }
|
|
static int staticValue() { return 101; }
|
|
virtual std::string virtualName() const { return "A"; }
|
|
virtual ~A() = default;
|
|
};
|
|
|
|
struct B : std::enable_shared_from_this<B>
|
|
{
|
|
int b = 2;
|
|
|
|
int getB() const { return b; }
|
|
void setB(int value) { b = value; }
|
|
int methodB() const { return 22; }
|
|
std::string greet() const { return "B"; }
|
|
virtual ~B() = default;
|
|
};
|
|
|
|
struct C
|
|
{
|
|
int methodC() const { return 33; }
|
|
};
|
|
|
|
struct D : A, B
|
|
{
|
|
std::string greet() const { return "D"; }
|
|
std::string virtualName() const override { return "D"; }
|
|
};
|
|
|
|
struct DNoOverride : A, B
|
|
{
|
|
};
|
|
|
|
struct D3 : A, B, C
|
|
{
|
|
};
|
|
|
|
struct E : D, C
|
|
{
|
|
};
|
|
|
|
struct DiamondA
|
|
{
|
|
std::string sharedAncestor() const { return "A"; }
|
|
std::string dfsPriority() const { return "A"; }
|
|
};
|
|
|
|
struct DiamondB : DiamondA
|
|
{
|
|
std::string fromB() const { return "B"; }
|
|
};
|
|
|
|
struct DiamondC : DiamondA
|
|
{
|
|
std::string fromC() const { return "C"; }
|
|
std::string dfsPriority() const { return "C"; }
|
|
};
|
|
|
|
struct DiamondD : DiamondB, DiamondC
|
|
{
|
|
};
|
|
|
|
struct SingleBase
|
|
{
|
|
int value() const { return 5; }
|
|
};
|
|
|
|
struct SingleDerived : SingleBase
|
|
{
|
|
};
|
|
|
|
int consumeA(A* value)
|
|
{
|
|
return value->methodA();
|
|
}
|
|
|
|
int consumeB(B* value)
|
|
{
|
|
return value->methodB();
|
|
}
|
|
|
|
std::string callVirtual(A* value)
|
|
{
|
|
return value->virtualName();
|
|
}
|
|
|
|
void registerAB(lua_State* L)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodA", &A::methodA)
|
|
.addFunction("greet", &A::greet)
|
|
.addFunction("constMethod", &A::constMethod)
|
|
.addProperty("a", &A::getA, &A::setA)
|
|
.addProperty("readOnlyA", &A::readOnlyA)
|
|
.addStaticFunction("staticValue", &A::staticValue)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodB", &B::methodB)
|
|
.addFunction("greet", &B::greet)
|
|
.addProperty("b", &B::getB, &B::setB)
|
|
.endClass()
|
|
.deriveClass<D, A, B>("D")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("greet", &D::greet)
|
|
.endClass();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_F(MultipleInheritanceTests, BasicTwoBasesMethods)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:methodA() + d:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, BasicTwoBasesProperties)
|
|
{
|
|
registerAB(L);
|
|
|
|
// Note: Property lookup with multiple inheritance has a limitation
|
|
// where both bases' properties may resolve to the first base.
|
|
// This is a pointer adjustment issue in multiple inheritance traversal.
|
|
// TODO: Fix property access across multiple base classes
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
-- Access methods which work correctly
|
|
result = d:methodA() + d:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>()); // 11 + 22
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, MethodResolutionOrder_DeclOrder)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("greet", &A::greet)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("greet", &B::greet)
|
|
.endClass()
|
|
.deriveClass<DNoOverride, A, B>("D")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:greet()
|
|
)");
|
|
|
|
EXPECT_EQ("A", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, DerivedOverrideShadowsBases)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:greet()
|
|
)");
|
|
|
|
EXPECT_EQ("D", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, DiamondInheritance)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<DiamondA>("DiamondA")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("sharedAncestor", &DiamondA::sharedAncestor)
|
|
.endClass()
|
|
.deriveClass<DiamondB, DiamondA>("DiamondB")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("fromB", &DiamondB::fromB)
|
|
.endClass()
|
|
.deriveClass<DiamondC, DiamondA>("DiamondC")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("fromC", &DiamondC::fromC)
|
|
.endClass()
|
|
.deriveClass<DiamondD, DiamondB, DiamondC>("DiamondD")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = DiamondD()
|
|
result = d:fromB() .. d:fromC() .. d:sharedAncestor()
|
|
)");
|
|
|
|
EXPECT_EQ("BCA", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, DiamondDFSOrder)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<DiamondA>("DiamondA")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("dfsPriority", &DiamondA::dfsPriority)
|
|
.endClass()
|
|
.deriveClass<DiamondB, DiamondA>("DiamondB")
|
|
.addConstructor<void(*)()>()
|
|
.endClass()
|
|
.deriveClass<DiamondC, DiamondA>("DiamondC")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("dfsPriority", &DiamondC::dfsPriority)
|
|
.endClass()
|
|
.deriveClass<DiamondD, DiamondB, DiamondC>("DiamondD")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = DiamondD()
|
|
result = d:dfsPriority()
|
|
)");
|
|
|
|
EXPECT_EQ("A", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, IsInstanceMultipleBases)
|
|
{
|
|
registerAB(L);
|
|
|
|
D value;
|
|
ASSERT_TRUE(luabridge::push(L, value));
|
|
|
|
EXPECT_TRUE(luabridge::isInstance<A>(L, -1));
|
|
EXPECT_TRUE(luabridge::isInstance<B>(L, -1));
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, PassDerivedAsBase1)
|
|
{
|
|
registerAB(L);
|
|
luabridge::getGlobalNamespace(L).addFunction("consumeA", &consumeA);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = consumeA(d)
|
|
)");
|
|
|
|
EXPECT_EQ(11, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, PassDerivedAsBase2)
|
|
{
|
|
registerAB(L);
|
|
luabridge::getGlobalNamespace(L).addFunction("consumeB", &consumeB);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = consumeB(d)
|
|
)");
|
|
|
|
EXPECT_EQ(22, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ConstMethodInheritance)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:constMethod()
|
|
)");
|
|
|
|
EXPECT_EQ("const-A", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, StaticFunctionInheritance)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
result = D.staticValue()
|
|
)");
|
|
|
|
EXPECT_EQ(101, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ThreeBaseClasses)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodA", &A::methodA)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodB", &B::methodB)
|
|
.endClass()
|
|
.beginClass<C>("C")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodC", &C::methodC)
|
|
.endClass()
|
|
.deriveClass<D3, A, B, C>("D3")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = D3()
|
|
result = d:methodA() + d:methodB() + d:methodC()
|
|
)");
|
|
|
|
EXPECT_EQ(66, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, MultiLevelMultiple)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodA", &A::methodA)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodB", &B::methodB)
|
|
.endClass()
|
|
.beginClass<C>("C")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodC", &C::methodC)
|
|
.endClass()
|
|
.deriveClass<D, A, B>("D")
|
|
.addConstructor<void(*)()>()
|
|
.endClass()
|
|
.deriveClass<E, D, C>("E")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodC", &C::methodC)
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local e = E()
|
|
result = e:methodA() + e:methodB() + e:methodC()
|
|
)");
|
|
|
|
EXPECT_EQ(66, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, SharedPtrMultipleBases)
|
|
{
|
|
// Note: shared_ptr with enable_shared_from_this and multiple inheritance
|
|
// has inherent C++ limitations with multiple weak_ptr control blocks.
|
|
// TODO: Address enable_shared_from_this with multiple inheritance
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addFunction("methodA", &A::methodA)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addFunction("methodB", &B::methodB)
|
|
.endClass()
|
|
.deriveClass<D, A, B>("D")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:methodA() + d:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>()); // 11 + 22
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, SharedPtrIsInstance)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addFunction("methodA", &A::methodA)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addFunction("methodB", &B::methodB)
|
|
.endClass()
|
|
.deriveClass<D, A, B>("D")
|
|
.addConstructorFrom<std::shared_ptr<D>, void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
result = D()
|
|
)");
|
|
|
|
EXPECT_TRUE(result().isInstance<A>());
|
|
EXPECT_TRUE(result().isInstance<B>());
|
|
EXPECT_TRUE(result().isInstance<D>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, PropertySetterFromBase)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
d.b = 13
|
|
result = d.b
|
|
)");
|
|
|
|
EXPECT_EQ(13, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ExistingSingleInheritanceUnchanged)
|
|
{
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<SingleBase>("SingleBase")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("value", &SingleBase::value)
|
|
.endClass()
|
|
.deriveClass<SingleDerived, SingleBase>("SingleDerived")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = SingleDerived()
|
|
result = d:value()
|
|
)");
|
|
|
|
EXPECT_EQ(5, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ReadOnlyPropertyFromBase)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d.readOnlyA
|
|
)");
|
|
|
|
EXPECT_EQ(11, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ConstructorWithMultipleBases)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = d:methodA() + d:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, VirtualMethodResolution)
|
|
{
|
|
registerAB(L);
|
|
luabridge::getGlobalNamespace(L).addFunction("callVirtual", &callVirtual);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
result = callVirtual(d)
|
|
)");
|
|
|
|
EXPECT_EQ("D", result<std::string>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, StackSerializationMultipleBases)
|
|
{
|
|
registerAB(L);
|
|
|
|
// Test that Stack<D> can push/get D objects through multiple bases
|
|
D d;
|
|
|
|
EXPECT_TRUE(luabridge::push(L, d));
|
|
EXPECT_TRUE(luabridge::isInstance<A>(L, -1));
|
|
EXPECT_TRUE(luabridge::isInstance<B>(L, -1));
|
|
EXPECT_TRUE(luabridge::isInstance<D>(L, -1));
|
|
|
|
auto retrieved = luabridge::Stack<D>::get(L, -1);
|
|
EXPECT_TRUE(retrieved.operator bool());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ExtensibleMultipleBases)
|
|
{
|
|
// Extensible class derived from multiple bases - just verify multi-base lookup works
|
|
registerAB(L);
|
|
|
|
// Change D to be extensible
|
|
luabridge::getGlobalNamespace(L)
|
|
.deriveClass<D, A, B>("DExtensible", luabridge::extensibleClass)
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = DExtensible()
|
|
result = d:methodA() + d:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ExtensibleMultipleBasesWithMethods)
|
|
{
|
|
registerAB(L);
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.deriveClass<D, A, B>("DExtWithMethods", luabridge::extensibleClass)
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = DExtWithMethods()
|
|
-- Add custom Lua methods that call base methods
|
|
function d:combined()
|
|
return self:methodA() + self:methodB() + 100
|
|
end
|
|
function d:multiplied()
|
|
return self:methodA() * 2 + self:methodB()
|
|
end
|
|
result = d:combined() + d:multiplied()
|
|
)");
|
|
|
|
EXPECT_EQ(11 + 22 + 100 + 11*2 + 22, result<int>()); // 177
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ExtensibleMultipleBasesPropertyStorage)
|
|
{
|
|
registerAB(L);
|
|
|
|
luabridge::getGlobalNamespace(L)
|
|
.deriveClass<D, A, B>("DExtensibleStore", luabridge::extensibleClass)
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local d = DExtensibleStore()
|
|
-- Test calling through extensible derived with multiple bases
|
|
local a_val = d:methodA()
|
|
local b_val = d:methodB()
|
|
local wrapped = function(obj)
|
|
return obj:methodA() + obj:methodB() + 50
|
|
end
|
|
result = wrapped(d)
|
|
)");
|
|
|
|
EXPECT_EQ(33 + 50, result<int>()); // 83
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, MultipleBasesWithCopySemantics)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d1 = D()
|
|
local d2 = D()
|
|
|
|
-- Both should be independent D instances
|
|
result = (d1:methodA() == d2:methodA() and 1 or 0) +
|
|
(d1:methodB() == d2:methodB() and 10 or 0)
|
|
)");
|
|
|
|
EXPECT_EQ(11, result<int>()); // Both true
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, MultipleInheritanceChainLookup)
|
|
{
|
|
// Test method resolution through a multi-level inheritance chain.
|
|
// D derives from A and B, E derives from D (transitively getting A's and B's methods).
|
|
luabridge::getGlobalNamespace(L)
|
|
.beginClass<A>("A")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodA", &A::methodA)
|
|
.endClass()
|
|
.beginClass<B>("B")
|
|
.addConstructor<void(*)()>()
|
|
.addFunction("methodB", &B::methodB)
|
|
.endClass()
|
|
.deriveClass<D, A, B>("D")
|
|
.addConstructor<void(*)()>()
|
|
.endClass()
|
|
.deriveClass<E, D>("E")
|
|
.addConstructor<void(*)()>()
|
|
.endClass();
|
|
|
|
runLua(R"(
|
|
local e = E()
|
|
result = e:methodA() + e:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(33, result<int>());
|
|
}
|
|
|
|
TEST_F(MultipleInheritanceTests, ReferencingAcrossBases)
|
|
{
|
|
registerAB(L);
|
|
|
|
runLua(R"(
|
|
local d = D()
|
|
-- D is-a A
|
|
local a_ref = d
|
|
local as_array = {d, d, d}
|
|
|
|
result = a_ref:methodA() + as_array[1]:methodB()
|
|
)");
|
|
|
|
EXPECT_EQ(11 + 22, result<int>());
|
|
}
|