aboutsummaryrefslogtreecommitdiffstats
path: root/deps/sol2/examples/source/indirect_function_calls.cpp
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:19:31 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-08-30 00:19:31 -0400
commitbd3fe0cac583739bc0d7c4b5c8f301bb350abca0 (patch)
tree7eeb1aabcebd6999de1c3457d0882246ec0ff4d4 /deps/sol2/examples/source/indirect_function_calls.cpp
parent2662ac356ce14dacfbc91689fd37244facff4989 (diff)
Renamed lib to deps so github will ignore it for language stats
Diffstat (limited to 'deps/sol2/examples/source/indirect_function_calls.cpp')
-rw-r--r--deps/sol2/examples/source/indirect_function_calls.cpp75
1 files changed, 75 insertions, 0 deletions
diff --git a/deps/sol2/examples/source/indirect_function_calls.cpp b/deps/sol2/examples/source/indirect_function_calls.cpp
new file mode 100644
index 0000000..da9e13c
--- /dev/null
+++ b/deps/sol2/examples/source/indirect_function_calls.cpp
@@ -0,0 +1,75 @@
+#define SOL_ALL_SAFETIES_ON 1
+#include <sol/sol.hpp>
+
+#include "assert.hpp"
+#include <iostream>
+
+sol::variadic_results call_it(sol::object function_name, sol::variadic_args args, sol::this_environment env, sol::this_state L) {
+ sol::state_view lua = L;
+ // default to global table as environment
+ sol::environment function_environment = lua.globals();
+ if (env) {
+ // if we have an environment, use that instead
+ function_environment = env;
+ }
+
+ // get and call the function
+ sol::protected_function pf = function_environment[function_name];
+ sol::protected_function_result res = pf(args);
+
+ //
+ sol::variadic_results results;
+ if (!res.valid()) {
+ // something went wrong: log/crash/whatever
+ return results;
+ }
+ int returncount = res.return_count();
+ for (int i = 0; i < returncount; i++) {
+ // pass offset to get the object that was returned
+ sol::object obj = res.get<sol::object>(i);
+ results.push_back(obj);
+ }
+ // return the results
+ return results;
+}
+
+int main(int, char* []) {
+ std::cout << "=== indirect function calls ===" << std::endl;
+
+ sol::state lua;
+ lua.open_libraries(sol::lib::base);
+
+ lua["call_it"] = call_it;
+
+ // some functions to call
+ lua.script(R"(
+function add (a, b)
+ return a + b;
+end
+
+function subtract (a, b)
+ return a - b;
+end
+
+function log (x)
+ print(x)
+end
+)");
+
+ // call the functions indirectly, using a name
+ lua.script(R"(
+ call_it("log", "hiyo")
+ call_it("log", 24)
+ subtract_result = call_it("subtract", 5, 1)
+ add_result = call_it("add", 5, 1)
+ )");
+
+ int subtract_result = lua["subtract_result"];
+ int add_result = lua["add_result"];
+
+ c_assert(add_result == 6);
+ c_assert(subtract_result == 4);
+
+ std::cout << std::endl;
+ return 0;
+}