// https://github.com/kunitoki/LuaBridge3 // Copyright 2022, Lucio Asnaghi // SPDX-License-Identifier: MIT #include "TestBase.h" #include "SharedCode.h" #include #include #if _WIN32 #include #elif __APPLE__ #include #include #else #include #endif namespace { template struct ScopedGuard { ScopedGuard(F func) : func_(std::move(func)) { } ~ScopedGuard() { func_(); } private: F func_; }; template ScopedGuard(F) -> ScopedGuard; std::optional getExecutablePath() { #if _WIN32 TCHAR path[MAX_PATH]; auto pathLength = GetModuleFileName(NULL, path, MAX_PATH); if (pathLength > 0) return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); #elif __APPLE__ char path[1024]; std::uint32_t pathLength = sizeof(path); if (_NSGetExecutablePath(path, &pathLength) == 0) return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); #else char path[1024]; auto pathLength = readlink("/proc/self/exe", path, sizeof(path)); if (pathLength > 0) return std::filesystem::canonical(std::string_view(path, static_cast(pathLength))); #endif return std::nullopt; } auto openSharedLibrary(const char* library_path) { #if _WIN32 return LoadLibrary(library_path); #else return dlopen(library_path, RTLD_NOW | RTLD_LAZY); #endif } template void closeSharedLibrary(T handle) { #if _WIN32 FreeLibrary(handle); #else dlclose(handle); #endif } template auto lookupSharedLibrarySymbol(T handle, const char* procedure_name) { #if _WIN32 return reinterpret_cast(GetProcAddress(handle, procedure_name)); #else return reinterpret_cast(dlsym(handle, procedure_name)); #endif } int callSharedClassMethod(xyz::ISharedClass* s) { return s->publicMethod("1337"); } } // namespace struct DynamicLibraryTests : TestBase { auto loadSharedLibrary() -> decltype(openSharedLibrary(std::declval())) { auto executablePath = getExecutablePath(); if (! executablePath.has_value()) return nullptr; auto libraryPath = executablePath->remove_filename() / LUABRIDGEDEMO_SHARED_LIBRARY; if (! std::filesystem::exists(libraryPath)) return nullptr; return openSharedLibrary(libraryPath.string().c_str()); } }; TEST_F(DynamicLibraryTests, ExampleUsageFromLibrary) { auto dll = loadSharedLibrary(); ASSERT_NE(nullptr, dll); auto unloadDll = ScopedGuard([dll] { closeSharedLibrary(dll); }); auto allocator = lookupSharedLibrarySymbol(dll, "allocator"); ASSERT_NE(nullptr, allocator); auto deallocator = lookupSharedLibrarySymbol(dll, "deallocator"); ASSERT_NE(nullptr, deallocator); luabridge::getGlobalNamespace(L) .beginClass("SharedClass") .addFactory(allocator, deallocator) .addFunction("publicMethod", &xyz::ISharedClass::publicMethod) .endClass(); luabridge::getGlobalNamespace(L) .addFunction("callSharedClassMethod", &callSharedClassMethod); xyz::ISharedClass* shared = allocator(); auto unloadSharedClass = ScopedGuard([shared, deallocator] { deallocator(shared); }); luabridge::setGlobal(L, shared, "shared"); runLua("result = shared:publicMethod('42')"); EXPECT_EQ(84, result()); runLua("result = callSharedClassMethod(shared)"); EXPECT_EQ(1379, result()); runLua("a = SharedClass(); result = callSharedClassMethod(a)"); EXPECT_EQ(1379, result()); runLua("b = SharedClass(); result = b"); auto ptr = result(); ASSERT_NE(nullptr, ptr); EXPECT_EQ(1379, callSharedClassMethod(ptr)); closeLuaState(); // Force garbage collection before we unload the deleter } #if !LUABRIDGE_ON_LUAU namespace { void* allocFunction(void*, void* ptr, std::size_t osize, std::size_t nsize) { static std::set allocs; void* nptr = nullptr; if (nsize > 0) { nptr = new uint8_t[nsize]; if (ptr != nullptr) std::memcpy(nptr, ptr, nsize < osize ? nsize : osize); allocs.emplace(nptr); } auto node = allocs.extract(ptr); if (! node.empty() && ptr != nullptr) delete[] static_cast(ptr); return nptr; } } // namespace TEST_F(DynamicLibraryTests, ExampleRegistrationFromLibrary) { auto dll = loadSharedLibrary(); ASSERT_NE(nullptr, dll); auto unloadDll = ScopedGuard([dll] { closeSharedLibrary(dll); }); auto registerAnotherClass = lookupSharedLibrarySymbol(dll, "registerAnotherClass"); ASSERT_NE(nullptr, registerAnotherClass); closeLuaState(); L = createNewLuaState(&allocFunction); registerAnotherClass(L); runLua("a = dll.AnotherClass(); result = a.value"); EXPECT_EQ(30, result()); runLua("b = dll.AnotherClass(); b:publicMethod(12); result = b.value"); EXPECT_EQ(12, result()); runLua("c = dll.AnotherClass(); result = c:publicMethod(12)"); EXPECT_EQ(12, result()); runLua("d = dll.AnotherClass(); result = d:publicConstMethod(12)"); EXPECT_EQ(42, result()); closeLuaState(); // Force garbage collection } #endif