From 377472a6dd2c9bdff4d5667f5789c49aee2abb4c Mon Sep 17 00:00:00 2001 From: Miodrag Milanovic Date: Fri, 7 Oct 2016 14:43:09 +0200 Subject: Added sol2 header only library as future replacement for luabridge (nw) --- 3rdparty/sol2/examples/protected_functions.cpp | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 3rdparty/sol2/examples/protected_functions.cpp (limited to '3rdparty/sol2/examples/protected_functions.cpp') diff --git a/3rdparty/sol2/examples/protected_functions.cpp b/3rdparty/sol2/examples/protected_functions.cpp new file mode 100644 index 00000000000..340f0134d63 --- /dev/null +++ b/3rdparty/sol2/examples/protected_functions.cpp @@ -0,0 +1,47 @@ +#include + +#include + +int main() { + std::cout << "=== protected_functions example ===" << std::endl; + + sol::state lua; + + // A complicated function which can error out + // We define both in terms of Lua code + + lua.script(R"( + function handler (message) + return "Handled this message: " .. message + end + + function f (a) + if a < 0 then + error("negative number detected") + end + return a + 5 + end + )"); + + // Get a protected function out of Lua + sol::protected_function f = lua["f"]; + // Set a non-default error handler + f.error_handler = lua["handler"]; + + sol::protected_function_result result = f(-500); + if (result.valid()) { + // Call succeeded + int x = result; + std::cout << "call succeeded, result is " << x << std::endl; + } + else { + // Call failed + sol::error err = result; + std::string what = err.what(); + std::cout << "call failed, sol::error::what() is " << what << std::endl; + // 'what' Should read + // "Handled this message: negative number detected" + } + + std::cout << std::endl; +} -- cgit v1.2.3-70-g09d2