Skip to content

Commit ab874eb

Browse files
committed
Add Nerixyz's test and fix for C++17
1 parent d426300 commit ab874eb

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

include/sol/usertype_storage.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,11 @@ namespace sol { namespace u_detail {
502502
stateless_reference* target = nullptr;
503503
{
504504
stack_reference k = stack::get<stack_reference>(L, 2);
505+
#if __cpp_lib_generic_unordered_lookup >= 201811L
505506
auto it = self.auxiliary_keys.find(k);
507+
#else
508+
auto it = self.auxiliary_keys.find(basic_reference(k));
509+
#endif
506510
if (it != self.auxiliary_keys.cend()) {
507511
target = &it->second;
508512
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# # # # sol2
2+
# The MIT License (MIT)
3+
#
4+
# Copyright (c) 2013-2022 Rapptz, ThePhD, and contributors
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
# this software and associated documentation files (the "Software"), to deal in
8+
# the Software without restriction, including without limitation the rights to
9+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
# the Software, and to permit persons to whom the Software is furnished to do so,
11+
# subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
23+
# # # # sol2 tests - simple regression tests
24+
25+
file(GLOB_RECURSE sources
26+
LIST_DIRECTORIES FALSE
27+
CONFIG_DEPENDS source/*.cpp)
28+
29+
sol2_create_basic_test(sol2.tests.regression_1716 sol2::sol2 ${sources})
30+
sol2_create_basic_test(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON sol2::sol2 ${sources})
31+
target_compile_options(sol2.tests.regression_1716
32+
PRIVATE
33+
${--allow-unreachable-code})
34+
target_compile_definitions(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON
35+
PRIVATE
36+
SOL_ALL_SAFETIES_ON=1)
37+
target_compile_options(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON
38+
PRIVATE
39+
${--allow-unreachable-code})
40+
if (SOL2_TESTS_SINGLE)
41+
sol2_create_basic_test(sol2.single.tests.regression_1716 sol2::sol2::single ${sources})
42+
target_compile_options(sol2.single.tests.regression_1716
43+
PRIVATE
44+
${--allow-unreachable-code})
45+
endif()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// sol2
2+
3+
// The MIT License (MIT)
4+
5+
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
6+
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
// this software and associated documentation files (the "Software"), to deal in
9+
// the Software without restriction, including without limitation the rights to
10+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
// the Software, and to permit persons to whom the Software is furnished to do so,
12+
// subject to the following conditions:
13+
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
#include <catch2/catch_all.hpp>
25+
26+
#include <sol/sol.hpp>
27+
28+
TEST_CASE("#1716 - index and newindex should not leak values into the registry", "[sol2][regression][issue1716]") {
29+
class vector {
30+
public:
31+
vector() = default;
32+
33+
static int my_index(vector&, int) {
34+
return 0;
35+
}
36+
37+
static void my_new_index(vector&, int, int) {
38+
return;
39+
}
40+
};
41+
42+
sol::state lua;
43+
lua.open_libraries(sol::lib::base);
44+
lua.new_usertype<vector>("vector",
45+
sol::constructors<sol::types<>>(), //
46+
sol::meta_function::index,
47+
&vector::my_index, //
48+
sol::meta_function::new_index,
49+
&vector::my_new_index);
50+
auto begin = lua.registry().size();
51+
lua.script(R"(
52+
local v = vector.new()
53+
local unused = 0
54+
for i=1,100 do
55+
unused = v[1]
56+
v[1] = 1
57+
end
58+
)");
59+
60+
REQUIRE(lua.registry().size() <= begin + 1);
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// sol2
2+
3+
// The MIT License (MIT)
4+
5+
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
6+
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
8+
// this software and associated documentation files (the "Software"), to deal in
9+
// the Software without restriction, including without limitation the rights to
10+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11+
// the Software, and to permit persons to whom the Software is furnished to do so,
12+
// subject to the following conditions:
13+
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19+
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21+
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23+
24+
#define CATCH_CONFIG_RUNNER
25+
26+
#include <catch2/catch_all.hpp>
27+
28+
int main(int argc, char* argv[]) {
29+
int result = Catch::Session().run(argc, argv);
30+
return result;
31+
}

tests/regression_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
# # # # sol2 tests
2424

2525
add_subdirectory(1011)
26+
add_subdirectory(1716)
2627
add_subdirectory(simple)

0 commit comments

Comments
 (0)