diff --git a/sol/stack_check_get_qualified.hpp b/sol/stack_check_get_qualified.hpp index 06f2a4bc..164d5cd0 100644 --- a/sol/stack_check_get_qualified.hpp +++ b/sol/stack_check_get_qualified.hpp @@ -25,27 +25,12 @@ #define SOL_STACK_CHECK_QUALIFIED_GET_HPP #include "stack_core.hpp" -#include "stack_get.hpp" -#include "stack_check.hpp" -#include "optional.hpp" -#include -#include +#include "stack_check_get_unqualified.hpp" namespace sol { namespace stack { template - struct qualified_check_getter { - typedef decltype(stack_detail::unchecked_get(nullptr, 0, std::declval())) R; - - template - static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { - if (!check(L, index, std::forward(handler))) { - tracking.use(static_cast(!lua_isnone(L, index))); - return nullopt; - } - return stack_detail::unchecked_get(L, index, tracking); - } - }; + struct qualified_check_getter : check_getter, C> {}; } } // namespace sol::stack diff --git a/sol/stack_check_get_unqualified.hpp b/sol/stack_check_get_unqualified.hpp index de45076a..4bb3ed95 100644 --- a/sol/stack_check_get_unqualified.hpp +++ b/sol/stack_check_get_unqualified.hpp @@ -53,7 +53,7 @@ namespace stack { static optional get(lua_State* L, int index, Handler&& handler, record& tracking) { // actually check if it's none here, otherwise // we'll have a none object inside an optional! - bool success = stack::check(L, index, no_panic); + bool success = lua_isnoneornil(L, index) == 0 && stack::check(L, index, no_panic); if (!success) { // expected type, actual type tracking.use(static_cast(success)); @@ -64,14 +64,6 @@ namespace stack { } }; - template - struct check_getter> { - template - static decltype(auto) get(lua_State* L, int index, Handler&&, record& tracking) { - return check_get(L, index, no_panic, tracking); - } - }; - template struct check_getter::value && lua_type_of::value == type::number>> { template diff --git a/sol/stack_core.hpp b/sol/stack_core.hpp index f3a2b789..ea36cd38 100644 --- a/sol/stack_core.hpp +++ b/sol/stack_core.hpp @@ -767,7 +767,8 @@ namespace sol { template inline decltype(auto) unqualified_check_get(lua_State* L, int index, Handler&& handler, record& tracking) { - check_getter cg{}; + typedef meta::unqualified_t Tu; + check_getter cg{}; (void)cg; return cg.get(L, index, std::forward(handler), tracking); } diff --git a/sol/types.hpp b/sol/types.hpp index 2a7da05c..e06ffba3 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -1180,7 +1180,7 @@ namespace sol { struct is_environment : std::integral_constant::value || is_table::value> {}; template - struct is_automagical : std::true_type {}; + struct is_automagical : meta::neg>> {}; template inline type type_of() { diff --git a/sol/usertype_core.hpp b/sol/usertype_core.hpp index d084868c..c4e7751e 100644 --- a/sol/usertype_core.hpp +++ b/sol/usertype_core.hpp @@ -92,7 +92,7 @@ namespace sol { template int comparsion_operator_wrap(lua_State* L) { - auto maybel = stack::unqualified_check_get(L, 1); + auto maybel = stack::unqualified_check_get(L, 1); if (maybel) { auto mayber = stack::unqualified_check_get(L, 2); if (mayber) { diff --git a/tests/tests.cpp b/tests/tests.cpp index 4cd8801d..33f33df0 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -542,6 +542,27 @@ TEST_CASE("optional/left out args", "Make sure arguments can be left out of opti }()); } +TEST_CASE("optional/engaged versus unengaged", "solidify semantics for an engaged and unengaged optional") { + sol::state lua; + lua.open_libraries(sol::lib::base); + + lua.set_function("f", [](sol::optional optional_arg) { + if (optional_arg) { + return true; + } + return false; + }); + + auto valid0 = lua.safe_script("assert(not f())", sol::script_pass_on_error); + REQUIRE(valid0.valid()); + auto valid1 = lua.safe_script("assert(not f(nil))", sol::script_pass_on_error); + REQUIRE(valid1.valid()); + auto valid2 = lua.safe_script("assert(f(1))", sol::script_pass_on_error); + REQUIRE(valid2.valid()); + auto valid3 = lua.safe_script("assert(f('hi'))", sol::script_pass_on_error); + REQUIRE(valid3.valid()); +} + TEST_CASE("pusher/constness", "Make sure more types can handle being const and junk") { struct Foo { Foo(const sol::function& f)