MCPcopy Create free account
hub / github.com/ThePhD/sol2 / call_it

Function call_it

examples/source/indirect_function_calls.cpp:6–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6sol::variadic_results call_it(sol::object function_name,
7 sol::variadic_args args, sol::this_environment env,
8 sol::this_state L) {
9 sol::state_view lua = L;
10 // default to global table as environment
11 sol::environment function_environment = lua.globals();
12 if (env) {
13 // if we have an environment, use that instead
14 function_environment = env;
15 }
16
17 // get and call the function
18 sol::protected_function pf
19 = function_environment[function_name];
20 sol::protected_function_result res = pf(args);
21
22 //
23 sol::variadic_results results;
24 if (!res.valid()) {
25 // something went wrong: log/crash/whatever
26 return results;
27 }
28 int returncount = res.return_count();
29 for (int i = 0; i < returncount; i++) {
30 // pass offset to get the object that was returned
31 sol::object obj = res.get<sol::object>(i);
32 results.push_back(obj);
33 }
34 // return the results
35 return results;
36}
37
38int main(int, char*[]) {
39 std::cout << "=== indirect function calls ===" << std::endl;

Callers

nothing calls this directly

Calls 4

globalsMethod · 0.80
validMethod · 0.45
return_countMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected