2019-05-22 07:17:31 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
2018-12-23 04:36:42 +08:00
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
struct two_things {
|
|
|
|
int a;
|
|
|
|
bool b;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Handler>
|
2021-03-06 23:14:48 +08:00
|
|
|
bool sol_lua_check(sol::types<two_things>, lua_State* L,
|
|
|
|
int index, Handler&& handler,
|
|
|
|
sol::stack::record& tracking) {
|
|
|
|
// indices can be negative to count backwards from the top
|
|
|
|
// of the stack, rather than the bottom up to deal with
|
|
|
|
// this, we adjust the index to its absolute position using
|
|
|
|
// the lua_absindex function
|
2018-12-23 04:36:42 +08:00
|
|
|
int absolute_index = lua_absindex(L, index);
|
2021-03-06 23:14:48 +08:00
|
|
|
// Check first and second second index for being the proper
|
|
|
|
// types
|
|
|
|
bool success
|
|
|
|
= sol::stack::check<int>(L, absolute_index, handler)
|
|
|
|
&& sol::stack::check<bool>(
|
|
|
|
L, absolute_index + 1, handler);
|
2018-12-23 04:36:42 +08:00
|
|
|
tracking.use(2);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
two_things sol_lua_get(sol::types<two_things>, lua_State* L,
|
|
|
|
int index, sol::stack::record& tracking) {
|
2018-12-23 04:36:42 +08:00
|
|
|
int absolute_index = lua_absindex(L, index);
|
|
|
|
// Get the first element
|
|
|
|
int a = sol::stack::get<int>(L, absolute_index);
|
2020-09-26 18:47:20 +08:00
|
|
|
// Get the second element,
|
2018-12-23 04:36:42 +08:00
|
|
|
// in the +1 position from the first
|
|
|
|
bool b = sol::stack::get<bool>(L, absolute_index + 1);
|
|
|
|
// we use 2 slots, each of the previous takes 1
|
|
|
|
tracking.use(2);
|
2020-09-26 18:47:20 +08:00
|
|
|
return two_things { a, b };
|
2018-12-23 04:36:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 18:47:20 +08:00
|
|
|
int sol_lua_push(lua_State* L, const two_things& things) {
|
2018-12-23 04:36:42 +08:00
|
|
|
int amount = sol::stack::push(L, things.a);
|
|
|
|
// amount will be 1: int pushes 1 item
|
|
|
|
amount += sol::stack::push(L, things.b);
|
|
|
|
// amount 2 now, since bool pushes a single item
|
|
|
|
// Return 2 things
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
std::cout << "=== customization ===" << std::endl;
|
|
|
|
std::cout << std::boolalpha;
|
2020-09-26 18:47:20 +08:00
|
|
|
|
2018-12-23 04:36:42 +08:00
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
|
|
|
// Create a pass-through style of function
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.script(
|
|
|
|
"function f ( a, b ) print(a, b) return a, b end");
|
2018-12-23 04:36:42 +08:00
|
|
|
|
|
|
|
// get the function out of Lua
|
|
|
|
sol::function f = lua["f"];
|
|
|
|
|
2020-09-26 18:47:20 +08:00
|
|
|
two_things things = f(two_things { 24, false });
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(things.a == 24);
|
|
|
|
SOL_ASSERT(things.b == false);
|
2018-12-23 04:36:42 +08:00
|
|
|
// things.a == 24
|
|
|
|
// things.b == true
|
|
|
|
|
|
|
|
std::cout << "things.a: " << things.a << std::endl;
|
|
|
|
std::cout << "things.b: " << things.b << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|