mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-20 14:11:32 +00:00
[Lua] add __len metafunction for readonly objects
This commit is contained in:
parent
c5789b5543
commit
f86a45d60a
1 changed files with 4 additions and 0 deletions
|
@ -246,6 +246,9 @@ namespace LuaUtil
|
||||||
local nextFn, t, firstKey = ipairs(getmetatable(v).t)
|
local nextFn, t, firstKey = ipairs(getmetatable(v).t)
|
||||||
return function(_, k) return nextFn(t, k) end, v, firstKey
|
return function(_, k) return nextFn(t, k) end, v, firstKey
|
||||||
end
|
end
|
||||||
|
function lenForReadOnly(v)
|
||||||
|
return #getmetatable(v).t
|
||||||
|
end
|
||||||
local function nextForArray(array, index)
|
local function nextForArray(array, index)
|
||||||
index = (index or 0) + 1
|
index = (index or 0) + 1
|
||||||
if index <= #array then
|
if index <= #array then
|
||||||
|
@ -300,6 +303,7 @@ namespace LuaUtil
|
||||||
meta["__index"] = table;
|
meta["__index"] = table;
|
||||||
meta["__pairs"] = lua["pairsForReadOnly"];
|
meta["__pairs"] = lua["pairsForReadOnly"];
|
||||||
meta["__ipairs"] = lua["ipairsForReadOnly"];
|
meta["__ipairs"] = lua["ipairsForReadOnly"];
|
||||||
|
meta["__len"] = lua["lenForReadOnly"];
|
||||||
|
|
||||||
lua_newuserdata(luaState, 0);
|
lua_newuserdata(luaState, 0);
|
||||||
sol::stack::push(luaState, meta);
|
sol::stack::push(luaState, meta);
|
||||||
|
|
Loading…
Reference in a new issue