Implement pairs and ipairs for Lua5.1/LuaJIT (#66)
* impl global pairs and ipairs methods and update tests * fixes for review
This commit is contained in:
parent
d935a3bfb1
commit
736cf8d96d
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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<SharedData> data_;
|
||||
};
|
||||
|
||||
|
||||
} // effil
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user