mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
parent
3620c4b8e1
commit
fd58e7fcd1
|
@ -51,7 +51,7 @@ ReflowComments: true
|
||||||
|
|
||||||
# Macros
|
# Macros
|
||||||
AlignEscapedNewlines: Left
|
AlignEscapedNewlines: Left
|
||||||
IndentPPDirectives: None
|
#IndentPPDirectives: None
|
||||||
|
|
||||||
# Functions
|
# Functions
|
||||||
AllowShortFunctionsOnASingleLine: None
|
AllowShortFunctionsOnASingleLine: None
|
||||||
|
|
|
@ -122,7 +122,7 @@ namespace sol {
|
||||||
|
|
||||||
template <typename... Ret, typename... Args>
|
template <typename... Ret, typename... Args>
|
||||||
decltype(auto) call(Args&&... args) {
|
decltype(auto) call(Args&&... args) {
|
||||||
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 191326131 && _MSC_FULL_VER >= 191200000
|
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 191426428 && _MSC_FULL_VER >= 191200000
|
||||||
// MSVC is ass sometimes
|
// MSVC is ass sometimes
|
||||||
return get<protected_function>().call<Ret...>(std::forward<Args>(args)...);
|
return get<protected_function>().call<Ret...>(std::forward<Args>(args)...);
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -126,7 +126,7 @@ namespace sol {
|
||||||
|
|
||||||
template <typename... Ret, typename... Args>
|
template <typename... Ret, typename... Args>
|
||||||
decltype(auto) call(Args&&... args) {
|
decltype(auto) call(Args&&... args) {
|
||||||
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 191326131 && _MSC_FULL_VER >= 191200000
|
#if defined(_MSC_FULL_VER) && _MSC_FULL_VER <= 191426428 && _MSC_FULL_VER >= 191200000
|
||||||
// MSVC is ass sometimes
|
// MSVC is ass sometimes
|
||||||
return get<function>().call<Ret...>(std::forward<Args>(args)...);
|
return get<function>().call<Ret...>(std::forward<Args>(args)...);
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -199,6 +199,16 @@ namespace stack {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename C>
|
||||||
|
struct checker<detail::non_lua_nil_t, type::poly, C> {
|
||||||
|
template <typename Handler>
|
||||||
|
static bool check(lua_State* L, int index, Handler&& handler, record& tracking) {
|
||||||
|
checker<lua_nil_t, type::lua_nil, C> c{};
|
||||||
|
(void)c;
|
||||||
|
return !c.check(L, index, std::forward<Handler>(handler), tracking);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template <type expected, typename C>
|
template <type expected, typename C>
|
||||||
struct checker<nullopt_t, expected, C> : checker<lua_nil_t> {};
|
struct checker<nullopt_t, expected, C> : checker<lua_nil_t> {};
|
||||||
|
|
||||||
|
|
|
@ -450,7 +450,7 @@ namespace sol {
|
||||||
|
|
||||||
template <typename T, bool global = false, bool raw = false, typename = void>
|
template <typename T, bool global = false, bool raw = false, typename = void>
|
||||||
struct field_getter;
|
struct field_getter;
|
||||||
template <typename T, bool global = false, bool raw = false, typename = void>
|
template <typename T, typename P, bool global = false, bool raw = false, typename = void>
|
||||||
struct probe_field_getter;
|
struct probe_field_getter;
|
||||||
template <typename T, bool global = false, bool raw = false, typename = void>
|
template <typename T, bool global = false, bool raw = false, typename = void>
|
||||||
struct field_setter;
|
struct field_setter;
|
||||||
|
@ -831,24 +831,24 @@ namespace sol {
|
||||||
get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool global = false, bool raw = false, typename Key>
|
template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
|
||||||
probe probe_get_field(lua_State* L, Key&& key) {
|
probe probe_get_field(lua_State* L, Key&& key) {
|
||||||
return probe_field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key));
|
return probe_field_getter<meta::unqualified_t<Key>, C, global, raw>{}.get(L, std::forward<Key>(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool global = false, bool raw = false, typename Key>
|
template <bool global = false, bool raw = false, typename C = detail::non_lua_nil_t, typename Key>
|
||||||
probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
|
probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
|
||||||
return probe_field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
|
return probe_field_getter<meta::unqualified_t<Key>, C, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool global = false, typename Key>
|
template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
|
||||||
probe probe_raw_get_field(lua_State* L, Key&& key) {
|
probe probe_raw_get_field(lua_State* L, Key&& key) {
|
||||||
return probe_get_field<global, true>(L, std::forward<Key>(key));
|
return probe_get_field<global, true, C>(L, std::forward<Key>(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool global = false, typename Key>
|
template <bool global = false, typename C = detail::non_lua_nil_t, typename Key>
|
||||||
probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
||||||
return probe_get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
return probe_get_field<global, true, C>(L, std::forward<Key>(key), tableindex);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <bool global = false, bool raw = false, typename Key, typename Value>
|
template <bool global = false, bool raw = false, typename Key, typename Value>
|
||||||
|
|
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
namespace sol {
|
namespace sol {
|
||||||
namespace stack {
|
namespace stack {
|
||||||
template <typename T, bool b, bool raw, typename>
|
template <typename T, typename P, bool b, bool raw, typename>
|
||||||
struct probe_field_getter {
|
struct probe_field_getter {
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
probe get(lua_State* L, Key&& key, int tableindex = -2) {
|
probe get(lua_State* L, Key&& key, int tableindex = -2) {
|
||||||
|
@ -38,12 +38,12 @@ namespace stack {
|
||||||
return probe(false, 0);
|
return probe(false, 0);
|
||||||
}
|
}
|
||||||
get_field<b, raw>(L, std::forward<Key>(key), tableindex);
|
get_field<b, raw>(L, std::forward<Key>(key), tableindex);
|
||||||
return probe(!check<lua_nil_t>(L), 1);
|
return probe(check<P>(L), 1);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename A, typename B, bool b, bool raw, typename C>
|
template <typename A, typename B, typename P, bool b, bool raw, typename C>
|
||||||
struct probe_field_getter<std::pair<A, B>, b, raw, C> {
|
struct probe_field_getter<std::pair<A, B>, P, b, raw, C> {
|
||||||
template <typename Keys>
|
template <typename Keys>
|
||||||
probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
|
probe get(lua_State* L, Keys&& keys, int tableindex = -2) {
|
||||||
if (!b && !maybe_indexable(L, tableindex)) {
|
if (!b && !maybe_indexable(L, tableindex)) {
|
||||||
|
@ -54,16 +54,16 @@ namespace stack {
|
||||||
return probe(false, 1);
|
return probe(false, 1);
|
||||||
}
|
}
|
||||||
get_field<false, raw>(L, std::get<1>(keys), tableindex);
|
get_field<false, raw>(L, std::get<1>(keys), tableindex);
|
||||||
return probe(!check<lua_nil_t>(L), 2);
|
return probe(check<P>(L), 2);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename... Args, bool b, bool raw, typename C>
|
template <typename... Args, typename P, bool b, bool raw, typename C>
|
||||||
struct probe_field_getter<std::tuple<Args...>, b, raw, C> {
|
struct probe_field_getter<std::tuple<Args...>, P, b, raw, C> {
|
||||||
template <std::size_t I, typename Keys>
|
template <std::size_t I, typename Keys>
|
||||||
probe apply(std::index_sequence<I>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
|
probe apply(std::index_sequence<I>, int sofar, lua_State* L, Keys&& keys, int tableindex) {
|
||||||
get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
|
get_field < I<1 && b, raw>(L, std::get<I>(keys), tableindex);
|
||||||
return probe(!check<lua_nil_t>(L), sofar);
|
return probe(check<P>(L), sofar);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <std::size_t I, std::size_t I1, std::size_t... In, typename Keys>
|
template <std::size_t I, std::size_t I1, std::size_t... In, typename Keys>
|
||||||
|
|
|
@ -135,7 +135,7 @@ namespace sol {
|
||||||
template <bool global, bool raw, typename T, std::size_t I, typename Key>
|
template <bool global, bool raw, typename T, std::size_t I, typename Key>
|
||||||
decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key) const {
|
decltype(auto) traverse_get_deep_optional(int& popcount, Key&& key) const {
|
||||||
typedef decltype(stack::get<T>(base_t::lua_state())) R;
|
typedef decltype(stack::get<T>(base_t::lua_state())) R;
|
||||||
auto p = stack::probe_get_field<global, raw>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
|
auto p = stack::probe_get_field<global, raw, T>(base_t::lua_state(), std::forward<Key>(key), lua_gettop(base_t::lua_state()));
|
||||||
popcount += p.levels;
|
popcount += p.levels;
|
||||||
if (!p.success)
|
if (!p.success)
|
||||||
return R(nullopt);
|
return R(nullopt);
|
||||||
|
|
|
@ -108,10 +108,14 @@ namespace sol {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
typedef lua_nil_t nil_t;
|
typedef lua_nil_t nil_t;
|
||||||
#if !defined(SOL_NO_NIL)
|
#if !defined(SOL_NO_NIL) || (SOL_NO_NIL == 0)
|
||||||
const nil_t nil{};
|
const nil_t nil{};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
struct non_lua_nil_t {};
|
||||||
|
}
|
||||||
|
|
||||||
struct metatable_t {};
|
struct metatable_t {};
|
||||||
const metatable_t metatable_key = {};
|
const metatable_t metatable_key = {};
|
||||||
|
|
||||||
|
@ -862,6 +866,9 @@ namespace sol {
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> {};
|
struct lua_type_of<nullopt_t> : std::integral_constant<type, type::lua_nil> {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct lua_type_of<detail::non_lua_nil_t> : std::integral_constant<type, type::poly> {};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> {};
|
struct lua_type_of<std::nullptr_t> : std::integral_constant<type, type::lua_nil> {};
|
||||||
|
|
||||||
|
|
|
@ -48,3 +48,52 @@ TEST_CASE("proxy/function results", "make sure that function results return prop
|
||||||
REQUIRE(accum == 10);
|
REQUIRE(accum == 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("proxy/optional conversion", "make sure optional conversions out of a table work properly") {
|
||||||
|
sol::state state{};
|
||||||
|
sol::table table = state.create_table_with("func", 42);
|
||||||
|
sol::optional<sol::function> func = table["func"];
|
||||||
|
REQUIRE(func == sol::nullopt);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("proxy/proper-pushing", "allow proxies to reference other proxies and be serialized as the proxy itself and not a function or something") {
|
||||||
|
sol::state lua;
|
||||||
|
lua.open_libraries(sol::lib::base, sol::lib::io);
|
||||||
|
|
||||||
|
class T {};
|
||||||
|
lua.new_usertype<T>("T");
|
||||||
|
|
||||||
|
T t;
|
||||||
|
lua["t1"] = &t;
|
||||||
|
lua["t2"] = lua["t1"];
|
||||||
|
lua.safe_script("b = t1 == t2");
|
||||||
|
bool b = lua["b"];
|
||||||
|
REQUIRE(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("proxy/equality", "check to make sure equality tests work") {
|
||||||
|
sol::state lua;
|
||||||
|
#ifndef __clang__
|
||||||
|
REQUIRE((lua["a"] == sol::lua_nil));
|
||||||
|
REQUIRE((lua["a"] == nullptr));
|
||||||
|
REQUIRE_FALSE((lua["a"] != sol::lua_nil));
|
||||||
|
REQUIRE_FALSE((lua["a"] != nullptr));
|
||||||
|
REQUIRE_FALSE((lua["a"] == 0));
|
||||||
|
REQUIRE_FALSE((lua["a"] == 2));
|
||||||
|
REQUIRE((lua["a"] != 0));
|
||||||
|
REQUIRE((lua["a"] != 2));
|
||||||
|
#endif // clang screws up by trying to access int128 types that it doesn't support, even when we don't ask for them
|
||||||
|
|
||||||
|
lua["a"] = 2;
|
||||||
|
|
||||||
|
#ifndef __clang__
|
||||||
|
REQUIRE_FALSE((lua["a"] == sol::lua_nil));
|
||||||
|
REQUIRE_FALSE((lua["a"] == nullptr));
|
||||||
|
REQUIRE((lua["a"] != sol::lua_nil));
|
||||||
|
REQUIRE((lua["a"] != nullptr));
|
||||||
|
REQUIRE_FALSE((lua["a"] == 0));
|
||||||
|
REQUIRE((lua["a"] == 2));
|
||||||
|
REQUIRE((lua["a"] != 0));
|
||||||
|
REQUIRE_FALSE((lua["a"] != 2));
|
||||||
|
#endif // clang screws up by trying to access int128 types that it doesn't support, even when we don't ask for them
|
||||||
|
}
|
||||||
|
|
|
@ -21,8 +21,6 @@
|
||||||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#define CATCH_CONFIG_MAIN 1
|
|
||||||
|
|
||||||
#include "test_sol.hpp"
|
#include "test_sol.hpp"
|
||||||
|
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
@ -570,47 +568,6 @@ TEST_CASE("pusher/constness", "Make sure more types can handle being const and j
|
||||||
REQUIRE(x == 20);
|
REQUIRE(x == 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("proxy/proper-pushing", "allow proxies to reference other proxies and be serialized as the proxy itself and not a function or something") {
|
|
||||||
sol::state lua;
|
|
||||||
lua.open_libraries(sol::lib::base, sol::lib::io);
|
|
||||||
|
|
||||||
class T {};
|
|
||||||
lua.new_usertype<T>("T");
|
|
||||||
|
|
||||||
T t;
|
|
||||||
lua["t1"] = &t;
|
|
||||||
lua["t2"] = lua["t1"];
|
|
||||||
lua.safe_script("b = t1 == t2");
|
|
||||||
bool b = lua["b"];
|
|
||||||
REQUIRE(b);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("proxy/equality", "check to make sure equality tests work") {
|
|
||||||
sol::state lua;
|
|
||||||
#ifndef __clang__
|
|
||||||
REQUIRE((lua["a"] == sol::lua_nil));
|
|
||||||
REQUIRE_FALSE((lua["a"] == nullptr));
|
|
||||||
REQUIRE_FALSE((lua["a"] != sol::lua_nil));
|
|
||||||
REQUIRE((lua["a"] != nullptr));
|
|
||||||
REQUIRE_FALSE((lua["a"] == 0));
|
|
||||||
REQUIRE_FALSE((lua["a"] == 2));
|
|
||||||
REQUIRE((lua["a"] != 0));
|
|
||||||
REQUIRE((lua["a"] != 2));
|
|
||||||
lua["a"] = 2;
|
|
||||||
#endif // clang screws up by trying to access int128 types that it doesn't support, even when we don't ask for them
|
|
||||||
|
|
||||||
#ifndef __clang__
|
|
||||||
REQUIRE_FALSE((lua["a"] == sol::lua_nil));
|
|
||||||
REQUIRE_FALSE((lua["a"] == nullptr));
|
|
||||||
REQUIRE((lua["a"] != sol::lua_nil));
|
|
||||||
REQUIRE((lua["a"] != nullptr));
|
|
||||||
REQUIRE_FALSE((lua["a"] == 0));
|
|
||||||
REQUIRE((lua["a"] == 2));
|
|
||||||
REQUIRE((lua["a"] != 0));
|
|
||||||
REQUIRE_FALSE((lua["a"] != 2));
|
|
||||||
#endif // clang screws up by trying to access int128 types that it doesn't support, even when we don't ask for them
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("compilation/const regression", "make sure constness in tables is respected all the way down") {
|
TEST_CASE("compilation/const regression", "make sure constness in tables is respected all the way down") {
|
||||||
struct State {
|
struct State {
|
||||||
public:
|
public:
|
||||||
|
|
7
tests/tests_main.cpp
Normal file
7
tests/tests_main.cpp
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#define CATCH_CONFIG_RUNNER
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
int result = Catch::Session().run(argc, argv);
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user