// https://github.com/kunitoki/LuaBridge3 // Copyright 2020, Dmitry Tarakanov // SPDX-License-Identifier: MIT #include "TestBase.h" #include struct NamespaceTests : TestBase { template T variable(const std::string& name) { runLua("result = " + name); return result(); } }; namespace { enum class A { x, y }; } // namespace TEST_F(NamespaceTests, Variables) { int int_ = -10; int stored = 42; auto any = luabridge::newTable(L); any["a"] = 1; #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &int_), std::logic_error); #endif runLua("result = int"); ASSERT_TRUE(result().isNil()); luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("int", &int_) .addProperty("any", &any) .addProperty("fnc_get", [stored] { return stored; }) .addProperty("fnc_getset", [stored] { return stored; }, [&stored](int v) { stored = v; }) .addVariable("A_x", A::x) .addVariable("A_y", A::y) .endNamespace(); ASSERT_EQ(-10, variable("ns.int")); ASSERT_EQ(any, variable("ns.any")); runLua("ns.int = -20"); ASSERT_EQ(-20, int_); runLua("ns.any = {b = 2}"); ASSERT_TRUE(any.isTable()); ASSERT_TRUE(any["b"].isNumber()); ASSERT_EQ(2, any["b"].cast()); runLua("result = ns.fnc_get"); ASSERT_EQ(stored, result()); runLua("result = ns.fnc_getset"); ASSERT_EQ(stored, result()); runLua("ns.fnc_getset = 1337"); ASSERT_EQ(1337, stored); runLua("result = ns.A_x"); ASSERT_EQ(A::x, static_cast(result())); runLua("result = ns.A_y"); ASSERT_EQ(A::y, static_cast(result())); } TEST_F(NamespaceTests, ReadOnlyVariables) { int int_ = -10; const int const_int_ = 1337; auto any = luabridge::newTable(L); any["a"] = 1; #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &int_), std::logic_error); #endif runLua("result = int"); ASSERT_TRUE(result().isNil()); luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("int", &int_, false) .addProperty("const_int", &const_int_) .addProperty("any", &any, false) .endNamespace(); ASSERT_EQ(-10, variable("ns.int")); ASSERT_EQ(any, variable("ns.any")); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(runLua("ns.int = -20"), std::runtime_error); #else ASSERT_FALSE(runLua("ns.int = -20")); #endif ASSERT_EQ(-10, variable("ns.int")); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(runLua("ns.const_int = 100"), std::runtime_error); #else ASSERT_FALSE(runLua("ns.const_int = 100")); #endif ASSERT_EQ(1337, variable("ns.const_int")); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(runLua("ns.any = {b = 2}"), std::runtime_error); #else ASSERT_FALSE(runLua("ns.any = {b = 2}")); #endif ASSERT_EQ(any, variable("ns.any")); } namespace { template struct Property { static T value; }; template T Property::value; template void setProperty(const T& value) { Property::value = value; } template void setPropertyNoexcept(const T& value) noexcept { Property::value = value; } template const T& getProperty() { return Property::value; } template const T& getPropertyNoexcept() noexcept { return Property::value; } } // namespace TEST_F(NamespaceTests, Properties) { setProperty(-10); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW( luabridge::getGlobalNamespace(L).addProperty("int", &getProperty, &setProperty), std::logic_error); #endif runLua("result = int"); ASSERT_TRUE(result().isNil()); luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("int", &getProperty, &setProperty) .addProperty("int2", &getPropertyNoexcept, &setPropertyNoexcept) .endNamespace(); ASSERT_EQ(-10, variable("ns.int")); ASSERT_EQ(-10, variable("ns.int2")); runLua("ns.int = -20"); ASSERT_EQ(-20, getProperty()); runLua("ns.int2 = -20"); ASSERT_EQ(-20, getPropertyNoexcept()); } TEST_F(NamespaceTests, ReadOnlyProperties) { setProperty(-10); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(luabridge::getGlobalNamespace(L).addProperty("int", &getProperty), std::logic_error); #endif runLua("result = int"); ASSERT_TRUE(result().isNil()); luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("int", &getProperty) .addProperty("int2", &getPropertyNoexcept) .endNamespace(); ASSERT_EQ(-10, variable("ns.int")); #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(runLua("ns.int = -20"), std::runtime_error); ASSERT_THROW(runLua("ns.int2 = -20"), std::runtime_error); #else ASSERT_FALSE(runLua("ns.int = -20")); ASSERT_FALSE(runLua("ns.int2 = -20")); #endif ASSERT_EQ(-10, getProperty()); ASSERT_EQ(-10, getPropertyNoexcept()); } TEST_F(NamespaceTests, AddVariable) { int int_ = -10; auto any = luabridge::newTable(L); any["a"] = 1; enum class A { a, b, c, d }; #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(luabridge::getGlobalNamespace(L).addVariable("int", &int_), std::logic_error); #endif runLua("result = int"); ASSERT_TRUE(result().isNil()); luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addVariable("int", int_) .addVariable("any", any) .beginNamespace("A") .addVariable("a", A::a) .addVariable("b", A::b) .addVariable("c", A::c) .addVariable("d", A::d) .endNamespace() .endNamespace(); ASSERT_EQ(-10, variable("ns.int")); ASSERT_EQ(any, variable("ns.any")); runLua("ns.A.a = 2; result = ns.A.a"); ASSERT_EQ(A::c, static_cast(result())); runLua("ns.int = -20"); ASSERT_EQ(-10, int_); runLua("result = ns.int"); ASSERT_EQ(-20, result()); runLua("ns.any = {a = 42, b = 2}"); ASSERT_TRUE(any.isTable()); ASSERT_TRUE(any["b"].isNil()); runLua("result = ns.any.a"); ASSERT_EQ(42, result()); } namespace { template struct Storage { static T value; }; template T Storage::value; template int getDataC(lua_State* L) { [[maybe_unused]] auto result = luabridge::Stack::push(L, Storage::value); return 1; } template int setDataC(lua_State* L) { auto result = luabridge::Stack::get(L, -1); if (! result) luaL_error(L, "%s", result.error().message().c_str()); Storage::value = *result; return 0; } } // namespace TEST_F(NamespaceTests, NamespaceFromStack) { // Create environment table lua_newtable(L); luabridge::getNamespaceFromStack(L) .addFunction("Function", [](int x) { return x; }); int tableReference = luabridge::luaL_ref(L, LUA_REGISTRYINDEX); // Load a script std::string script = "result = Function (42)"; auto success = luaL_loadstring(L, script.c_str()) == LUABRIDGE_LUA_OK; ASSERT_TRUE(success); // Register lua_rawgeti(L, LUA_REGISTRYINDEX, tableReference); // Set as up value #if LUA_VERSION_NUM < 502 ASSERT_EQ(1, lua_setfenv(L, -2)); #else auto newUpvalueName = lua_setupvalue(L, -2, 1); ASSERT_TRUE(newUpvalueName); ASSERT_STREQ("_ENV", newUpvalueName); if (! newUpvalueName) lua_pop(L, -1); #endif success = lua_pcall(L, 0, 0, 0) == LUABRIDGE_LUA_OK; ASSERT_TRUE(success); lua_rawgeti(L, LUA_REGISTRYINDEX, tableReference); auto resultTable = luabridge::LuaRef::fromStack(L); ASSERT_TRUE(resultTable.isTable()); ASSERT_TRUE(resultTable["result"].cast()); ASSERT_EQ(42, resultTable["result"].cast()); } TEST_F(NamespaceTests, Properties_ProxyCFunctions) { luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("value", &getDataC, &setDataC) .endNamespace(); Storage::value = 1; runLua("ns.value = 2"); ASSERT_EQ(2, Storage::value); Storage::value = 3; runLua("result = ns.value"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(3, result()); } TEST_F(NamespaceTests, Properties_ProxyCFunctions_ReadOnly) { luabridge::getGlobalNamespace(L) .beginNamespace("ns") .addProperty("value", &getDataC) .endNamespace(); Storage::value = 1; #if LUABRIDGE_HAS_EXCEPTIONS ASSERT_THROW(runLua("ns.value = 2"), std::exception); #else ASSERT_FALSE(runLua("ns.value = 2")); #endif ASSERT_EQ(1, Storage::value); Storage::value = 3; runLua("result = ns.value"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(3, result()); } namespace { struct Class {}; } // namespace TEST_F(NamespaceTests, LuaStackIntegrity) { const int initialStackIndex = 0; EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... { auto ns2 = luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginNamespace("ns2"); EXPECT_EQ( initialStackIndex + 3, lua_gettop(L)); // Stack: ..., global namespace table (gns), namespace table (ns), ns2 ns2.endNamespace(); // Stack: ... EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... { auto globalNs = luabridge::getGlobalNamespace(L); EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns { auto ns = luabridge::getGlobalNamespace(L).beginNamespace("namespace"); // both globalNs an ns are active EXPECT_EQ(initialStackIndex + 3, lua_gettop(L)); // Stack: ..., gns, gns, ns } EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns { auto ns = globalNs.beginNamespace("namespace"); // globalNs became inactive EXPECT_EQ(initialStackIndex + 2, lua_gettop(L)); // Stack: ..., gns, ns } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... #if LUABRIDGE_HAS_EXCEPTIONS EXPECT_THROW(globalNs.beginNamespace("namespace"), std::exception); EXPECT_THROW(globalNs.beginClass("Class"), std::exception); #endif } { auto globalNs = luabridge::getGlobalNamespace(L).beginNamespace("namespace").endNamespace(); // globalNs is active EXPECT_EQ(initialStackIndex + 1, lua_gettop(L)); // Stack: ..., gns } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // StacK: ... { auto cls = luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginClass("Class"); EXPECT_EQ(initialStackIndex + 5, lua_gettop(L)); // Stack: ..., gns, ns, const table, class table, static table { auto ns = cls.endClass(); EXPECT_EQ(initialStackIndex + 2, lua_gettop(L)); // Stack: ..., gns, ns } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // StacK: ... // Test class continuation { auto cls = luabridge::getGlobalNamespace(L).beginNamespace("namespace").beginClass("Class"); EXPECT_EQ(initialStackIndex + 5, lua_gettop(L)); // Stack: ..., gns, ns, const table, class table, static table } EXPECT_EQ(initialStackIndex, lua_gettop(L)); // Stack: ... } TEST_F(NamespaceTests, LuaStackAdditionalIntegrity) { [[maybe_unused]] auto globalNs = luabridge::getGlobalNamespace(L); #if LUABRIDGE_HAS_EXCEPTIONS EXPECT_THROW(globalNs.endNamespace(), std::exception); #endif } namespace { template T Function(T param) { return param; } int LuaFunction(lua_State* L) { lua_pushnumber(L, 42); return 1; } } // namespace TEST_F(NamespaceTests, Functions) { luabridge::getGlobalNamespace(L).addFunction("Function", &Function); runLua("result = Function (3.14)"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(3.14, result()); } TEST_F(NamespaceTests, LuaFunctions) { luabridge::getGlobalNamespace(L).addFunction("Function", &LuaFunction); runLua("result = Function ()"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(42, result()); } TEST_F(NamespaceTests, StdFunctions) { luabridge::getGlobalNamespace(L).addFunction("Function", std::function(&Function)); runLua("result = Function (12)"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(12, result()); } TEST_F(NamespaceTests, CapturingLambdas) { int x = 30; luabridge::getGlobalNamespace(L).addFunction("Function", [x](int v) -> int { return x + Function(v); }); runLua("result = Function (12)"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(42, result()); } namespace { class SystemDestroyer {}; } // namespacw TEST_F(NamespaceTests, IndexAccessByNonStringShouldNotCrash) { luabridge::getGlobalNamespace(L) .beginNamespace("test") .beginClass("SystemDestroyer") .endClass() .endNamespace(); runLua("result = test[SystemDestroyer]"); EXPECT_TRUE(result().isNil()); } TEST_F(NamespaceTests, NamespaceAsRValueReferenceShouldWork) { auto ns = luabridge::getGlobalNamespace(L); ns = ns.beginNamespace("test"); auto cls = ns.beginClass("SystemDestroyer"); ns = cls.endClass(); ns.endNamespace(); runLua("result = test[SystemDestroyer]"); EXPECT_TRUE(result().isNil()); } #ifdef _M_IX86 // Windows 32bit only namespace { int __stdcall StdCall(int i) { return i + 10; } } // namespace TEST_F(NamespaceTests, StdCallFunctions) { luabridge::getGlobalNamespace(L).addFunction("StdCall", &StdCall); runLua("result = StdCall (2)"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(12, result()); } namespace { int __fastcall FastCall(int i) { return i + 10; } } // namespace TEST_F(NamespaceTests, FastCallFunctions) { luabridge::getGlobalNamespace(L).addFunction("FastCall", &FastCall); runLua("result = FastCall (2)"); ASSERT_TRUE(result().isNumber()); ASSERT_EQ(12, result()); } #endif // _M_IX86