From 54cffb7b6d78ed5a3239608180d64c6e2dd29dd8 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Wed, 24 Aug 2016 08:31:18 -0400 Subject: [PATCH] tables and userdata and pairs and ipairs, oh my --- sol/container_usertype_metatable.hpp | 1 - sol/state_view.hpp | 6 +++--- test_containers.cpp | 13 +++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sol/container_usertype_metatable.hpp b/sol/container_usertype_metatable.hpp index 8fceb051..8ac22d9f 100644 --- a/sol/container_usertype_metatable.hpp +++ b/sol/container_usertype_metatable.hpp @@ -295,7 +295,6 @@ namespace sol { iter& i = stack::get>(L, 1); auto& source = i.source; auto& it = i.it; - K k = stack::get(L, 2); std::advance(it, 1); if (it == end(source)) { return 0; diff --git a/sol/state_view.hpp b/sol/state_view.hpp index f073dff0..3212fe6b 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -238,17 +238,17 @@ namespace sol { load_result load(const std::string& code) { load_status x = static_cast(luaL_loadstring(L, code.c_str())); - return load_result(L, -1, 1, 1, x); + return load_result(L, lua_absindex(L, -1), 1, 1, x); } load_result load_file(const std::string& filename) { load_status x = static_cast(luaL_loadfile(L, filename.c_str())); - return load_result(L, -1, 1, 1, x); + return load_result(L, lua_absindex(L, -1), 1, 1, x); } load_result load_buffer(const char *buff, size_t size, const char *name, const char* mode = nullptr) { load_status x = static_cast(luaL_loadbufferx(L, buff, size, name, mode)); - return load_result(L, -1, 1, 1, x); + return load_result(L, lua_absindex(L, -1), 1, 1, x); } iterator begin() const { diff --git a/test_containers.cpp b/test_containers.cpp index ebc92863..7ef92a3d 100644 --- a/test_containers.cpp +++ b/test_containers.cpp @@ -129,20 +129,20 @@ TEST_CASE("containers/basic-serialization", "make sure containers are turned int lua.open_libraries(); lua.set("b", woof{ 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 }); REQUIRE_NOTHROW( - lua.script("for k, v in pairs(b) do assert(k == v) end"); + lua.script("for k = 1, #b do assert(k == b[k]) end"); ); woof w{ 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 }; lua.set("b", w); REQUIRE_NOTHROW( - lua.script("for k, v in pairs(b) do assert(k == v) end"); + lua.script("for k = 1, #b do assert(k == b[k]) end"); ); lua.set("b", &w); REQUIRE_NOTHROW( - lua.script("for k, v in pairs(b) do assert(k == v) end"); + lua.script("for k = 1, #b do assert(k == b[k]) end"); ); lua.set("b", std::ref(w)); REQUIRE_NOTHROW( - lua.script("for k, v in pairs(b) do assert(k == v) end"); + lua.script("for k = 1, #b do assert(k == b[k]) end"); ); } @@ -204,7 +204,8 @@ TEST_CASE("containers/const-correctness", "usertype metatable names should reaso lua.script(R"( func = function(vecs) - for i, vec in pairs(vecs) do + for i = 1, #vecs do + vec = vecs[i] print(i, ":", vec.x, vec.y, vec.z) end end @@ -212,7 +213,7 @@ end REQUIRE_NOTHROW({ lua["func"](foo); - lua["func"](bar); + lua["func"](bar); }); }