mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
New test cases for the order of returns. Apparently, its screwing up between both lua and C++. Have to test thoroughly. Changes currently are half-working.
This commit is contained in:
parent
0e31358d97
commit
8ef3ceb8a0
|
@ -36,7 +36,7 @@ private:
|
||||||
template<typename... Ret>
|
template<typename... Ret>
|
||||||
std::tuple<Ret...> invoke(types<Ret...>, std::size_t n) {
|
std::tuple<Ret...> invoke(types<Ret...>, std::size_t n) {
|
||||||
luacall(n, sizeof...(Ret));
|
luacall(n, sizeof...(Ret));
|
||||||
return stack::pop_call(state(), std::make_tuple<Ret...>, types<Ret...>());
|
return stack::pop_reverse_call(state(), std::make_tuple<Ret...>, types<Ret...>());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Ret>
|
template<typename Ret>
|
||||||
|
|
|
@ -63,17 +63,22 @@ struct static_lua_func {
|
||||||
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
typedef typename std::remove_pointer<typename std::decay<TFx>::type>::type fx_t;
|
||||||
typedef function_traits<fx_t> fx_traits;
|
typedef function_traits<fx_t> fx_traits;
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
static int typed_call(types<>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||||
|
return (*this)(types<void>(), t, fx, L);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
static int typed_call(types<void>, types<Args...> t, fx_t* fx, lua_State* L) {
|
static int typed_call(types<void>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||||
stack::pop_call(L, fx, t);
|
stack::pop_call(L, fx, t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
static int typed_call(types<TRn...>, types<Args...> t, fx_t* fx, lua_State* L) {
|
static int typed_call(types<Ret...>, types<Args...> t, fx_t* fx, lua_State* L) {
|
||||||
auto r = stack::pop_call(L, fx, t);
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push(L, std::move(r));
|
stack::push_reverse(L, std::move(r));
|
||||||
return sizeof...(TRn);
|
return sizeof...(Ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int call(lua_State* L) {
|
static int call(lua_State* L) {
|
||||||
|
@ -101,9 +106,9 @@ struct static_object_lua_func {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename TR, typename... Args>
|
template<typename Ret, typename... Args>
|
||||||
static int typed_call(types<TR>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
static int typed_call(types<Ret>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
||||||
auto fx = [&item, &ifx](Args&&... args) -> TR {
|
auto fx = [&item, &ifx](Args&&... args) -> Ret {
|
||||||
return (item.*ifx)(std::forward<Args>(args)...);
|
return (item.*ifx)(std::forward<Args>(args)...);
|
||||||
};
|
};
|
||||||
auto r = stack::pop_call(L, fx, types<Args...>());
|
auto r = stack::pop_call(L, fx, types<Args...>());
|
||||||
|
@ -111,12 +116,12 @@ struct static_object_lua_func {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
static int typed_call(types<TRn...>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
static int typed_call(types<Ret...>, types<Args...>, T& item, fx_t& ifx, lua_State* L) {
|
||||||
auto fx = [&item, &ifx](Args&&... args) -> std::tuple<TRn...> { return (item.*ifx)(std::forward<Args>(args)...); };
|
auto fx = [&item, &ifx](Args&&... args) -> std::tuple<Ret...> { return (item.*ifx)(std::forward<Args>(args)...); };
|
||||||
auto r = stack::pop_call(L, fx, types<Args...>());
|
auto r = stack::pop_call(L, fx, types<Args...>());
|
||||||
stack::push(L, std::move(r));
|
stack::push_reverse(L, std::move(r));
|
||||||
return sizeof...(TRn);
|
return sizeof...(Ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int call(lua_State* L) {
|
static int call(lua_State* L) {
|
||||||
|
@ -182,17 +187,23 @@ struct lambda_lua_func : public lua_func {
|
||||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
||||||
|
stack::pop_call(L, fx, t);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
||||||
stack::pop_call(L, fx, t);
|
stack::pop_call(L, fx, t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
||||||
auto r = stack::pop_call(L, fx, t);
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push(L, r);
|
stack::push_reverse(L, r);
|
||||||
return sizeof...(TRn);
|
return sizeof...(Ret);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -209,17 +220,22 @@ struct explicit_lua_func : public lua_func {
|
||||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
||||||
|
return (*this)(types<void>(), t, L);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
||||||
stack::pop_call(L, fx, t);
|
stack::pop_call(L, fx, t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
||||||
auto r = stack::pop_call(L, fx, t);
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push(L, std::move(r));
|
stack::push_reverse(L, std::move(r));
|
||||||
return sizeof...(TRn);
|
return sizeof...(Ret);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -247,17 +263,22 @@ struct explicit_lua_func<TFx, T, true> : public lua_func {
|
||||||
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
return (*this)(tuple_types<typename fx_traits::return_type>(), typename fx_traits::args_type(), L);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
int operator()(types<>, types<Args...> t, lua_State* L) {
|
||||||
|
return (*this)(types<void>(), t, L);
|
||||||
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
int operator()(types<void>, types<Args...> t, lua_State* L) {
|
||||||
stack::pop_call(L, fx, t);
|
stack::pop_call(L, fx, t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... TRn, typename... Args>
|
template<typename... Ret, typename... Args>
|
||||||
int operator()(types<TRn...>, types<Args...> t, lua_State* L) {
|
int operator()(types<Ret...>, types<Args...> t, lua_State* L) {
|
||||||
auto r = stack::pop_call(L, fx, t);
|
auto r = stack::pop_call(L, fx, t);
|
||||||
stack::push(L, r);
|
stack::push_reverse(L, r);
|
||||||
return sizeof...(TRn);
|
return sizeof...(Ret);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -224,15 +224,43 @@ template<typename F, typename Head, typename... Tail, typename... Vs>
|
||||||
auto ltr_pop(lua_State* L, F&& f, types<Head, Tail...>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)..., std::declval<Head>(), std::declval<Tail>()...)) {
|
auto ltr_pop(lua_State* L, F&& f, types<Head, Tail...>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)..., std::declval<Head>(), std::declval<Tail>()...)) {
|
||||||
return ltr_pop(L, std::forward<F>(f), types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L));
|
return ltr_pop(L, std::forward<F>(f), types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename F, typename... Vs>
|
||||||
|
auto rtl_pop(lua_State*, F&& f, types<>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)...)) {
|
||||||
|
return f(std::forward<Vs>(vs)...);
|
||||||
|
}
|
||||||
|
template<typename F, typename Head, typename... Vs>
|
||||||
|
auto rtl_pop(lua_State* L, F&& f, types<Head>, Vs&&... vs) -> decltype(rtl_pop(L, std::forward<F>(f), types<>(), pop<Head>(L), std::forward<Vs>(vs)...)) {
|
||||||
|
return rtl_pop(L, std::forward<F>(f), types<>(), pop<Head>(L), std::forward<Vs>(vs)...);
|
||||||
|
}
|
||||||
|
template<typename F, typename Head, typename... Tail, typename... Vs>
|
||||||
|
auto rtl_pop(lua_State* L, F&& f, types<Head, Tail...>, Vs&&... vs) -> decltype(rtl_pop(L, std::forward<F>(f), types<Tail...>(), pop<Head>(L), std::forward<Vs>(vs)...)) {
|
||||||
|
return rtl_pop(L, std::forward<F>(f), types<Tail...>(), pop<Head>(L), std::forward<Vs>(vs)...);
|
||||||
|
}
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
inline void push(lua_State* L, const std::tuple<Args...>& tuplen) {
|
inline void push(lua_State* L, const std::tuple<Args...>& tuplen) {
|
||||||
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), tuplen);
|
detail::push_tuple(L, build_indices<sizeof...(Args)>(), tuplen);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
inline void push(lua_State* L, std::tuple<Args...>&& tuplen) {
|
inline void push(lua_State* L, std::tuple<Args...>&& tuplen) {
|
||||||
|
detail::push_tuple(L, build_indices<sizeof...(Args)>(), std::move(tuplen));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline void push_reverse(lua_State* L, T&& item) {
|
||||||
|
push(L, std::forward<T>(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
inline void push_reverse(lua_State* L, const std::tuple<Args...>& tuplen) {
|
||||||
|
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), tuplen);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
inline void push_reverse(lua_State* L, std::tuple<Args...>&& tuplen) {
|
||||||
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), std::move(tuplen));
|
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), std::move(tuplen));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,6 +269,11 @@ inline auto pop_call(lua_State* L, TFx&& fx, types<Args...>) -> decltype(detail:
|
||||||
return detail::ltr_pop(L, std::forward<TFx>(fx), types<Args...>());
|
return detail::ltr_pop(L, std::forward<TFx>(fx), types<Args...>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args, typename TFx>
|
||||||
|
inline auto pop_reverse_call(lua_State* L, TFx&& fx, types<Args...>) -> decltype(detail::rtl_pop(L, std::forward<TFx>(fx), types<Args...>())) {
|
||||||
|
return detail::rtl_pop(L, std::forward<TFx>(fx), reversed<Args...>());
|
||||||
|
}
|
||||||
|
|
||||||
void push_args(lua_State*) {
|
void push_args(lua_State*) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,16 @@ template<size_t... Ns>
|
||||||
struct build_reverse_indices<0, Ns...> : indices<Ns...> {};
|
struct build_reverse_indices<0, Ns...> : indices<Ns...> {};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
struct types : build_indices<sizeof...(Args)> {};
|
struct types : build_indices<sizeof...(Args)> { typedef types type; };
|
||||||
|
|
||||||
|
template<class Acc, class... Args>
|
||||||
|
struct reversed_ : Acc{};
|
||||||
|
|
||||||
|
template<typename... RArgs, typename Arg, typename... Args>
|
||||||
|
struct reversed_<types<RArgs...>, Arg, Args...> : reversed_<types<Arg, RArgs...>, Args...>{};
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
struct reversed : reversed_<types<>, Args...>{};
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
struct tuple_types : types<Args...>, std::false_type {};
|
struct tuple_types : types<Args...>, std::false_type {};
|
||||||
|
|
15
tests.cpp
15
tests.cpp
|
@ -220,6 +220,21 @@ TEST_CASE("tables/functions_variables", "Check if tables and function calls work
|
||||||
REQUIRE_NOTHROW(run_script(lua));
|
REQUIRE_NOTHROW(run_script(lua));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("functions/return _order", "Check if return order is in the same reading order specified in Lua" ) {
|
||||||
|
const static std::tuple<int, int, int> triple = std::make_tuple(10, 11, 12);
|
||||||
|
sol::state lua;
|
||||||
|
lua.set_function( "f", [ ] {
|
||||||
|
return std::make_tuple( 10, 11, 12 );
|
||||||
|
} );
|
||||||
|
lua.script( "function g() return 10, 11, 12 end" );
|
||||||
|
auto tcpp = lua.get<sol::function>( "f" ).call<int, int, int>( );
|
||||||
|
auto tlua = lua.get<sol::function>( "g" ).call<int, int, int>( );
|
||||||
|
std::cout << std::get<0>( tcpp ) << ',' << std::get<1>( tcpp ) << ',' << std::get<2>( tcpp ) << std::endl;
|
||||||
|
std::cout << std::get<0>( tlua ) << ',' << std::get<1>( tlua ) << ',' << std::get<2>( tlua ) << std::endl;
|
||||||
|
REQUIRE( tcpp == triple );
|
||||||
|
REQUIRE( tlua == triple );
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") {
|
TEST_CASE("tables/operator[]", "Check if operator[] retrieval and setting works properly") {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
lua.open_libraries(sol::lib::base);
|
lua.open_libraries(sol::lib::base);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user