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:
ThePhD 2018-11-21 08:19:26 -05:00
commit d15709d85a
No known key found for this signature in database
GPG Key ID: 1509DB1C0F702BFA
7 changed files with 110 additions and 11 deletions

View File

@ -46,7 +46,7 @@ namespace sol {
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) {
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)...);
}

View File

@ -28,7 +28,7 @@
namespace sol {
namespace function_detail {
template <typename Func, bool is_yielding>
template <typename Func, bool is_yielding, bool no_trampoline>
struct functor_function {
typedef std::decay_t<meta::unwrap_unqualified_t<Func>> function_type;
function_type fx;
@ -49,8 +49,13 @@ namespace function_detail {
}
int operator()(lua_State* L) {
auto f = [&](lua_State*) -> int { return this->call(L); };
return detail::trampoline(L, f);
if (!no_trampoline) {
auto f = [&](lua_State*) -> int { return this->call(L); };
return detail::trampoline(L, f);
}
else {
return call(L);
}
}
};

View File

@ -28,8 +28,14 @@
#include "stack_get.hpp"
#include "stack_check.hpp"
#include "optional.hpp"
#include <cstdlib>
#include <cmath>
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
#include <optional>
#endif // C++17
namespace sol {
namespace stack {
@ -137,6 +143,17 @@ namespace stack {
};
#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
template <typename... Tn>
struct check_getter<std::variant<Tn...>> {

View File

@ -41,6 +41,9 @@
#include <forward_list>
#include <string>
#include <algorithm>
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
#include <optional>
#endif // C++17
namespace sol {
namespace detail {
@ -1017,6 +1020,13 @@ namespace sol {
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>
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) {
@ -1030,6 +1040,14 @@ namespace sol {
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);
}
#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
template <typename T>
inline decltype(auto) tagged_unqualified_get(types<T>, lua_State* L, int index, record& tracking) {

View File

@ -171,7 +171,11 @@ namespace stack {
if (i == 0) {
break;
}
#if defined(LUA_NILINTABLE) && LUA_NILINTABLE
lua_pop(L, vi);
#else
lua_pop(L, (vi + 1));
#endif
return arr;
}
}
@ -882,6 +886,8 @@ namespace stack {
};
#if defined(SOL_CXX17_FEATURES) && SOL_CXX17_FEATURES
#if defined(SOL_STD_VARIANT) && SOL_STD_VARIANT
template <typename... Tn>
struct getter<std::variant<Tn...>> {

View File

@ -1630,8 +1630,6 @@ TEST_CASE("usertype/runtime-extensibility", "Check if usertypes are runtime exte
class derived_b : public base_a {
};
SECTION("just functions") {
sol::state lua;
lua.open_libraries(sol::lib::base);
@ -1742,8 +1740,12 @@ end
REQUIRE(pfr0.valid());
auto pfr1 = lua.safe_script("function B:c() print('B') return 2 end", sol::script_pass_on_error);
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());
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());
}
}

View File

@ -72,10 +72,14 @@ TEST_CASE("utility/variant", "test that variant can be round-tripped") {
return v == 2;
});
lua["v"] = std::variant<float, int, std::string>(2);
REQUIRE_NOTHROW([&]() {
lua.safe_script("assert(f(v))");
lua.safe_script("assert(g(v))");
}());
{
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;
@ -103,6 +107,53 @@ TEST_CASE("utility/variant", "test that variant can be round-tripped") {
#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") {
#ifdef SOL_CXX17_FEATURES
sol::state lua;