File: array_proxy_lifetime.cpp

package info (click to toggle)
sol2 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,096 kB
  • sloc: cpp: 43,816; ansic: 1,018; python: 356; sh: 288; makefile: 202
file content (185 lines) | stat: -rw-r--r-- 5,014 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// sol2

// The MIT License (MIT)

// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// 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.

#include <catch2/catch_all.hpp>

#include <sol/sol.hpp>
#include <vector>
#include <memory>
#include <iterator>

inline namespace sol2_regression_test_coroutines_array_proxy_lifetime {
	struct A {
	public:
		A(int value_) : value(value_) {
		}
		int value;

		int getValue() {
			return value;
		}
		std::vector<std::shared_ptr<A>> children;
	};

	struct ArrayProxy {
		using value_type = std::weak_ptr<A>;

		struct iterator {
			using iterator_category = std::random_access_iterator_tag;
			using difference_type = std::ptrdiff_t;
			using value_type = std::weak_ptr<A>;
			using pointer = std::weak_ptr<A>*;   // or also value_type*
			using reference = std::weak_ptr<A>&; // or also value_type&

			const ArrayProxy& a;
			size_t index;

			iterator(const ArrayProxy& a_, size_t index_) : a(a_), index(index_) {
			}

			value_type operator*() const {
				size_t size = a.mpParent.children.size();
				if (index < size) {
					return a.mpParent.children[index];
				}
				return std::weak_ptr<A>();
			}

			// Operators : arithmetic
			inline iterator& operator++() {
				++index;
				return *this;
			}
			inline iterator& operator--() {
				--index;
				return *this;
			}
			inline iterator& operator+=(const size_t& rhs) {
				index += rhs;
				return *this;
			}
			inline iterator& operator-=(const size_t& rhs) {
				index -= rhs;
				return *this;
			}

			// Operators : comparison
			inline bool operator==(const iterator& rhs) {
				return index == rhs.index;
			}
			inline bool operator!=(const iterator& rhs) {
				return index != rhs.index;
			}
			inline bool operator>(const iterator& rhs) {
				return index > rhs.index;
			}
			inline bool operator<(const iterator& rhs) {
				return index < rhs.index;
			}
			inline bool operator>=(const iterator& rhs) {
				return index >= rhs.index;
			}
			inline bool operator<=(const iterator& rhs) {
				return index <= rhs.index;
			}
		};

		ArrayProxy(const A& parent) : mpParent(parent) {
		}

		~ArrayProxy() {
		}

		auto begin() const {
			return iterator(*this, 0);
		}
		auto end() const {
			return iterator(*this, mpParent.children.size());
		}
		size_t size() const {
			return mpParent.children.size();
		}

		const A& mpParent;
	};
	static ArrayProxy getChildren(const A& a) {
		return ArrayProxy(a);
	}
} // namespace sol2_regression_test_coroutines_array_proxy_lifetime

namespace sol {
	template <typename T>
	struct unique_usertype_traits<std::weak_ptr<T>> {
		static T* get(lua_State*, const std::weak_ptr<T>& ptr) noexcept {
			return ptr.lock().get();
		}

		static bool is_null(lua_State*, const std::weak_ptr<T>& ptr) noexcept {
			return ptr.expired();
		}
	};

	template <>
	struct is_container<ArrayProxy> : std::true_type { };
} // namespace sol

TEST_CASE("test for issue #1400 - array proxy tests", "[sol2][regression][issue-1400]") {
	sol::state lua;
	lua.open_libraries(
	     sol::lib::base, sol::lib::package, sol::lib::table, sol::lib::debug, sol::lib::string, sol::lib::math, sol::lib::bit32, sol::lib::coroutine);

	const int outer = 10;
	const int inner = 100;
	A a(0);
	for (int i = 0; i < outer; i++) {
		auto child = std::make_shared<A>(i * outer);
		for (int j = 0; j < inner; j++) {
			auto child2 = std::make_shared<A>(i * outer + j);
			child->children.push_back(child2);
		}
		a.children.push_back(child);
	}

	lua.new_usertype<A>("A", "value", sol::property(&A::getValue), "children", sol::property(&getChildren));

	lua.globals()["A"] = &a;

	const auto& code = R"(
-- print(A.value)
for i=1, 10 do
	co = coroutine.create( function()
		-- print("A")
		for _, child in pairs(A.children) do
			for _, child2 in pairs(child.children) do
				-- print(child2.value)
			end
		end
		end)
	coroutine.resume(co)
end
    )";

	// call lua code directly
	sol::optional<sol::error> result = lua.safe_script(code);
	REQUIRE_FALSE(result.has_value());
}