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++.
This commit is contained in:
4 files changed
+141
No files matched your search
@@ -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)
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,32 @@
|
||||
// Demo program: uses Lua modules compiled into C++ packages.
|
||||
#include <iostream>
|
||||
|
||||
#include <luacpp/rt.hpp>
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in new issue
Block a user