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-12-11 04:56:49 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
|
|
struct number_shim {
|
|
|
|
double num = 0;
|
|
|
|
};
|
|
|
|
|
2018-12-21 14:07:05 +08:00
|
|
|
template <typename Handler>
|
2021-03-06 23:14:48 +08:00
|
|
|
bool sol_lua_check(sol::types<number_shim>, lua_State* L,
|
|
|
|
int index, Handler&& handler,
|
|
|
|
sol::stack::record& tracking) {
|
|
|
|
// check_usertype is a backdoor for directly checking sol2
|
|
|
|
// usertypes
|
|
|
|
if (!sol::stack::check_usertype<number_shim>(L, index)
|
|
|
|
&& !sol::stack::check<double>(L, index)) {
|
|
|
|
handler(L,
|
|
|
|
index,
|
|
|
|
sol::type_of(L, index),
|
|
|
|
sol::type::userdata,
|
|
|
|
"expected a number_shim or a number");
|
2018-12-21 14:07:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tracking.use(1);
|
|
|
|
return true;
|
|
|
|
}
|
2017-12-11 04:56:49 +08:00
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
number_shim sol_lua_get(sol::types<number_shim>, lua_State* L,
|
|
|
|
int index, sol::stack::record& tracking) {
|
2018-12-21 14:07:05 +08:00
|
|
|
if (sol::stack::check_usertype<number_shim>(L, index)) {
|
2021-03-06 23:14:48 +08:00
|
|
|
number_shim& ns
|
|
|
|
= sol::stack::get_usertype<number_shim>(
|
|
|
|
L, index, tracking);
|
2018-12-21 14:07:05 +08:00
|
|
|
return ns;
|
|
|
|
}
|
2021-03-06 14:03:23 +08:00
|
|
|
number_shim ns {};
|
2018-12-21 14:07:05 +08:00
|
|
|
ns.num = sol::stack::get<double>(L, index, tracking);
|
|
|
|
return ns;
|
|
|
|
}
|
2017-12-11 04:56:49 +08:00
|
|
|
|
|
|
|
int main() {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
lua.script("vf = f(25) vg = g(35)");
|
2021-03-06 14:03:23 +08:00
|
|
|
|
2017-12-11 04:56:49 +08:00
|
|
|
number_shim thingsf = lua["vf"];
|
|
|
|
number_shim thingsg = lua["vg"];
|
|
|
|
|
2022-09-28 13:56:26 +08:00
|
|
|
SOL_ASSERT(thingsf.num == 25);
|
|
|
|
SOL_ASSERT(thingsg.num == 35);
|
2017-12-11 04:56:49 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|