From 9b135b53b4c5dbf9803f7e444809c03337cdaf0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gr=C3=A9goire?= Date: Tue, 13 Jan 2026 11:25:44 +0100 Subject: [PATCH] Add lua to test/test.cpp --- .github/workflows/linux.yml | 2 +- test/CMakeLists.txt | 11 +++++ test/test.cpp | 92 +++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 729df316..bcad27b3 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -15,7 +15,7 @@ jobs: container: archlinux:base-devel steps: - name: Install dependencies - run: pacman -Syu --noconfirm && pacman -S --noconfirm --needed freetype2 debuginfod wayland dbus libxkbcommon libglvnd meson cmake git wayland-protocols nodejs + run: pacman -Syu --noconfirm && pacman -S --noconfirm --needed freetype2 debuginfod wayland dbus libxkbcommon libglvnd meson cmake git wayland-protocols nodejs lua - name: Trust git repo run: git config --global --add safe.directory '*' - uses: actions/checkout@v4 diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3e3a4990..1fffcdff 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,6 +21,17 @@ add_executable(tracy-test test.cpp) target_link_options(tracy-test PRIVATE -rdynamic) target_link_libraries(tracy-test TracyClient) +# Lua support (optional) +find_package(Lua) +if(LUA_FOUND) + message(STATUS "Lua found, enabling Lua support in tracy-test") + target_compile_definitions(tracy-test PRIVATE TRACY_HAS_LUA) + target_include_directories(tracy-test PRIVATE ${LUA_INCLUDE_DIR}) + target_link_libraries(tracy-test ${LUA_LIBRARIES}) +else() + message(STATUS "Lua not found, building tracy-test without Lua support") +endif() + # OS-specific options if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") diff --git a/test/test.cpp b/test/test.cpp index db074a6f..f7cf494e 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -7,6 +7,13 @@ #include #include "tracy/Tracy.hpp" +#ifdef TRACY_HAS_LUA +#include +#include +#include +#include "tracy/TracyLua.hpp" +#endif + #define STB_IMAGE_IMPLEMENTATION #define STBI_ONLY_JPEG #include "stb_image.h" @@ -344,6 +351,87 @@ void ArenaAllocatorTest() } } +#ifdef TRACY_HAS_LUA + +void LuaTest() +{ + tracy::SetThreadName( "Lua test" ); + + lua_State* L = luaL_newstate(); + luaL_openlibs( L ); + tracy::LuaRegister( L ); + + const char* luaScript = R"( + for i = 1, 10 do + tracy.ZoneBeginN("Lua iteration") + tracy.ZoneText("Iteration: " .. i) + tracy.Message("Lua message: " .. i) + tracy.ZoneEnd() + end + )"; + + for(;;) + { + std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); + ZoneScopedN( "Lua script execution" ); + + if( luaL_dostring( L, luaScript ) != 0 ) + { + const char* error = lua_tostring( L, -1 ); + TracyLogString( tracy::MessageSeverity::Error, 0, TRACY_CALLSTACK, error ); + lua_pop( L, 1 ); + } + + std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); + } + + lua_close( L ); +} + +void LuaHookTest() +{ + tracy::SetThreadName( "Lua hook test" ); + + lua_State* L = luaL_newstate(); + luaL_openlibs( L ); + tracy::LuaRegister( L ); + + // Enable Lua hook for automatic function profiling + lua_sethook( L, tracy::LuaHook, LUA_MASKCALL | LUA_MASKRET, 0 ); + + const char* luaScript = R"( + function fibonacci(n) + if n < 2 then + return n + end + return fibonacci(n-1) + fibonacci(n-2) + end + + for i = 1, 5 do + local result = fibonacci(10) + end + )"; + + for(;;) + { + std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) ); + ZoneScopedN( "Lua hook script execution" ); + + if( luaL_dostring( L, luaScript ) != 0 ) + { + const char* error = lua_tostring( L, -1 ); + TracyLogString( tracy::MessageSeverity::Error, 0, TRACY_CALLSTACK, error ); + lua_pop( L, 1 ); + } + + std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) ); + } + + lua_close( L ); +} + +#endif + int main() { #ifndef _WIN32 @@ -382,6 +470,10 @@ int main() auto t21 = std::thread( DeadlockTest1 ); auto t22 = std::thread( DeadlockTest2 ); auto t23 = std::thread( ArenaAllocatorTest ); +#ifdef TRACY_HAS_LUA + auto t24 = std::thread( LuaTest ); + auto t25 = std::thread( LuaHookTest ); +#endif int x, y; auto image = stbi_load( "image.jpg", &x, &y, nullptr, 4 );