From f86a45d60a4bc659aabedeae7bc9f698baf6d428 Mon Sep 17 00:00:00 2001 From: Petr Mikheev Date: Sun, 16 Jul 2023 06:17:04 +0200 Subject: [PATCH] [Lua] add __len metafunction for readonly objects --- components/lua/luastate.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/lua/luastate.cpp b/components/lua/luastate.cpp index 2a5769e6dd..2269a40c26 100644 --- a/components/lua/luastate.cpp +++ b/components/lua/luastate.cpp @@ -246,6 +246,9 @@ namespace LuaUtil local nextFn, t, firstKey = ipairs(getmetatable(v).t) return function(_, k) return nextFn(t, k) end, v, firstKey end + function lenForReadOnly(v) + return #getmetatable(v).t + end local function nextForArray(array, index) index = (index or 0) + 1 if index <= #array then @@ -300,6 +303,7 @@ namespace LuaUtil meta["__index"] = table; meta["__pairs"] = lua["pairsForReadOnly"]; meta["__ipairs"] = lua["ipairsForReadOnly"]; + meta["__len"] = lua["lenForReadOnly"]; lua_newuserdata(luaState, 0); sol::stack::push(luaState, meta);