mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Merge branch 'develop' into sol3
# Conflicts: # include/single/sol/sol.hpp # include/single/sol/sol_forward.hpp # include/sol/coroutine.hpp # include/sol/stack_core.hpp # sol/usertype_metatable.hpp
This commit is contained in:
commit
d15709d85a
|
@ -46,7 +46,7 @@ namespace sol {
|
||||||
template <bool is_yielding, typename... Sig, typename Fx, typename... Args>
|
template <bool is_yielding, typename... Sig, typename Fx, typename... Args>
|
||||||
static void select_convertible(std::false_type, types<Sig...>, lua_State* L, Fx&& fx, Args&&... args) {
|
static void select_convertible(std::false_type, types<Sig...>, lua_State* L, Fx&& fx, Args&&... args) {
|
||||||
typedef std::remove_pointer_t<std::decay_t<Fx>> clean_fx;
|
typedef std::remove_pointer_t<std::decay_t<Fx>> clean_fx;
|
||||||
typedef function_detail::functor_function<clean_fx, is_yielding> F;
|
typedef function_detail::functor_function<clean_fx, is_yielding, true> F;
|
||||||
set_fx<false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
|
set_fx<false, F>(L, std::forward<Fx>(fx), std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace function_detail {
|
namespace function_detail {
|
||||||
template <typename Func, bool is_yielding>
|
template <typename Func, bool is_yielding, bool no_trampoline>
|
||||||
struct functor_function {
|
struct functor_function {
|
||||||
typedef std::decay_t<meta::unwrap_unqualified_t<Func>> function_type;
|
typedef std::decay_t<meta::unwrap_unqualified_t<Func>> function_type;
|
||||||
function_type fx;
|
function_type fx;
|
||||||
|
@ -49,8 +49,13 @@ namespace function_detail {
|
||||||
}
|
}
|
||||||
|
|
||||||
int operator()(lua_State* L) {
|
int operator()(lua_State* L) {
|
||||||
auto f = [&](lua_State*) -> int { return this->call(L); };
|
if (!no_trampoline) {
|
||||||
return detail::trampoline(L, f);
|
auto f = [&](lua_State*) -> int { return this->call(L); };
|
||||||
|
return detail::trampoline(L, f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return call(L);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -28,8 +28,14 @@
|
||||||
#include "stack_get.hpp"
|
#include "stack_get.hpp"
|
||||||
#include "stack_check.hpp"
|
#include "stack_check.hpp"
|
||||||
#include "optional.hpp"
|
#include "optional.hpp"
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
#include <optional>
|
||||||
|
#endif // C++17
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace stack {
|
namespace stack {
|
||||||
|
@ -137,6 +143,17 @@ namespace stack {
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
template <typename T>
|
||||||
|
struct getter<std::optional<T>> {
|
||||||
|
static std::optional<T> get(lua_State* L, int index, record& tracking) {
|
||||||
|
if (!unqualified_check<T>(L, index, no_panic)) {
|
||||||
|
tracking.use(static_cast<int>(!lua_isnone(L, index)));
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return stack_detail::unchecked_unqualified_get<T>(L, index, tracking);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
||||||
template <typename... Tn>
|
template <typename... Tn>
|
||||||
struct check_getter<std::variant<Tn...>> {
|
struct check_getter<std::variant<Tn...>> {
|
||||||
|
|
|
@ -41,6 +41,9 @@
|
||||||
#include <forward_list>
|
#include <forward_list>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
#include <optional>
|
||||||
|
#endif // C++17
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
@ -1017,6 +1020,13 @@ namespace sol {
|
||||||
return stack_detail::unchecked_unqualified_get<optional<T>>(L, index, tracking);
|
return stack_detail::unchecked_unqualified_get<optional<T>>(L, index, tracking);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
template <typename T>
|
||||||
|
inline decltype(auto) tagged_unqualified_get(types<std::optional<T>>, lua_State* L, int index, record& tracking) {
|
||||||
|
return stack_detail::unchecked_unqualified_get<std::optional<T>>(L, index, tracking);
|
||||||
|
}
|
||||||
|
#endif // shitty optional
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline auto tagged_get(types<T>, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get<T>(L, index, tracking)) {
|
inline auto tagged_get(types<T>, lua_State* L, int index, record& tracking) -> decltype(stack_detail::unchecked_get<T>(L, index, tracking)) {
|
||||||
if (is_lua_reference<T>::value) {
|
if (is_lua_reference<T>::value) {
|
||||||
|
@ -1030,6 +1040,14 @@ namespace sol {
|
||||||
inline decltype(auto) tagged_get(types<optional<T>>, lua_State* L, int index, record& tracking) {
|
inline decltype(auto) tagged_get(types<optional<T>>, lua_State* L, int index, record& tracking) {
|
||||||
return stack_detail::unchecked_get<optional<T>>(L, index, tracking);
|
return stack_detail::unchecked_get<optional<T>>(L, index, tracking);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
template <typename T>
|
||||||
|
inline decltype(auto) tagged_get(types<std::optional<T>>, lua_State* L, int index, record& tracking) {
|
||||||
|
return stack_detail::unchecked_get<std::optional<T>>(L, index, tracking);
|
||||||
|
}
|
||||||
|
#endif // shitty optional
|
||||||
|
|
||||||
#else
|
#else
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline decltype(auto) tagged_unqualified_get(types<T>, lua_State* L, int index, record& tracking) {
|
inline decltype(auto) tagged_unqualified_get(types<T>, lua_State* L, int index, record& tracking) {
|
||||||
|
|
|
@ -171,7 +171,11 @@ namespace stack {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
#if defined(LUA_NILINTABLE) && LUA_NILINTABLE
|
||||||
|
lua_pop(L, vi);
|
||||||
|
#else
|
||||||
lua_pop(L, (vi + 1));
|
lua_pop(L, (vi + 1));
|
||||||
|
#endif
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -882,6 +886,8 @@ namespace stack {
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
|
||||||
|
|
||||||
|
|
||||||
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
|
||||||
template <typename... Tn>
|
template <typename... Tn>
|
||||||
struct getter<std::variant<Tn...>> {
|
struct getter<std::variant<Tn...>> {
|
||||||
|
|
|
@ -1630,8 +1630,6 @@ TEST_CASE("usertype/runtime-extensibility", "Check if usertypes are runtime exte
|
||||||
class derived_b : public base_a {
|
class derived_b : public base_a {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SECTION("just functions") {
|
SECTION("just functions") {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
lua.open_libraries(sol::lib::base);
|
lua.open_libraries(sol::lib::base);
|
||||||
|
@ -1742,8 +1740,12 @@ end
|
||||||
REQUIRE(pfr0.valid());
|
REQUIRE(pfr0.valid());
|
||||||
auto pfr1 = lua.safe_script("function B:c() print('B') return 2 end", sol::script_pass_on_error);
|
auto pfr1 = lua.safe_script("function B:c() print('B') return 2 end", sol::script_pass_on_error);
|
||||||
REQUIRE(pfr1.valid());
|
REQUIRE(pfr1.valid());
|
||||||
auto pfr2 = lua.safe_script("local obja = A.new() local objb = B.new() assert(obja:c() == 1) assert(objb:c() == 2)", sol::script_pass_on_error);
|
auto pfr2 = lua.safe_script("obja = A.new() objb = B.new()", sol::script_default_on_error);
|
||||||
REQUIRE(pfr2.valid());
|
REQUIRE(pfr2.valid());
|
||||||
|
auto pfr3 = lua.safe_script("assert(obja:c() == 1)", sol::script_default_on_error);
|
||||||
|
REQUIRE(pfr3.valid());
|
||||||
|
auto pfr4 = lua.safe_script("assert(objb:c() == 2)", sol::script_default_on_error);
|
||||||
|
REQUIRE(pfr4.valid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,10 +72,14 @@ TEST_CASE("utility/variant", "test that variant can be round-tripped") {
|
||||||
return v == 2;
|
return v == 2;
|
||||||
});
|
});
|
||||||
lua["v"] = std::variant<float, int, std::string>(2);
|
lua["v"] = std::variant<float, int, std::string>(2);
|
||||||
REQUIRE_NOTHROW([&]() {
|
{
|
||||||
lua.safe_script("assert(f(v))");
|
auto result = lua.safe_script("assert(f(v))", sol::script_pass_on_error);
|
||||||
lua.safe_script("assert(g(v))");
|
REQUIRE(result.valid());
|
||||||
}());
|
};
|
||||||
|
{
|
||||||
|
auto result = lua.safe_script("assert(g(v))", sol::script_pass_on_error);
|
||||||
|
REQUIRE(result.valid());
|
||||||
|
};
|
||||||
}
|
}
|
||||||
SECTION("throws") {
|
SECTION("throws") {
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
|
@ -103,6 +107,53 @@ TEST_CASE("utility/variant", "test that variant can be round-tripped") {
|
||||||
#endif // C++17
|
#endif // C++17
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("utility/optional", "test that shit optional can be round-tripped") {
|
||||||
|
#ifdef SOL_CXX17_FEATURES
|
||||||
|
SECTION("okay") {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base);
|
||||||
|
|
||||||
|
lua.set_function("f", [](int v) {
|
||||||
|
return v == 2;
|
||||||
|
});
|
||||||
|
lua.set_function("g", [](std::optional<int> vv) {
|
||||||
|
return vv && *vv == 2;
|
||||||
|
});
|
||||||
|
lua["v"] = std::optional<int>(2);
|
||||||
|
{
|
||||||
|
auto result = lua.safe_script("assert(f(v))", sol::script_pass_on_error);
|
||||||
|
REQUIRE(result.valid());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
auto result = lua.safe_script("assert(g(v))", sol::script_pass_on_error);
|
||||||
|
REQUIRE(result.valid());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SECTION("throws") {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base);
|
||||||
|
|
||||||
|
lua.set_function("f", [](int v) {
|
||||||
|
return v == 2;
|
||||||
|
});
|
||||||
|
lua.set_function("g", [](std::optional<int> vv) {
|
||||||
|
return vv && *vv == 2;
|
||||||
|
});
|
||||||
|
lua["v"] = std::optional<int>(std::nullopt);
|
||||||
|
{
|
||||||
|
auto result = lua.safe_script("assert(f(v))", sol::script_pass_on_error);
|
||||||
|
REQUIRE_FALSE(result.valid());
|
||||||
|
};
|
||||||
|
{
|
||||||
|
auto result = lua.safe_script("assert(g(v))", sol::script_pass_on_error);
|
||||||
|
REQUIRE_FALSE(result.valid());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
REQUIRE(true);
|
||||||
|
#endif // C++17
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("utility/string_view", "test that string_view can be taken as an argument") {
|
TEST_CASE("utility/string_view", "test that string_view can be taken as an argument") {
|
||||||
#ifdef SOL_CXX17_FEATURES
|
#ifdef SOL_CXX17_FEATURES
|
||||||
sol::state lua;
|
sol::state lua;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user