diff options
author | 2016-10-07 14:43:09 +0200 | |
---|---|---|
committer | 2016-10-07 14:43:09 +0200 | |
commit | 377472a6dd2c9bdff4d5667f5789c49aee2abb4c (patch) | |
tree | a0ce4d6fc3db1524609efc61307d48448e703f38 /3rdparty/sol2/examples/protected_functions.cpp | |
parent | 1335933ce00cf6095e403a24d0b1dd9e0b565650 (diff) |
Added sol2 header only library as future replacement for luabridge (nw)
Diffstat (limited to '3rdparty/sol2/examples/protected_functions.cpp')
-rw-r--r-- | 3rdparty/sol2/examples/protected_functions.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
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 <sol.hpp> + +#include <iostream> + +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; +} |