From ff9d380846f1f3f1ebcd595d8feef1828cc82095 Mon Sep 17 00:00:00 2001 From: BlackAngelTVdev Date: Wed, 18 Nov 2026 08:05:41 +0100 Subject: [PATCH] feat(demo): add a standalone project exercising compiled modules A separate CMake project using the compiler as a subproject shows the real workflow: namespaced facades, require between compiled modules and dynamic access from C++. --- Demo/CMakeLists.txt | 19 +++++++++++++++++ Demo/lua/report.lua | 39 ++++++++++++++++++++++++++++++++++ Demo/lua/todo.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++ Demo/main.cpp | 32 ++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 Demo/CMakeLists.txt create mode 100644 Demo/lua/report.lua create mode 100644 Demo/lua/todo.lua create mode 100644 Demo/main.cpp diff --git a/Demo/CMakeLists.txt b/Demo/CMakeLists.txt new file mode 100644 index 0000000..9277331 --- /dev/null +++ b/Demo/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.20) +project(luacpp_demo LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Reuse the compiler from the repository root as a subproject. +set(LUACPP_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) +set(LUACPP_BUILD_TESTS OFF CACHE BOOL "" FORCE) +add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/.." "${CMAKE_CURRENT_BINARY_DIR}/luacpp") +include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/lua-package.cmake") + +add_lua_package(demo_lua + ROOT "${CMAKE_CURRENT_SOURCE_DIR}/lua" + MODULES todo.lua report.lua + NAMESPACE demo) + +add_executable(luacpp_demo main.cpp) +target_link_libraries(luacpp_demo PRIVATE demo_lua) diff --git a/Demo/lua/report.lua b/Demo/lua/report.lua new file mode 100644 index 0000000..0e286c0 --- /dev/null +++ b/Demo/lua/report.lua @@ -0,0 +1,39 @@ +-- Demo module that requires another compiled module (demo.todo). +local todo = require("demo.todo") + +local M = {} + +function M.summary() + return string.format( + "todo v%s : %d element(s), %d en attente", + todo.version(), todo.count(), todo.pending() + ) +end + +function M.render() + local lines = {} + for _, item in ipairs(todo.list()) do + lines[#lines + 1] = string.format( + "[%s] #%d %s (p%d)", + item.done and "x" or " ", item.id, item.text, item.priority + ) + end + return table.concat(lines, "\n") +end + +function M.stats() + local total = todo.count() + local finished = 0 + for _, item in ipairs(todo.list()) do + if item.done then + finished = finished + 1 + end + end + return { + total = total, + finished = finished, + ratio = total > 0 and finished / total or 0, + } +end + +return M diff --git a/Demo/lua/todo.lua b/Demo/lua/todo.lua new file mode 100644 index 0000000..5deabbf --- /dev/null +++ b/Demo/lua/todo.lua @@ -0,0 +1,51 @@ +-- Small demo package: a toy todo list, compiled into the C++ package demo::todo. +local M = {} + +local items = {} +local next_id = 0 + +function M.add(text, priority) + next_id = next_id + 1 + local item = { + id = next_id, + text = text, + done = false, + priority = priority or 2, + } + items[#items + 1] = item + return item.id +end + +function M.done(id) + for _, item in ipairs(items) do + if item.id == id then + item.done = true + return true + end + end + return false +end + +function M.count() + return #items +end + +function M.pending() + local n = 0 + for _, item in ipairs(items) do + if not item.done then + n = n + 1 + end + end + return n +end + +function M.list() + return items +end + +function M.version() + return "1.0" +end + +return M diff --git a/Demo/main.cpp b/Demo/main.cpp new file mode 100644 index 0000000..5e5006f --- /dev/null +++ b/Demo/main.cpp @@ -0,0 +1,32 @@ +// Demo program: uses Lua modules compiled into C++ packages. +#include + +#include + +#include "report.hpp" +#include "todo.hpp" + +int main() { + luacpp::State state; + + auto todo = demo::todo::bind(state); + auto report = demo::report::bind(state); + + todo.add("Ecrire le compilateur Lua -> C++", 1); + todo.add("Compiler les modules en packages C++", 1); + todo.add("Ajouter les tests de conformite", 3); + todo.done(2); + + std::cout << report.summary().as_string() << "\n\n"; + std::cout << report.render().as_string() << "\n\n"; + + luacpp::Value stats = report.stats(); + std::cout << "termines : " << stats.get("finished").to_string() << "/" + << stats.get("total").to_string() << "\n"; + + // Dynamic access, without the generated facade. + luacpp::Value items = demo::todo::module(state).get("list").call(); + std::cout << "premier item : " << items.at(1).get("text").as_string() << "\n"; + + return 0; +}