// Shows how compiled Lua modules are consumed as C++ packages. #include #include #include #include "greet.hpp" #include "mathx.hpp" int main() { luacpp::State state; // 1. Namespaced facade generated from the exported Lua functions. auto mathx = demo::mathx::bind(state); auto greet = demo::greet::bind(state); std::cout << "mathx.add(2, 3) = " << mathx.add(2, 3).to_string() << "\n"; std::cout << "mathx.factorial(6) = " << mathx.factorial(6).to_string() << "\n"; std::cout << "greet.hello(\"Buffy\") = " << greet.hello("Buffy").to_string() << "\n"; std::cout << "greet.double_factorial(5) = " << greet.double_factorial(5).to_string() << "\n"; // 2. Dynamic module handle (fallback when no facade is generated). luacpp::Module mathx_module = state.module("demo.mathx"); std::vector values{3.0, 4.0, 5.0}; luacpp::Value summary = mathx_module.get("summarise").call(values); std::cout << "summarise({3,4,5}).sum = " << summary.get("sum").to_string() << "\n"; std::cout << "summarise({3,4,5}).average = " << summary.get("average").to_string() << "\n"; // 3. Host values visible from Lua, and Lua errors turned into C++ exceptions. state.set_global("cpp_value", luacpp::Value(42)); state.do_string("print('lua sees cpp_value = ' .. cpp_value)"); try { mathx.add("not a number", 1); } catch (const luacpp::Error& error) { std::cout << "caught Lua error: " << error.what() << "\n"; } return 0; }