Skip to content

Commit

Permalink
Conditionally define table.empty and table.getsize in Lua (#6537)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas authored Nov 16, 2024
1 parent 76393d3 commit fd11eae
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions lua/system/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,39 @@ function safecall(msg, fn, ...)
end
end

--- table.empty(t) returns true iff t has no keys/values.
---@param t table
---@return boolean
function table.empty(t)
if type(t) ~= 'table' then return true end
return next(t) == nil
end

--- Returns actual size of a table, including string keys
---@param t table
---@return number
function table.getsize(t)
if type(t) ~= 'table' then return 0 end
local size = 0
for k, v in t do
size = size + 1
if not rawget(table, 'empty') then
-- This function should be defined in the engine for performance.
-- See also:
-- - https://github.com/FAForever/FA-Binary-Patches/pull/98

--- table.empty(t) returns true iff t has no keys/values.
---@param t table
---@return boolean
function table.empty(t)
if type(t) ~= 'table' then return true end
return next(t) == nil
end
return size
end

-- replace with assembly implementations
table.empty = table.empty2 or table.empty
table.getsize = table.getsize2 or table.getsize
if not rawget(table, 'getsize') then
-- This function should be defined in the engine for performance.
-- See also:
-- - https://github.com/FAForever/FA-Binary-Patches/pull/98

--- Returns actual size of a table, including string keys
---@param t table
---@return number
function table.getsize(t)
if type(t) ~= 'table' then return 0 end
local size = 0
for k, v in t do
size = size + 1
end
return size
end

end

--- Returns a shallow copy of t
---@generic T
Expand Down

0 comments on commit fd11eae

Please sign in to comment.