From 814562260d71814a0542c5fc0a53115ff5a0a264 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Sat, 8 Apr 2017 08:25:49 -0400 Subject: [PATCH] Fixes #394 --- single/sol/sol.hpp | 6 ++--- sol/container_usertype_metatable.hpp | 2 +- test_containers.cpp | 36 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index 053e2dde..6c46b574 100644 --- a/single/sol/sol.hpp +++ b/single/sol/sol.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 2017-04-07 01:10:42.173645 UTC -// This header was generated with sol v2.16.0 (revision e856abc) +// Generated 2017-04-08 12:25:20.970713 UTC +// This header was generated with sol v2.17.0 (revision 28fff10) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -11783,11 +11783,11 @@ namespace sol { #endif using std::begin; auto it = begin(src); + --k; if (k == src.size()) { real_add_call_push(std::integral_constant::value>(), L, src, 1); return 0; } - --k; std::advance(it, k); *it = stack::get(L, 3); return 0; diff --git a/sol/container_usertype_metatable.hpp b/sol/container_usertype_metatable.hpp index 3c4f29f7..80f4d8ce 100644 --- a/sol/container_usertype_metatable.hpp +++ b/sol/container_usertype_metatable.hpp @@ -239,11 +239,11 @@ namespace sol { #endif using std::begin; auto it = begin(src); + --k; if (k == src.size()) { real_add_call_push(std::integral_constant::value>(), L, src, 1); return 0; } - --k; std::advance(it, k); *it = stack::get(L, 3); return 0; diff --git a/test_containers.cpp b/test_containers.cpp index 05222c8b..6ba36641 100644 --- a/test_containers.cpp +++ b/test_containers.cpp @@ -522,3 +522,39 @@ end REQUIRE(ct.x == 20); } } + +TEST_CASE("containers/append-idiom", "ensure the append-idiom works as intended") { + sol::state lua; + lua.open_libraries(sol::lib::base); + lua.script( +R"( +function f_fill(vec) + print("#vec in lua: " .. #vec) + for k = 1, #vec do + vec[k] = k + end + print("#vec in lua: " .. #vec) +end +function f_append(vec) + print("#vec in lua: " .. #vec) + vec[#vec] = -10456407 + vec[#vec + 1] = -54 + print("#vec in lua: " .. #vec) +end +)"); + std::vector fill_cmp{ 1, 2, 3 }; + std::vector append_cmp{ -1, -1, -10456407, -54 }; + + std::vector vec1{ -1, -1, -1 }; + std::vector vec2{ -1, -1, -1 }; + + REQUIRE(vec1.size() == 3); + lua["f_fill"](vec1); + REQUIRE(vec1.size() == 3); + REQUIRE(vec1 == fill_cmp); + + REQUIRE(vec2.size() == 3); + lua["f_append"](vec2); + REQUIRE(vec2.size() == 4); + REQUIRE(vec2 == append_cmp); +}