mirror of
https://github.com/OpenMW/openmw.git
synced 2025-03-03 03:39:42 +00:00
Merge branch 'lua_pairs' into 'master'
[Lua] pairs and ipairs for ObjectList (resolves #6732) Closes #6732 See merge request OpenMW/openmw!1833
This commit is contained in:
commit
9b0928e8f4
2 changed files with 11 additions and 24 deletions
|
@ -131,30 +131,8 @@ namespace MWLua
|
||||||
else
|
else
|
||||||
throw std::runtime_error("Index out of range");
|
throw std::runtime_error("Index out of range");
|
||||||
};
|
};
|
||||||
if constexpr (std::is_same_v<ObjectT, GObject>)
|
listT[sol::meta_function::pairs] = lua["ipairsForArray"].template get<sol::function>();
|
||||||
{
|
listT[sol::meta_function::ipairs] = lua["ipairsForArray"].template get<sol::function>();
|
||||||
// GObject and LObject iterators are in separate branches because if they share source code
|
|
||||||
// there is a collision in sol and only one iterator can be mapped to Lua.
|
|
||||||
auto iter = sol::make_object(lua, [registry](const GObjectList& l, int64_t i) -> sol::optional<std::tuple<int64_t, GObject>>
|
|
||||||
{
|
|
||||||
if (i >= 0 && i < static_cast<int64_t>(l.mIds->size()))
|
|
||||||
return std::make_tuple(i + 1, GObject((*l.mIds)[i], registry));
|
|
||||||
else
|
|
||||||
return sol::nullopt;
|
|
||||||
});
|
|
||||||
listT[sol::meta_function::ipairs] = [iter](const GObjectList& list) { return std::make_tuple(iter, list, 0); };
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto iter = sol::make_object(lua, [registry](const LObjectList& l, int64_t i) -> sol::optional<std::tuple<int64_t, LObject>>
|
|
||||||
{
|
|
||||||
if (i >= 0 && i < static_cast<int64_t>(l.mIds->size()))
|
|
||||||
return std::make_tuple(i + 1, LObject((*l.mIds)[i], registry));
|
|
||||||
else
|
|
||||||
return sol::nullopt;
|
|
||||||
});
|
|
||||||
listT[sol::meta_function::ipairs] = [iter](const LObjectList& list) { return std::make_tuple(iter, list, 0); };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ObjectT>
|
template <class ObjectT>
|
||||||
|
|
|
@ -92,6 +92,15 @@ namespace LuaUtil
|
||||||
local nextFn, t, firstKey = ipairs(getmetatable(v).__index)
|
local nextFn, t, firstKey = ipairs(getmetatable(v).__index)
|
||||||
return function(_, k) return nextFn(t, k) end, v, firstKey
|
return function(_, k) return nextFn(t, k) end, v, firstKey
|
||||||
end
|
end
|
||||||
|
local function nextForArray(array, index)
|
||||||
|
index = (index or 0) + 1
|
||||||
|
if index < #array then
|
||||||
|
return index, array[index]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
function ipairsForArray(array)
|
||||||
|
return nextForArray, array, 0
|
||||||
|
end
|
||||||
|
|
||||||
getmetatable('').__metatable = false
|
getmetatable('').__metatable = false
|
||||||
getSafeMetatable = function(v)
|
getSafeMetatable = function(v)
|
||||||
|
|
Loading…
Reference in a new issue