2017-08-25 02:39:02 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS 1
|
2018-09-28 13:27:38 +08:00
|
|
|
#include <sol/sol.hpp>
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
2017-12-26 12:27:22 +08:00
|
|
|
#include "assert.hpp"
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
struct two_things {
|
|
|
|
int a;
|
|
|
|
bool b;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
|
|
|
|
// First, the expected size
|
|
|
|
// Specialization of a struct
|
|
|
|
template <>
|
|
|
|
struct lua_size<two_things> : std::integral_constant<int, 2> {};
|
|
|
|
|
2017-02-21 07:07:02 +08:00
|
|
|
// Then, specialize the type
|
|
|
|
// this makes sure Sol can return it properly
|
|
|
|
template <>
|
|
|
|
struct lua_type_of<two_things> : std::integral_constant<sol::type, sol::type::poly> {};
|
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
// Now, specialize various stack structures
|
|
|
|
namespace stack {
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct checker<two_things> {
|
|
|
|
template <typename Handler>
|
|
|
|
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
2016-08-23 10:45:06 +08:00
|
|
|
// 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
|
|
|
|
int absolute_index = lua_absindex(L, index);
|
2016-08-11 08:39:30 +08:00
|
|
|
// Check first and second second index for being the proper types
|
2017-04-03 04:10:00 +08:00
|
|
|
bool success = stack::check<int>(L, absolute_index, handler)
|
|
|
|
&& stack::check<bool>(L, absolute_index + 1, handler);
|
2016-08-11 08:39:30 +08:00
|
|
|
tracking.use(2);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct getter<two_things> {
|
|
|
|
static two_things get(lua_State* L, int index, record& tracking) {
|
2016-08-23 10:45:06 +08:00
|
|
|
int absolute_index = lua_absindex(L, index);
|
2016-08-11 08:39:30 +08:00
|
|
|
// Get the first element
|
2016-08-23 10:45:06 +08:00
|
|
|
int a = stack::get<int>(L, absolute_index);
|
|
|
|
// Get the second element,
|
2016-08-11 08:39:30 +08:00
|
|
|
// in the +1 position from the first
|
2016-08-23 10:45:06 +08:00
|
|
|
bool b = stack::get<bool>(L, absolute_index + 1);
|
2016-08-11 08:39:30 +08:00
|
|
|
// we use 2 slots, each of the previous takes 1
|
|
|
|
tracking.use(2);
|
|
|
|
return two_things{ a, b };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct pusher<two_things> {
|
|
|
|
static int push(lua_State* L, const two_things& things) {
|
|
|
|
int amount = stack::push(L, things.a);
|
2016-08-23 10:45:06 +08:00
|
|
|
// amount will be 1: int pushes 1 item
|
2016-08-11 08:39:30 +08:00
|
|
|
amount += stack::push(L, things.b);
|
2016-08-23 10:45:06 +08:00
|
|
|
// amount 2 now, since bool pushes a single item
|
2016-08-11 08:39:30 +08:00
|
|
|
// Return 2 things
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2018-03-16 05:16:28 +08:00
|
|
|
std::cout << "=== customization ===" << std::endl;
|
2017-02-21 07:07:02 +08:00
|
|
|
std::cout << std::boolalpha;
|
|
|
|
|
2016-08-11 08:39:30 +08:00
|
|
|
sol::state lua;
|
2017-02-21 07:07:02 +08:00
|
|
|
lua.open_libraries(sol::lib::base);
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
// Create a pass-through style of function
|
2017-02-21 07:07:02 +08:00
|
|
|
lua.script("function f ( a, b ) print(a, b) return a, b end");
|
2016-08-11 08:39:30 +08:00
|
|
|
|
|
|
|
// get the function out of Lua
|
|
|
|
sol::function f = lua["f"];
|
|
|
|
|
2017-02-21 07:07:02 +08:00
|
|
|
two_things things = f(two_things{ 24, false });
|
2017-12-26 12:27:22 +08:00
|
|
|
c_assert(things.a == 24);
|
|
|
|
c_assert(things.b == false);
|
2016-08-11 08:39:30 +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;
|
|
|
|
}
|