From 736cf8d96d9a255bc177b848b47aac4ffe8e7dc5 Mon Sep 17 00:00:00 2001 From: mihacooper Date: Sun, 10 Sep 2017 22:24:35 +0300 Subject: [PATCH] Implement pairs and ipairs for Lua5.1/LuaJIT (#66) * impl global pairs and ipairs methods and update tests * fixes for review --- src/cpp/lua-module.cpp | 4 +++- src/cpp/shared-table.cpp | 8 ++++++++ src/cpp/shared-table.h | 3 ++- src/lua/effil.lua | 4 +++- tests/lua/metatable.lua | 14 ++++++++------ 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/cpp/lua-module.cpp b/src/cpp/lua-module.cpp index 9e5f5b1..a811bdd 100644 --- a/src/cpp/lua-module.cpp +++ b/src/cpp/lua-module.cpp @@ -68,7 +68,9 @@ extern "C" int luaopen_libeffil(lua_State* L) { "G", sol::make_object(lua, globalTable), "gc", GC::getLuaApi(lua), "channel", createChannel, - "userdata_type", userdataType + "userdata_type", userdataType, + "pairs", SharedTable::globalLuaPairs, + "ipairs", SharedTable::globalLuaIPairs ); sol::stack::push(lua, publicApi); return 1; diff --git a/src/cpp/shared-table.cpp b/src/cpp/shared-table.cpp index 7b9890e..42eb29d 100644 --- a/src/cpp/shared-table.cpp +++ b/src/cpp/shared-table.cpp @@ -298,6 +298,14 @@ size_t SharedTable::luaSize(SharedTable& stable) { return stable.data_->entries.size(); } +SharedTable::PairsIterator SharedTable::globalLuaPairs(sol::this_state state, SharedTable& obj) { + return obj.luaPairs(state); +} + +SharedTable::PairsIterator SharedTable::globalLuaIPairs(sol::this_state state, SharedTable& obj) { + return obj.luaIPairs(state); +} + #undef DEFFINE_METAMETHOD_CALL_0 #undef DEFFINE_METAMETHOD_CALL #undef PROXY_METAMETHOD_IMPL diff --git a/src/cpp/shared-table.h b/src/cpp/shared-table.h index ee8a276..51e8767 100644 --- a/src/cpp/shared-table.h +++ b/src/cpp/shared-table.h @@ -59,6 +59,8 @@ public: static sol::object luaRawGet(const SharedTable& stable, const sol::stack_object& key, sol::this_state state); static SharedTable luaRawSet(SharedTable& stable, const sol::stack_object& key, const sol::stack_object& value); static size_t luaSize(SharedTable& stable); + static PairsIterator globalLuaPairs(sol::this_state state, SharedTable& obj); + static PairsIterator globalLuaIPairs(sol::this_state state, SharedTable& obj); private: PairsIterator getNext(const sol::object& key, sol::this_state lua); @@ -75,5 +77,4 @@ private: std::shared_ptr data_; }; - } // effil diff --git a/src/lua/effil.lua b/src/lua/effil.lua index 583a499..0d466fe 100644 --- a/src/lua/effil.lua +++ b/src/lua/effil.lua @@ -23,7 +23,9 @@ local api = { getmetatable = capi.getmetatable, G = capi.G, gc = capi.gc, - channel = capi.channel + channel = capi.channel, + pairs = capi.pairs, + ipairs = capi.ipairs } api.type = function (something) diff --git a/tests/lua/metatable.lua b/tests/lua/metatable.lua index e500616..288c431 100644 --- a/tests/lua/metatable.lua +++ b/tests/lua/metatable.lua @@ -112,9 +112,7 @@ test_unary_op("len", function(a) return #a end) test.shared_table_with_metatable.tear_down = default_tear_down -if LUA_VERSION > 51 then - -test.shared_table_with_metatable.iterators = function (iterator_type) +test.shared_table_with_metatable.iterators = function (iterator_type, iterator_trigger) local share = effil.table() local iterator = iterator_type effil.setmetatable(share, { @@ -138,9 +136,10 @@ test.shared_table_with_metatable.iterators = function (iterator_type) for i = 1, 100 do share[math.random(1000) * 10 - 1] = math.random(1000) end + -- Check that *pairs iterator works local pow_iter = 1 - for k,v in _G[iterator](share) do + for k,v in _G[iterator_trigger][iterator](share) do test.equal(k, pow_iter) test.equal(v, share[pow_iter]) pow_iter = pow_iter * 2 @@ -148,9 +147,12 @@ test.shared_table_with_metatable.iterators = function (iterator_type) test.equal(pow_iter, 2 ^ 11) end -test.shared_table_with_metatable.iterators("pairs") -test.shared_table_with_metatable.iterators("ipairs") +test.shared_table_with_metatable.iterators("pairs", "effil") +test.shared_table_with_metatable.iterators("ipairs", "effil") +if LUA_VERSION > 51 then + test.shared_table_with_metatable.iterators("pairs", "_G") + test.shared_table_with_metatable.iterators("ipairs", "_G") end -- LUA_VERSION > 51 test.shared_table_with_metatable.as_shared_table = function()