mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
fix weird nested shit
This commit is contained in:
parent
d63ba49b0e
commit
ef1c0e7a38
|
@ -407,12 +407,13 @@ namespace sol {
|
|||
template <typename T>
|
||||
struct unqualified_pusher<nested<T>> {
|
||||
static int push(lua_State* L, const T& tablecont) {
|
||||
using inner_t = std::remove_pointer_t<meta::unwrap_unqualified_t<T>>;
|
||||
using Tu = meta::unwrap_unqualified_t<T>;
|
||||
using inner_t = std::remove_pointer_t<Tu>;
|
||||
if constexpr (is_container_v<inner_t>) {
|
||||
return stack::push<detail::as_table_tag<T>>(L, tablecont, nested_tag);
|
||||
}
|
||||
else {
|
||||
return stack::push<inner_t>(L, tablecont);
|
||||
return stack::push<Tu>(L, tablecont);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -38,6 +38,67 @@ namespace sol {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
class stateless_stack_reference {
|
||||
protected:
|
||||
int index = 0;
|
||||
|
||||
int registry_index() const noexcept {
|
||||
return LUA_NOREF;
|
||||
}
|
||||
|
||||
public:
|
||||
stateless_stack_reference() noexcept = default;
|
||||
stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference(){};
|
||||
stateless_stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(absolute_index(L, i)) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, absolute_index i) noexcept : stateless_stack_reference(i) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, raw_index i) noexcept : stateless_stack_reference(i) {
|
||||
}
|
||||
stateless_stack_reference(absolute_index i) noexcept : index(i) {
|
||||
}
|
||||
stateless_stack_reference(raw_index i) noexcept : index(i) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, ref_index) noexcept = delete;
|
||||
stateless_stack_reference(ref_index) noexcept = delete;
|
||||
stateless_stack_reference(const reference&) noexcept = delete;
|
||||
stateless_stack_reference(const stateless_stack_reference&) noexcept = default;
|
||||
stateless_stack_reference(stateless_stack_reference&& o) noexcept = default;
|
||||
stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default;
|
||||
stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default;
|
||||
|
||||
int push(lua_State* L) const noexcept {
|
||||
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
||||
luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value");
|
||||
#endif // make sure stack doesn't overflow
|
||||
lua_pushvalue(L, index);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void pop(lua_State* L, int n = 1) const noexcept {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
|
||||
int stack_index() const noexcept {
|
||||
return index;
|
||||
}
|
||||
|
||||
const void* pointer(lua_State* L) const noexcept {
|
||||
const void* vp = lua_topointer(L, stack_index());
|
||||
return vp;
|
||||
}
|
||||
|
||||
type get_type(lua_State* L) const noexcept {
|
||||
int result = lua_type(L, index);
|
||||
return static_cast<type>(result);
|
||||
}
|
||||
|
||||
bool valid(lua_State* L) const noexcept {
|
||||
type t = get_type(L);
|
||||
return t != type::lua_nil && t != type::none;
|
||||
}
|
||||
};
|
||||
|
||||
class stack_reference {
|
||||
private:
|
||||
lua_State* luastate = nullptr;
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2019-06-04 18:14:05.186483 UTC
|
||||
// This header was generated with sol v3.0.2 (revision cbb0575)
|
||||
// Generated 2019-06-09 01:46:16.048592 UTC
|
||||
// This header was generated with sol v3.0.2 (revision d63ba49)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
|
||||
|
|
|
@ -20,8 +20,8 @@
|
|||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
// This file was generated with a script.
|
||||
// Generated 2019-06-04 18:14:04.496190 UTC
|
||||
// This header was generated with sol v3.0.2 (revision cbb0575)
|
||||
// Generated 2019-06-09 01:46:15.617622 UTC
|
||||
// This header was generated with sol v3.0.2 (revision d63ba49)
|
||||
// https://github.com/ThePhD/sol2
|
||||
|
||||
#ifndef SOL_SINGLE_INCLUDE_HPP
|
||||
|
@ -8366,6 +8366,67 @@ namespace sol {
|
|||
}
|
||||
} // namespace detail
|
||||
|
||||
class stateless_stack_reference {
|
||||
protected:
|
||||
int index = 0;
|
||||
|
||||
int registry_index() const noexcept {
|
||||
return LUA_NOREF;
|
||||
}
|
||||
|
||||
public:
|
||||
stateless_stack_reference() noexcept = default;
|
||||
stateless_stack_reference(lua_nil_t) noexcept : stateless_stack_reference(){};
|
||||
stateless_stack_reference(lua_State* L, int i) noexcept : stateless_stack_reference(absolute_index(L, i)) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, absolute_index i) noexcept : stateless_stack_reference(i) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, raw_index i) noexcept : stateless_stack_reference(i) {
|
||||
}
|
||||
stateless_stack_reference(absolute_index i) noexcept : index(i) {
|
||||
}
|
||||
stateless_stack_reference(raw_index i) noexcept : index(i) {
|
||||
}
|
||||
stateless_stack_reference(lua_State*, ref_index) noexcept = delete;
|
||||
stateless_stack_reference(ref_index) noexcept = delete;
|
||||
stateless_stack_reference(const reference&) noexcept = delete;
|
||||
stateless_stack_reference(const stateless_stack_reference&) noexcept = default;
|
||||
stateless_stack_reference(stateless_stack_reference&& o) noexcept = default;
|
||||
stateless_stack_reference& operator=(stateless_stack_reference&&) noexcept = default;
|
||||
stateless_stack_reference& operator=(const stateless_stack_reference&) noexcept = default;
|
||||
|
||||
int push(lua_State* L) const noexcept {
|
||||
#if defined(SOL_SAFE_STACK_CHECK) && SOL_SAFE_STACK_CHECK
|
||||
luaL_checkstack(L, 1, "not enough Lua stack space to push a single reference value");
|
||||
#endif // make sure stack doesn't overflow
|
||||
lua_pushvalue(L, index);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void pop(lua_State* L, int n = 1) const noexcept {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
|
||||
int stack_index() const noexcept {
|
||||
return index;
|
||||
}
|
||||
|
||||
const void* pointer(lua_State* L) const noexcept {
|
||||
const void* vp = lua_topointer(L, stack_index());
|
||||
return vp;
|
||||
}
|
||||
|
||||
type get_type(lua_State* L) const noexcept {
|
||||
int result = lua_type(L, index);
|
||||
return static_cast<type>(result);
|
||||
}
|
||||
|
||||
bool valid(lua_State* L) const noexcept {
|
||||
type t = get_type(L);
|
||||
return t != type::lua_nil && t != type::none;
|
||||
}
|
||||
};
|
||||
|
||||
class stack_reference {
|
||||
private:
|
||||
lua_State* luastate = nullptr;
|
||||
|
@ -13155,12 +13216,13 @@ namespace sol {
|
|||
template <typename T>
|
||||
struct unqualified_pusher<nested<T>> {
|
||||
static int push(lua_State* L, const T& tablecont) {
|
||||
using inner_t = std::remove_pointer_t<meta::unwrap_unqualified_t<T>>;
|
||||
using Tu = meta::unwrap_unqualified_t<T>;
|
||||
using inner_t = std::remove_pointer_t<Tu>;
|
||||
if constexpr (is_container_v<inner_t>) {
|
||||
return stack::push<detail::as_table_tag<T>>(L, tablecont, nested_tag);
|
||||
}
|
||||
else {
|
||||
return stack::push<inner_t>(L, tablecont);
|
||||
return stack::push<Tu>(L, tablecont);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -220,3 +220,52 @@ end
|
|||
table_check_unordered_values(src, t1ummap.value());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("containers/as_table with pointers", "test to make sure pointers are respected in as_table work") {
|
||||
using EHandle = std::uint32_t;
|
||||
|
||||
struct Entity {
|
||||
public:
|
||||
Entity(EHandle handle) : handle_(handle) {}
|
||||
Entity(const Entity&) = default;
|
||||
Entity(Entity&&) = default;
|
||||
Entity& operator=(const Entity&) = default;
|
||||
Entity& operator=(Entity&&) = default;
|
||||
|
||||
private:
|
||||
EHandle handle_;
|
||||
};
|
||||
|
||||
auto test_func_vec = []() -> std::vector<Entity*> {
|
||||
return { reinterpret_cast<Entity*>(0x01), reinterpret_cast<Entity*>(0x02), reinterpret_cast<Entity*>(0x03) };
|
||||
};
|
||||
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base);
|
||||
lua.new_usertype<Entity>("Entity");
|
||||
|
||||
lua["f"] = [&test_func_vec]() { return sol::as_table(test_func_vec()); };
|
||||
|
||||
lua["g"] = [&test_func_vec]() { return sol::as_nested(test_func_vec()); };
|
||||
|
||||
sol::optional<sol::error> maybe_err0 = lua.safe_script("t = f()");
|
||||
sol::optional<sol::error> maybe_err1 = lua.safe_script("u = g()");
|
||||
REQUIRE_FALSE(maybe_err0.has_value());
|
||||
REQUIRE_FALSE(maybe_err1.has_value());
|
||||
|
||||
sol::table t = lua["t"];
|
||||
Entity* e1 = t[1];
|
||||
Entity* e2 = t[2];
|
||||
Entity* e3 = t[3];
|
||||
REQUIRE(e1 == reinterpret_cast<Entity*>(0x01));
|
||||
REQUIRE(e2 == reinterpret_cast<Entity*>(0x02));
|
||||
REQUIRE(e3 == reinterpret_cast<Entity*>(0x03));
|
||||
|
||||
sol::table u = lua["u"];
|
||||
Entity* f1 = u[1];
|
||||
Entity* f2 = u[2];
|
||||
Entity* f3 = u[3];
|
||||
REQUIRE(f1 == reinterpret_cast<Entity*>(0x01));
|
||||
REQUIRE(f2 == reinterpret_cast<Entity*>(0x02));
|
||||
REQUIRE(f3 == reinterpret_cast<Entity*>(0x03));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user