2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2017-11-08 09:19:36 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::variadic_results call_it(sol::object function_name,
|
|
|
|
sol::variadic_args args, sol::this_environment env,
|
|
|
|
sol::this_state L) {
|
2017-11-08 09:19:36 +08:00
|
|
|
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
|
2021-03-06 23:14:48 +08:00
|
|
|
sol::protected_function pf
|
|
|
|
= function_environment[function_name];
|
2017-11-08 09:19:36 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
int main(int, char*[]) {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== indirect function calls ===" << std::endl;
|
2017-11-08 09:19:36 +08:00
|
|
|
|
|
|
|
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"];
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(add_result == 6);
|
|
|
|
sol_c_assert(subtract_result == 4);
|
2017-11-08 09:19:36 +08:00
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|