2017-11-10 06:41:46 +08:00
|
|
|
#define SOL_CHECK_ARGUMENTS 1
|
|
|
|
#define SOL_ENABLE_INTEROP 1
|
2016-07-29 12:57:47 +08:00
|
|
|
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include <sol.hpp>
|
|
|
|
|
2016-08-24 09:42:27 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
struct two_things {
|
|
|
|
int a;
|
|
|
|
bool b;
|
|
|
|
};
|
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
struct number_shim {
|
|
|
|
double num = 0;
|
|
|
|
};
|
|
|
|
|
2016-07-29 12:57:47 +08:00
|
|
|
namespace sol {
|
|
|
|
|
|
|
|
// First, the expected size
|
|
|
|
// Specialization of a struct
|
|
|
|
template <>
|
|
|
|
struct lua_size<two_things> : std::integral_constant<int, 2> {};
|
|
|
|
|
2017-07-10 00:00:57 +08:00
|
|
|
// Then, the expected type
|
|
|
|
template <>
|
|
|
|
struct lua_type_of<two_things> : std::integral_constant<sol::type, sol::type::poly> {};
|
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
// do note specialize size for this because it is our type
|
|
|
|
template <>
|
|
|
|
struct lua_type_of<number_shim> : std::integral_constant<sol::type, sol::type::poly> {};
|
|
|
|
|
2016-07-29 12:57:47 +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) {
|
|
|
|
// Check first and second second index for being the proper types
|
|
|
|
bool success = stack::check<int>(L, index, handler)
|
|
|
|
&& stack::check<bool>(L, index + 1, handler);
|
|
|
|
tracking.use(2);
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct getter<two_things> {
|
|
|
|
static two_things get(lua_State* L, int index, record& tracking) {
|
|
|
|
// Get the first element
|
|
|
|
int a = stack::get<int>(L, index);
|
2017-09-17 02:18:45 +08:00
|
|
|
// Get the second element,
|
2016-07-29 12:57:47 +08:00
|
|
|
// in the +1 position from the first
|
|
|
|
bool b = stack::get<bool>(L, index + 1);
|
|
|
|
// 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);
|
|
|
|
amount += stack::push(L, things.b);
|
|
|
|
// Return 2 things
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
template <>
|
|
|
|
struct checker<number_shim> {
|
|
|
|
template <typename Handler>
|
|
|
|
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
|
|
|
// check_usertype is a backdoor for directly checking sol2 usertypes
|
|
|
|
if (!check_usertype<number_shim>(L, index) && !stack::check<double>(L, index)) {
|
|
|
|
handler(L, index, type_of(L, index), type::userdata, "expected a number_shim or a number");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tracking.use(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct getter<number_shim> {
|
|
|
|
static number_shim get(lua_State* L, int index, record& tracking) {
|
|
|
|
if (check_usertype<number_shim>(L, index)) {
|
|
|
|
number_shim& ns = get_usertype<number_shim>(L, index, tracking);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
number_shim ns{};
|
|
|
|
ns.num = stack::get<double>(L, index, tracking);
|
|
|
|
return ns;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-17 02:18:45 +08:00
|
|
|
} // namespace stack
|
|
|
|
} // namespace sol
|
2016-07-29 12:57:47 +08:00
|
|
|
|
2017-07-10 00:00:57 +08:00
|
|
|
TEST_CASE("customization/split struct", "using the newly documented customization points to handle different kinds of classes") {
|
2016-07-29 12:57:47 +08:00
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
// Create a pass-through style of function
|
2017-08-22 03:25:43 +08:00
|
|
|
lua.safe_script("function f ( a, b, c ) return a + c, b end");
|
2016-07-29 12:57:47 +08:00
|
|
|
lua.set_function("g", [](int a, bool b, int c, double d) {
|
|
|
|
return std::make_tuple(a + c, b, d + 2.5);
|
|
|
|
});
|
|
|
|
|
|
|
|
// get the function out of Lua
|
|
|
|
sol::function f = lua["f"];
|
|
|
|
sol::function g = lua["g"];
|
|
|
|
|
|
|
|
two_things thingsf = f(two_things{ 24, true }, 1);
|
|
|
|
two_things thingsg;
|
|
|
|
double d;
|
2017-09-17 02:18:45 +08:00
|
|
|
sol::tie(thingsg, d) = g(two_things{ 25, false }, 2, 34.0);
|
2016-07-29 12:57:47 +08:00
|
|
|
REQUIRE(thingsf.a == 25);
|
|
|
|
REQUIRE(thingsf.b);
|
|
|
|
|
|
|
|
REQUIRE(thingsg.a == 27);
|
|
|
|
REQUIRE_FALSE(thingsg.b);
|
|
|
|
REQUIRE(d == 36.5);
|
|
|
|
}
|
2017-12-11 04:56:49 +08:00
|
|
|
|
|
|
|
TEST_CASE("customization/get_ check_usertype", "using the newly documented customization points to handle different kinds of classes") {
|
|
|
|
sol::state lua;
|
|
|
|
|
|
|
|
// Create a pass-through style of function
|
|
|
|
lua.safe_script("function f ( a ) return a end");
|
|
|
|
lua.set_function("g", [](double a) {
|
|
|
|
number_shim ns;
|
|
|
|
ns.num = a;
|
|
|
|
return ns;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto result = lua.safe_script("vf = f(25) vg = g(35)", sol::script_pass_on_error);
|
|
|
|
REQUIRE(result.valid());
|
|
|
|
|
|
|
|
number_shim thingsf = lua["vf"];
|
|
|
|
number_shim thingsg = lua["vg"];
|
|
|
|
|
|
|
|
REQUIRE(thingsf.num == 25);
|
|
|
|
REQUIRE(thingsg.num == 35);
|
|
|
|
}
|