Add support for thread and userdata

This commit is contained in:
Emik 2026-03-09 11:33:20 +01:00
parent 5466b2dc35
commit 4bf3ef046e
Signed by: emik
GPG key ID: 6B0CD72A5E503BDF

View file

@ -103,32 +103,33 @@ end
--- Creates an enumeration. --- Creates an enumeration.
---@generic K, V ---@generic K, V
---@param tbl table<K, V> ---@param iter table<K, V>
---@param fpairs? fun(t: table<K, V>): (fun(table: table<K, V>, index?: K): K?, V?) ---@param fpairs? fun(t: table<K, V>): (fun(table: table<K, V>, index?: K): K?, V?)
---@param step? nil ---@param step? nil
---@return F | { [K]: V } ---@return F | { [K]: V }
---@overload fun(tbl: number, fpairs?: number, step?: number): F | { [number]: number } ---@overload fun(iter: number, fpairs?: number, step?: number): F | { [number]: number }
---@overload fun(tbl: string): (fun(table: { [string]: V }): V) ---@overload fun(iter: string): (fun(table: { [string]: V }): V)
---@overload fun(tbl: false): fun(): false ---@overload fun(iter: fun(): K, V): F | { [K]: V }
---@overload fun(tbl: true): fun(): true ---@overload fun(iter: false): fun(): false
---@overload fun(tbl: nil): F ---@overload fun(iter: true): fun(): true
function f.from(tbl, fpairs, step) ---@overload fun(iter: nil): F
if tbl == true then function f.from(iter, fpairs, step)
if iter == true then
return f.tru return f.tru
elseif tbl == false then elseif iter == false then
return f.fals return f.fals
elseif tbl == nil then elseif iter == nil then
return none return none
end end
local tbl_type = type(tbl) local t = type(iter)
if tbl_type == "string" then if t == "string" then
return f.index(tbl) return f.index(iter)
elseif tbl_type == "number" then elseif t == "number" then
local ik, is, start = 0, step or 1, fpairs and tbl or 1 local ik, is, start = 0, step or 1, fpairs and iter or 1
local stop = not fpairs and tbl or local stop = not fpairs and iter or
(type(fpairs) == "number") and fpairs or error("Invalid argument type for 'fpairs': " .. type(fpairs)) (type(fpairs) == "number") and fpairs or error("Invalid argument type for 'fpairs': " .. type(fpairs))
if start ~= stop and (is == 0 or ((is < 0) == (start < stop))) then if start ~= stop and (is == 0 or ((is < 0) == (start < stop))) then
@ -145,16 +146,24 @@ function f.from(tbl, fpairs, step)
return ik, iv return ik, iv
end end
end) end)
elseif tbl_type ~= "table" then elseif t == "function" then
error("Invalid argument type for 'tbl': " .. type(tbl)) return f.new(iter)
elseif t == "thread" then
return f.new(function()
local s, k, v = coroutine.resume(iter)
if s then
return k, v
end
end)
else
local next, context, k, v = autopairs(iter, fpairs)
return f.new(function()
k, v = next(context, k)
return k, v
end)
end end
local next, context, k, v = autopairs(tbl, fpairs)
return f.new(function()
k, v = next(context, k)
return k, v
end)
end end
---@generic K, V ---@generic K, V