From f20980157933fa3cecc512bd4bf538d9233c7ddb Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 10:43:19 -0500 Subject: [PATCH] ws-cleanup-types: Types, api, host: honest annotations; return the full annotated value count --- src/Formats/Types.luau | 18 +++++------ src/Formats/archive/7z/init.luau | 12 +++---- src/Formats/audio/aiff/init.luau | 32 +++++++++---------- src/Formats/codecs/Jpeg/Decode.luau | 12 +++---- src/Formats/codecs/Lzma/init.luau | 4 +-- src/Formats/codecs/Raster.luau | 14 ++++---- src/Formats/codecs/Video/init.luau | 14 ++++---- src/Formats/codecs/Xml/Parse.luau | 14 ++++---- src/Formats/containers/Archive.luau | 6 ++-- src/Formats/containers/AudioWrap.luau | 4 +-- src/Formats/containers/Demux.luau | 2 +- src/Formats/containers/Ebml/init.luau | 18 +++++------ src/Formats/font/bmfont/init.luau | 2 +- src/Formats/font/woff/init.luau | 18 +++++------ src/Formats/host/Love.luau | 18 +++++------ src/Formats/host/init.luau | 8 ++--- src/Formats/image/tiff/init.luau | 10 +++--- src/Formats/init.luau | 30 ++++++++--------- src/Formats/ops/ImageOps/init.luau | 46 +++++++++++++-------------- src/Formats/vector/tiled/Data.luau | 6 ++-- src/Formats/vector/tiled/Tmj.luau | 4 +-- src/Formats/vector/tiled/Tmx.luau | 2 +- 22 files changed, 147 insertions(+), 147 deletions(-) diff --git a/src/Formats/Types.luau b/src/Formats/Types.luau index f34b569..effef54 100644 --- a/src/Formats/Types.luau +++ b/src/Formats/Types.luau @@ -672,7 +672,7 @@ local OBJECT = { __object = true } local objectMeta: { [any]: { any } } = setmetatable({}, { __mode = "k" }) :: any -- object -> its key order -- object(t?, keys?) -> t marked as an object, keys = its key order (default: none recorded; Types.set appends) function Types.object(t: { [any]: any }?, keys: { any }?): { [any]: any } - local o = t or {} + local o: { [any]: any } = t or {} if getmetatable(o) == nil then setmetatable(o, OBJECT) end @@ -746,13 +746,13 @@ local function p2(n: number): string return string.format("%02d", n) end local function fmtDate(d: any): string - local s = "" + local s: string = "" if d.year then s = string.format("%04d-%02d-%02d", d.year, d.month or 1, d.day or 1) end if d.hour then if s ~= "" then - s ..= "T" + s = s .. "T" end s ..= p2(d.hour) .. ":" .. p2(d.minute or 0) .. ":" .. p2(d.second or 0) local ns = d.nanosecond or 0 @@ -841,11 +841,11 @@ end -- "07:32:00.999", "1979-05-27 07:32:00Z", lower-case t / z accepted); out-of-range fields return nil function DateLib.parse(s: string): Date? local d: any = {} - local rest = s + local rest: string = s local y, mo, dd, r1 = string.match(rest, "^(%d%d%d%d)%-(%d%d)%-(%d%d)(.*)$") if y then d.year, d.month, d.day = tonumber(y), tonumber(mo), tonumber(dd) - rest = r1 + rest = r1 :: string if rest == "" then return if DateLib.valid(d) then DateLib.new(d) else nil end @@ -860,17 +860,17 @@ function DateLib.parse(s: string): Date? return nil end d.hour, d.minute = tonumber(h), tonumber(mi) - rest = r2 + rest = r2 :: string local sec, r3 = string.match(rest, "^:(%d%d)(.*)$") if not sec then return nil end d.second = tonumber(sec) - rest = r3 + rest = r3 :: string local frac, r4 = string.match(rest, "^%.(%d+)(.*)$") if frac then d.nanosecond = tonumber(string.sub(frac .. "000000000", 1, 9)) - rest = r4 + rest = r4 :: string else d.nanosecond = 0 end @@ -1082,6 +1082,6 @@ Types.CATEGORIES = table.freeze({ subtitle = "subtitles", music = "music", playlist = "playlist", -}) +} :: { [string]: string }) return Types diff --git a/src/Formats/archive/7z/init.luau b/src/Formats/archive/7z/init.luau index 39097fd..780f05c 100644 --- a/src/Formats/archive/7z/init.luau +++ b/src/Formats/archive/7z/init.luau @@ -169,7 +169,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf end local b = buffer.create(size) buffer.copy(b, 0, src, so, size) - return b + return b, nil elseif id == "\3\1\1" then if buffer.len(c.props) < 5 then return nil, Err.new(Err.CORRUPT, "bad LZMA properties") @@ -195,7 +195,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf if buffer.len(b) ~= size then return nil, Err.new(Err.CORRUPT, methodName(id) .. " stream decodes to the wrong size") end - return b + return b, nil elseif FILTERS[id] then if sl ~= size then return nil, Err.new(Err.CORRUPT, "filter size mismatch") @@ -206,7 +206,7 @@ local function decodeFolder(ctx: Ctx, s: Header.Streams, f: Header.Folder): (buf if not ok then return nil, e end - return b + return b, nil end return nil, Err.new(Err.UNSUPPORTED, methodName(id) .. " is not supported") end @@ -311,7 +311,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive? local function folderData(fi: number): (buffer?, Err.Err?) local c = cache[fi] if c then - return c + return c, nil end local f = s.folders[fi] local d, e = decodeFolder(ctx, s, f) @@ -322,7 +322,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive? return nil, Err.new(Err.CHECKSUM, "folder CRC-32 mismatch") end cache[fi] = d - return d + return d, nil end local si = 0 for _, file in parsed.files do @@ -376,7 +376,7 @@ function SevenZip.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Archive? if checkCrc and want and Crc32.raw(out, 0, size) ~= want then return nil, Err.new(Err.CHECKSUM, "CRC-32 mismatch in " .. file.name) end - return out + return out, nil end end Archive.put(a, e) diff --git a/src/Formats/audio/aiff/init.luau b/src/Formats/audio/aiff/init.luau index 94b02c2..dfd2a29 100644 --- a/src/Formats/audio/aiff/init.luau +++ b/src/Formats/audio/aiff/init.luau @@ -78,42 +78,42 @@ end local function sampleFormat(comp: string, bits: number): (string?, string?) if comp == "NONE" or comp == "twos" or comp == "" then if bits >= 1 and bits <= 8 then - return "s8" + return "s8", nil elseif bits <= 16 then - return "s16be" + return "s16be", nil elseif bits <= 24 then - return "s24be" + return "s24be", nil elseif bits <= 32 then - return "s32be" + return "s32be", nil end return nil, "bad sample size " .. bits elseif comp == "sowt" then if bits >= 1 and bits <= 8 then - return "s8" + return "s8", nil elseif bits <= 16 then - return "s16le" + return "s16le", nil elseif bits <= 24 then - return "s24le" + return "s24le", nil elseif bits <= 32 then - return "s32le" + return "s32le", nil end return nil, "bad sample size " .. bits elseif comp == "raw " then - return "u8" + return "u8", nil elseif comp == "in24" then - return "s24be" + return "s24be", nil elseif comp == "in32" then - return "s32be" + return "s32be", nil elseif comp == "42ni" then - return "s32le" + return "s32le", nil elseif comp == "fl32" or comp == "FL32" then - return "f32be" + return "f32be", nil elseif comp == "fl64" or comp == "FL64" then - return "f64be" + return "f64be", nil elseif comp == "ulaw" or comp == "ULAW" then - return "ulaw" + return "ulaw", nil elseif comp == "alaw" or comp == "ALAW" then - return "alaw" + return "alaw", nil end return nil, "compression " .. string.format("%q", comp) end diff --git a/src/Formats/codecs/Jpeg/Decode.luau b/src/Formats/codecs/Jpeg/Decode.luau index 05a0a7f..6cae5ff 100644 --- a/src/Formats/codecs/Jpeg/Decode.luau +++ b/src/Formats/codecs/Jpeg/Decode.luau @@ -429,7 +429,7 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly: frame = f st.frame = f if headerOnly then - return st + return st, nil, nil end elseif SOF_REFUSED[m] then return nil, "unsupported", SOF_REFUSED[m] .. " JPEG" @@ -559,12 +559,12 @@ function Decode.parse(b: buffer, base: number, n: number, opts: any, headerOnly: st.icc = icc end if headerOnly then - return st + return st, nil, nil end if scans == 0 then return nil, "truncated", "no image data" end - return st + return st, nil, nil end local SPACE_IDS = { gray = Color.GRAY, ycbcr = Color.YCC, rgb = Color.RGB, cmyk = Color.CMYK, ycck = Color.YCCK } @@ -697,7 +697,7 @@ end function Decode.outputYuv(st: State): (number?, number?, buffer?, string?) local f: Frame = st.frame if st.scale ~= 1 or f.precision ~= 8 then - return nil + return nil, nil, nil, nil end local comps = f.comps local space = colorSpaceOf(f, st.adobe, st.jfif) @@ -721,11 +721,11 @@ function Decode.outputYuv(st: State): (number?, number?, buffer?, string?) end end if not mode then - return nil + return nil, nil, nil, nil end for _, c in comps do if c.coef and not c.quant then - return nil -- output() reports the missing table + return nil, nil, nil, nil -- output() reports the missing table end end local w, h = f.width, f.height diff --git a/src/Formats/codecs/Lzma/init.luau b/src/Formats/codecs/Lzma/init.luau index f6f2825..cbb870a 100644 --- a/src/Formats/codecs/Lzma/init.luau +++ b/src/Formats/codecs/Lzma/init.luau @@ -319,7 +319,7 @@ function Lzma.filter(id: number, b: buffer, o: number, n: number, props: any?): return nil, Err.new(Err.CORRUPT, "bad delta filter properties") end Filters.delta(b, o, n, readu8(pb :: buffer, 0) + 1) - return true + return true, nil end local f = BCJ[id] if not f then @@ -332,7 +332,7 @@ function Lzma.filter(id: number, b: buffer, o: number, n: number, props: any?): return nil, Err.new(Err.CORRUPT, "bad BCJ filter properties") end f(b, o, n, start) - return true + return true, nil end -- ------------------------------------------------------------------------------------------------ xz diff --git a/src/Formats/codecs/Raster.luau b/src/Formats/codecs/Raster.luau index 953336e..4f06477 100644 --- a/src/Formats/codecs/Raster.luau +++ b/src/Formats/codecs/Raster.luau @@ -159,12 +159,12 @@ local function flatten(path: any, transform: Transform?, tolerance: number?, min local cmds = commands(path) local polys: { { number } } = {} if not cmds then - return polys + return polys, nil, nil, nil, nil end local m = transform or Raster.IDENTITY local a, b, c, d, e, f = m[1], m[2], m[3], m[4], m[5], m[6] if type(a) ~= "number" or type(b) ~= "number" or type(c) ~= "number" or type(d) ~= "number" or type(e) ~= "number" or type(f) ~= "number" then - return polys + return polys, nil, nil, nil, nil end local tol = tolerance or 0.1 if type(tol) ~= "number" or not (tol > 0.001) then @@ -271,7 +271,7 @@ local function flatten(path: any, transform: Transform?, tolerance: number?, min table.insert(polys, cur) end if #polys == 0 or bx0 ~= bx0 or by0 ~= by0 or bx1 ~= bx1 or by1 ~= by1 then - return polys + return polys, nil, nil, nil, nil end return polys, bx0, by0, bx1, by1 end @@ -411,7 +411,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?) local o: Options = opts or {} local polys, bx0, by0, bx1, by1 = Raster.flatten(path, o.transform, o.tolerance) if not bx0 then - return nil + return nil, nil end if abs(bx0 :: number) > LIMIT or abs(by0 :: number) > LIMIT or abs(bx1 :: number) > LIMIT or abs(by1 :: number) > LIMIT then return nil, "coordinates out of range" @@ -427,7 +427,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?) end local W, H = X1 - X0, Y1 - Y0 if W <= 0 or H <= 0 then - return nil + return nil, nil end local maxPixels = if type(o.maxPixels) == "number" then o.maxPixels else 2 ^ 26 if W * H > maxPixels then @@ -464,7 +464,7 @@ function Raster.mask(path: any, opts: Options?): (Mask?, string?) writeu8(cov, os + x, floor(v * 255 + 0.5)) end end - return { x = X0, y = Y0, width = W, height = H, coverage = cov } + return { x = X0, y = Y0, width = W, height = H, coverage = cov }, nil end -- ------------------------------------------------------------------------------------------------ filling @@ -533,7 +533,7 @@ function Raster.fill(img: any, path: any, paint: any, opts: Options?): (boolean, end end end - return true + return true, nil end -- ------------------------------------------------------------------------------------------------ stroking diff --git a/src/Formats/codecs/Video/init.luau b/src/Formats/codecs/Video/init.luau index d2d09d8..7c38620 100644 --- a/src/Formats/codecs/Video/init.luau +++ b/src/Formats/codecs/Video/init.luau @@ -51,7 +51,7 @@ function Video.simpleDecoder(decodeOne: (data: buffer, packet: Types.Packet) -> if q then return q[1], q[2], q[3] end - return nil + return nil, nil, nil end function self.flush(_: any) end function self.reset(_: any) @@ -155,15 +155,15 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec if pf == "yuv420p" and bits == 8 then local img = Types.Image.new(w, h, "yuv420p") buffer.copy(img.pixels, 0, data, 0, need) - return img + return img, nil elseif pf == "yv12" then local img = Types.Image.new(w, h, "yuv420p") buffer.copy(img.pixels, 0, data, 0, w * h) buffer.copy(img.pixels, w * h, data, w * h + cw * ch, cw * ch) buffer.copy(img.pixels, w * h + cw * ch, data, w * h, cw * ch) - return img + return img, nil end - return rawPlanar(data, pf, bits, w, h) + return rawPlanar(data, pf, bits, w, h), nil end local bpp = if pf == "bgra" or pf == "rgba" then 4 else 3 local stride = if track.stride then track.stride else w * bpp @@ -185,7 +185,7 @@ local function rawDecoder(track: Types.Track, _opts: any?): (Types.VideoCodecDec buffer.writeu32(px, o + x * 4, a + b * 256 + c * 65536 + 0xff000000) end end - return img + return img, nil end) end @@ -269,7 +269,7 @@ local function mpeg1Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecD if q then return q[1], q[2], q[3] end - return nil + return nil, nil, nil end function self.flush(_: any) for _ = 1, 4 do @@ -324,7 +324,7 @@ function Video.register(entry: Entry): (boolean, Err.Err?) for _, m in entry.matroska or {} do byMatroska[m] = entry.id end - return true + return true, nil end for _, e in BUILTIN do Video.register(e) diff --git a/src/Formats/codecs/Xml/Parse.luau b/src/Formats/codecs/Xml/Parse.luau index 4a04ff3..1cff3bc 100644 --- a/src/Formats/codecs/Xml/Parse.luau +++ b/src/Formats/codecs/Xml/Parse.luau @@ -291,33 +291,33 @@ function Parse.events(s: string, opts: Options?): () -> (string?, any, any, any) if kw == "SYSTEM" then local q = match(s, "^[ \t\n]+()", p + 6) :: any if not q then - return nil + return nil, nil, nil end local sys, e = literal(q) if not sys then - return nil + return nil, nil, nil end return "", sys, e elseif kw == "PUBLIC" then local q = match(s, "^[ \t\n]+()", p + 6) :: any if not q then - return nil + return nil, nil, nil end local pub, e = literal(q) if not pub or find(pub, "[^ \r\n%w%-'()+,./:=?;!*#@$_%%]") then - return nil + return nil, nil, nil end local r = match(s, "^[ \t\n]+()", e) :: any if not r then - return nil + return nil, nil, nil end local sys, e2 = literal(r) if not sys then - return nil + return nil, nil, nil end return pub, sys, e2 end - return nil + return nil, nil, nil end -- check an entity value's references and expand its character references (the rest expand when used) local function entityValue(v: string): string? diff --git a/src/Formats/containers/Archive.luau b/src/Formats/containers/Archive.luau index 1cdd84c..ead480e 100644 --- a/src/Formats/containers/Archive.luau +++ b/src/Formats/containers/Archive.luau @@ -59,7 +59,7 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?) return nil, Err.new(Err.ARG, "no entry named " .. tostring(name)) end if e.data then - return e.data + return e.data, nil end if e.load then local ok, b, err = pcall(e.load) @@ -69,9 +69,9 @@ function Methods.read(self: any, name: string): (buffer?, Err.Err?) if not b then return nil, err or Err.new(Err.CORRUPT, "entry " .. name .. ": decode failed") end - return b + return b, nil end - return buffer.create(0) + return buffer.create(0), nil end -- add(name, data, opts?) -> (): opts.mtime (unix seconds), opts.dir, opts.mode; replaces an entry of the same name diff --git a/src/Formats/containers/AudioWrap.luau b/src/Formats/containers/AudioWrap.luau index a2fa1d4..fa82ebe 100644 --- a/src/Formats/containers/AudioWrap.luau +++ b/src/Formats/containers/AudioWrap.luau @@ -242,7 +242,7 @@ end local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean }?) if buffer.len(id) < 30 or buffer.len(setup) < 8 then - return nil + return nil, nil end local bs = buffer.readu8(id, 28) local sizes = { 2 ^ (bs % 16), 2 ^ (bs // 16) } @@ -278,7 +278,7 @@ local function vorbisModes(id: buffer, setup: buffer): ({ number }?, { boolean } return sizes, out end end - return nil + return nil, nil end -- ------------------------------------------------------------------------------------------------ ADTS diff --git a/src/Formats/containers/Demux.luau b/src/Formats/containers/Demux.luau index 1ceec61..953550b 100644 --- a/src/Formats/containers/Demux.luau +++ b/src/Formats/containers/Demux.luau @@ -239,7 +239,7 @@ function D.info(self: Demux, track: number, k: number): (number?, number?, numbe local list = self.byTrack[track] local i = list and list[k] if not i then - return nil + return nil, nil, nil, nil, nil end return self.pts[i], self.dts[i], self.dur[i], self.key[i], self.len[i] end diff --git a/src/Formats/containers/Ebml/init.luau b/src/Formats/containers/Ebml/init.luau index 2efc113..bfd1ce4 100644 --- a/src/Formats/containers/Ebml/init.luau +++ b/src/Formats/containers/Ebml/init.luau @@ -21,11 +21,11 @@ Ebml.UNKNOWN = -1 -- vint(buf, pos, limit) -> value (marker removed), length | nil. All-ones values (unknown sizes) return -1. function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?) if pos >= limit then - return nil + return nil, nil end local b = readu8(buf, pos) if b == 0 then - return nil + return nil, nil end local len = 1 local mask = 0x80 @@ -34,7 +34,7 @@ function Ebml.vint(buf: buffer, pos: number, limit: number): (number?, number?) mask = mask // 2 end if pos + len > limit then - return nil + return nil, nil end local v = bit32.band(b, mask - 1) local allOnes = v == mask - 1 @@ -55,10 +55,10 @@ end function Ebml.svint(buf: buffer, pos: number, limit: number): (number?, number?) local v, len = Ebml.vint(buf, pos, limit) if v == nil or len == nil then - return nil + return nil, nil end if v == -1 then - return nil + return nil, nil end return v - (2 ^ (7 * len - 1) - 1), len end @@ -66,12 +66,12 @@ end -- id(buf, pos, limit) -> the element ID with its marker bits, length | nil (IDs are 1..4 bytes) function Ebml.id(buf: buffer, pos: number, limit: number): (number?, number?) if pos >= limit then - return nil + return nil, nil end local b = readu8(buf, pos) local len = if b >= 0x80 then 1 elseif b >= 0x40 then 2 elseif b >= 0x20 then 3 elseif b >= 0x10 then 4 else 0 if len == 0 or pos + len > limit then - return nil + return nil, nil end local v = b for i = 1, len - 1 do @@ -84,11 +84,11 @@ end function Ebml.header(buf: buffer, pos: number, limit: number): (number?, number?, number?) local id, il = Ebml.id(buf, pos, limit) if not id or not il then - return nil + return nil, nil, nil end local size, sl = Ebml.vint(buf, pos + il, limit) if not size or not sl then - return nil + return nil, nil, nil end return id, size, pos + il + sl end diff --git a/src/Formats/font/bmfont/init.luau b/src/Formats/font/bmfont/init.luau index e40aa7e..f5459ae 100644 --- a/src/Formats/font/bmfont/init.luau +++ b/src/Formats/font/bmfont/init.luau @@ -93,7 +93,7 @@ function Face.loadPages(self: any, loader: any?): (boolean?, Err.Err?) pages[i] = img end self.pages = pages - return true + return true, nil end local CHANNEL = { [1] = 2, [2] = 1, [4] = 0, [8] = 3 } -- chnl bit -> byte in rgba8 diff --git a/src/Formats/font/woff/init.luau b/src/Formats/font/woff/init.luau index 6b1bb91..84d8ad9 100644 --- a/src/Formats/font/woff/init.luau +++ b/src/Formats/font/woff/init.luau @@ -34,7 +34,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s local maxBytes = (opts and opts.maxBytes) or 2 ^ 28 local base = r.base if not r:match("wOFF") then - return nil, Err.new(Err.CORRUPT, "bad signature", base) + return nil, Err.new(Err.CORRUPT, "bad signature", base), nil end local flavor = r:string(4) local length = r:u32be() @@ -45,16 +45,16 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s local metaOffset, metaLength, metaOrig = r:u32be(), r:u32be(), r:u32be() r:skip(8) if r.err then - return nil, r.err + return nil, r.err, nil end if flavor == "ttcf" then - return nil, Err.new(Err.UNSUPPORTED, "font collections in WOFF", base + 4) + return nil, Err.new(Err.UNSUPPORTED, "font collections in WOFF", base + 4), nil end if length > r:size() then - return nil, Err.new(Err.TRUNCATED, string.format("file is %d bytes, header says %d", r:size(), length), base + 8) + return nil, Err.new(Err.TRUNCATED, string.format("file is %d bytes, header says %d", r:size(), length), base + 8), nil end if numTables == 0 or total > maxBytes or total < 12 + 16 * numTables then - return nil, Err.new(if total > maxBytes then Err.LIMIT else Err.CORRUPT, "bad table count or sfnt size", base + 12) + return nil, Err.new(if total > maxBytes then Err.LIMIT else Err.CORRUPT, "bad table count or sfnt size", base + 12), nil end local dir = {} for i = 1, numTables do @@ -62,7 +62,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s dir[i] = e end if r.err then - return nil, r.err + return nil, r.err, nil end local out = buffer.create(total) local w = Bytes.writer(12) @@ -80,10 +80,10 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s local p = 12 + 16 * numTables for i, e in dir do if e.offset + e.comp > r:size() or e.comp > e.orig then - return nil, Err.new(Err.CORRUPT, "table " .. e.tag .. " out of range", base + 44 + (i - 1) * 20) + return nil, Err.new(Err.CORRUPT, "table " .. e.tag .. " out of range", base + 44 + (i - 1) * 20), nil end if p + e.orig > total then - return nil, Err.new(Err.CORRUPT, "tables larger than totalSfntSize", base + 16) + return nil, Err.new(Err.CORRUPT, "tables larger than totalSfntSize", base + 16), nil end local d = 12 + (i - 1) * 16 buffer.writestring(out, d, e.tag) @@ -93,7 +93,7 @@ function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, s if e.comp < e.orig then local t, err = Deflate.zlib(r.buf, base + e.offset, e.comp, e.orig) if not t then - return nil, Err.new((err :: any).code, "table " .. e.tag .. ": " .. (err :: any).message, base + e.offset) + return nil, Err.new((err :: any).code, "table " .. e.tag .. ": " .. (err :: any).message, base + e.offset), nil end buffer.copy(out, p, t, 0, e.orig) else diff --git a/src/Formats/host/Love.luau b/src/Formats/host/Love.luau index 0ae01dc..7ee8bc0 100644 --- a/src/Formats/host/Love.luau +++ b/src/Formats/host/Love.luau @@ -31,7 +31,7 @@ function Host.readFile(path: string): (buffer?, Err.Err?) if type(s) ~= "string" then return nil, Err.new(Err.IO, "cannot read " .. path .. ": " .. tostring(msg)) end - return buffer.fromstring(s) + return buffer.fromstring(s), nil end local f, msg = io().open(path, "rb") if not f then @@ -39,7 +39,7 @@ function Host.readFile(path: string): (buffer?, Err.Err?) end local s = f:read("*a") f:close() - return buffer.fromstring(s) + return buffer.fromstring(s), nil end -- writeFile: relative paths go to the save directory (love.filesystem.write), absolute ones through io @@ -50,7 +50,7 @@ function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?) if not ok then return false, Err.new(Err.IO, "cannot write " .. path .. ": " .. tostring(msg)) end - return true + return true, nil end local f, msg = io().open(path, "wb") if not f then @@ -58,7 +58,7 @@ function Host.writeFile(path: string, data: buffer): (boolean, Err.Err?) end f:write(s) f:close() - return true + return true, nil end function Host.exists(path: string): boolean @@ -79,8 +79,8 @@ function Host.toImageData(img: any): (any, Err.Err?) return nil, Err.new(Err.ARG, "toImageData: needs rgba8 pixels, got " .. tostring(img.format)) end local n = img.width * img.height * 4 - local F = ffi() - local B = buffer :: any + local F: any = ffi() + local B: { toffi: ((buffer) -> any)? } = buffer :: any if F and B.toffi then local id = love().image.newImageData(img.width, img.height, "rgba8") F.copy(id:getFFIPointer(), B.toffi(img.pixels), n) @@ -94,8 +94,8 @@ end function Host.fromImageData(id: any): Types.Image local w, h = id:getDimensions() local img = Types.Image.new(w, h, "rgba8") - local F = ffi() - local B = buffer :: any + local F: any = ffi() + local B: { toffi: ((buffer) -> any)? } = buffer :: any if F and B.toffi and id:getFormat() == "rgba8" then F.copy(B.toffi(img.pixels), id:getFFIPointer(), w * h * 4) return img @@ -131,7 +131,7 @@ function Host.toSoundData(a: any): (any, Err.Err?) end local sd = love().sound.newSoundData(math.max(frames, 1), rate, 16, channels) local count = frames * channels - local F = ffi() + local F: any = ffi() if F then local p = F.cast("int16_t*", sd:getFFIPointer()) for i = 0, count - 1 do diff --git a/src/Formats/host/init.luau b/src/Formats/host/init.luau index 3852922..bbd09f3 100644 --- a/src/Formats/host/init.luau +++ b/src/Formats/host/init.luau @@ -31,13 +31,13 @@ end local host: Host if name == "roblox" then - host = require("@formats/host/Roblox") + host = require("@formats/host/Roblox") :: any elseif name == "love" then - host = require("@formats/host/Love") + host = require("@formats/host/Love") :: any elseif name == "luajit" then - host = require("@formats/host/LuaJIT") + host = require("@formats/host/LuaJIT") :: any else - host = require("@formats/host/Luau") + host = require("@formats/host/Luau") :: any end -- LuaJIT (plain or LÖVE): the big decoders (Opus CELT, H.264, ...) outgrow the default 1000 traces / 8 MB of machine -- code and fall back to the interpreter (CELT runs about 2.5x faster with these limits). Raising the limits only diff --git a/src/Formats/image/tiff/init.luau b/src/Formats/image/tiff/init.luau index 81dca4b..008fcff 100644 --- a/src/Formats/image/tiff/init.luau +++ b/src/Formats/image/tiff/init.luau @@ -208,7 +208,7 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b -- a chunk past the end: keep what exists (truncated files) len = math.max(0, math.min(len, c.limit - p)) if p < c.base or p >= c.limit then - return 0 + return 0, nil end end local src, sp = c.b, p @@ -221,10 +221,10 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b if comp == 1 then local n = math.min(len, size) buffer.copy(out, o, src, sp, n) - return n + return n, nil elseif comp == 5 then local n = Lzw.decode(src, sp, len, out, o, size, "tiff") - return n + return n, nil elseif comp == 8 or comp == 32946 then local raw = Deflate.zlib(src, sp, len, size) if not raw then @@ -235,9 +235,9 @@ local function inflateChunk(c: Ctx, comp: number, p: number, len: number, out: b end local n = math.min(buffer.len(raw :: buffer), size) buffer.copy(out, o, raw :: buffer, 0, n) - return n + return n, nil elseif comp == 32773 then - return Pixels.packbits(src, sp, len, out, o, size) + return Pixels.packbits(src, sp, len, out, o, size), nil end return nil, Err.new(Err.UNSUPPORTED, "compression " .. comp) end diff --git a/src/Formats/init.luau b/src/Formats/init.luau index 57d5dcc..d28ed76 100644 --- a/src/Formats/init.luau +++ b/src/Formats/init.luau @@ -95,7 +95,7 @@ local function detect(buf: buffer, name: string?, category: string?, format: str if not e then return nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. format .. (if category then " in " .. category else "")) end - return e + return e, nil end local ext = Input.extension(name) local best: Registry.Entry? = nil @@ -117,12 +117,12 @@ local function detect(buf: buffer, name: string?, category: string?, format: str end end if best then - return best + return best, nil end if ext then local e = find(ext, category) if e then - return e + return e, nil end end return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else "")) @@ -196,7 +196,7 @@ local function pageLoader(value: any, name: string?, o: Options) if type(value) ~= "table" or value.kind ~= "font" or type(value.meta) ~= "table" or type(value.meta.pageFiles) ~= "table" then return end - local user = o.loadPage + local user: any = o.loadPage if value.pages or (value.meta.pageLoader and type(user) ~= "function") then return end @@ -207,7 +207,7 @@ local function pageLoader(value: any, name: string?, o: Options) value.meta.pageLoader = function(file: string, page: number): (any, Err.Err?) local src: any if type(user) == "function" then - src = user(file, page) + src = (user :: any)(file, page) else src = (dir :: string) .. file end @@ -304,7 +304,7 @@ end -- load(src, opts?) -> value | nil, Err function F.load(src: any, opts: Options?): (any, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local buf, ierr, name = Input.resolve(src, o, host) if not buf then return nil, ierr @@ -356,7 +356,7 @@ load = F.load -- open(src, opts?) -> a streaming decoder (audio Decoder / VideoDecoder / Demuxer) | nil, Err function F.open(src: any, opts: Options?): (any, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local buf, ierr, name = Input.resolve(src, o, host) if not buf then return nil, ierr @@ -453,7 +453,7 @@ end -- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local entry, m, err, key = encoderFor(value, dest, o) if not entry then return nil, err @@ -513,12 +513,12 @@ function F.saveString(value: any, opts: Options?): (string?, Err.Err?) if not buf then return nil, err end - return buffer.tostring(buf) + return buffer.tostring(buf), nil end -- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save. function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local value, err = F.load(src, o) if value == nil then return nil, err @@ -531,7 +531,7 @@ end -- detect(src, opts?) -> format name | nil, Err function F.detect(src: any, opts: Options?): (string?, Err.Err?) - local o = opts or {} + local o: Options = opts or {} local buf, ierr, name = Input.resolve(src, o, host) if not buf then return nil, ierr @@ -540,7 +540,7 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?) if not entry then return nil, err end - return entry.name + return entry.name, nil end export type FormatInfo = { name: string, category: string, extensions: { string }, mime: { string }, caps: { [string]: any } } @@ -568,7 +568,7 @@ end -- ------------------------------------------------------------------------------------------------ category facades -- F.image.load / save / open / formats, F.audio..., F.model... : the same calls restricted to one category local function with(opts: Options?, category: string): Options - local o = if opts then table.clone(opts) else {} + local o: Options = if opts then table.clone(opts) else {} o.category = category return o end @@ -597,7 +597,7 @@ F.data.null = Types.null for _, fn in { "toBodies", "fromBodies", "tessellate" } do local name: string = fn F.cad[name] = function(value: any, opts: Options?): (any, any) - local o = if opts then table.clone(opts) else {} + local o: Options = if opts then table.clone(opts) else {} if o.plugins == nil then o.plugins = F.plugins end @@ -659,7 +659,7 @@ F.model.loadTextures = function(scene: any, opts: Options?): (any, { [number]: E if type(scene) ~= "table" or type(scene.textures) ~= "table" then return scene, errs end - local resolve = opts and opts.resolve + local resolve: ((any) -> any)? = opts and opts.resolve for i, tex in scene.textures do if type(tex) == "table" and tex.image == nil then local data = tex.data diff --git a/src/Formats/ops/ImageOps/init.luau b/src/Formats/ops/ImageOps/init.luau index 6a5e976..5c6abde 100644 --- a/src/Formats/ops/ImageOps/init.luau +++ b/src/Formats/ops/ImageOps/init.luau @@ -57,7 +57,7 @@ local function check(img: any): (boolean?, Err.Err?) if buffer.len(img.pixels) < need then return arg("pixel buffer too small for " .. img.width .. " x " .. img.height .. " " .. img.format) end - return true + return true, nil end ImageOps.check = check @@ -66,7 +66,7 @@ local function limit(w: number, h: number, opts: any): (boolean?, Err.Err?) if w * h > max then return nil, Err.new(Err.LIMIT, string.format("%d x %d is over the %d pixel limit", w, h, max)) end - return true + return true, nil end -- a new Image of the same kind with a copy of meta's plain fields (source, palette...) but no frames @@ -93,7 +93,7 @@ function ImageOps.new(w: number, h: number, format: string?): (Image?, Err.Err?) if not ok then return nil, e end - return Types.Image.new(w, h, fmt :: any) + return Types.Image.new(w, h, fmt :: any), nil end -- copy(img) -> a deep copy of the pixels (frames are shared) @@ -105,7 +105,7 @@ function ImageOps.copy(img: Image): (Image?, Err.Err?) local out = like(img, img.width, img.height) buffer.copy(out.pixels, 0, img.pixels, 0, buffer.len(out.pixels)) out.frames = img.frames - return out + return out, nil end -- ------------------------------------------------------------------------------------------------ convert @@ -127,7 +127,7 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?) local rgba = like(img, w, h, "rgba8") K.yuvToRgba(img.pixels, w, h, rgba.pixels) if format == "rgba8" then - return rgba + return rgba, nil end src = rgba end @@ -139,10 +139,10 @@ function ImageOps.convert(img: Image, format: string): (Image?, Err.Err?) K.convert(src.pixels, K.ID[src.format], rgba.pixels, K.ID.rgba8, w * h) end K.rgbaToYuv(rgba.pixels, w, h, out.pixels) - return out + return out, nil end K.convert(src.pixels, K.ID[src.format], out.pixels, K.ID[format], w * h) - return out + return out, nil end -- ops on yuv420p run on rgba8 and convert back @@ -179,7 +179,7 @@ function ImageOps.crop(img: Image, x: number, y: number, w: number, h: number): local bpp = Types.BPP[im.format] :: number local out = like(im, x1 - x0, y1 - y0) K.copyRect(out.pixels, out.width, im.pixels, im.width, x0, y0, 0, 0, out.width, out.height, bpp) - return out + return out, nil end) end @@ -195,7 +195,7 @@ function ImageOps.flip(img: Image, axis: string): (Image?, Err.Err?) return viaRgba(img, function(im: Image): (Image?, Err.Err?) local out = like(im, im.width, im.height) K.flip(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, axis == "h") - return out + return out, nil end) end @@ -216,7 +216,7 @@ function ImageOps.rotate90(img: Image, turns: number): (Image?, Err.Err?) local swap = t ~= 2 local out = like(im, if swap then im.height else im.width, if swap then im.width else im.height) K.rotate90(im.pixels, out.pixels, im.width, im.height, Types.BPP[im.format] :: number, t) - return out + return out, nil end) end @@ -245,7 +245,7 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts local out = like(im, w, h) if f == "nearest" then K.nearest(im.pixels, im.width, im.height, bpp, out.pixels, w, h) - return out + return out, nil end local kind, ch = K.KIND[fmt], K.CHANNELS[fmt] local src, sw, sh = im.pixels, im.width, im.height @@ -264,14 +264,14 @@ function ImageOps.resize(img: Image, w: number, h: number, filter: string?, opts K.resamplePass(src, sw, sh, ch, kind, out.pixels, h, K.coeffs(sh, h, f), false) elseif src == im.pixels or (w == im.width and alpha) then buffer.copy(out.pixels, 0, im.pixels, 0, w * h * bpp) - return out + return out, nil else out.pixels = src end if alpha then K.premultiply(out.pixels, w * h, alpha, kind, true) end - return out + return out, nil end) end @@ -333,7 +333,7 @@ function ImageOps.rotate(img: Image, degrees: number, opts: { expand: boolean?, if alpha then K.premultiply(out.pixels, nw * nh, alpha, kind, true) end - return out + return out, nil end) end @@ -371,7 +371,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO local w = math.min(src.width - sx, dst.width - dx) local h = math.min(src.height - sy, dst.height - dy) if w <= 0 or h <= 0 then - return dst + return dst, nil end if dst.format == "yuv420p" or (blend == "over" and not RGBA[dst.format]) then -- work on an rgba copy of the region, then write it back in dst's format @@ -387,7 +387,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO local back = ImageOps.convert(work, dst.format) :: Image K.copyRect(dst.pixels, dst.width, back.pixels, back.width, 0, 0, dx, dy, w, h, Types.BPP[dst.format] :: number) end - return dst + return dst, nil end local s = src if src.format ~= dst.format then @@ -400,7 +400,7 @@ function ImageOps.blit(dst: Image, src: Image, x: number, y: number, opts: BlitO else K.overFloat(dst.pixels, dst.width, s.pixels, s.width, sx, sy, dx, dy, w, h, opacity, K.KIND[dst.format]) end - return dst + return dst, nil end -- composite(dst, src, x?, y?, opts?) -> a new Image: a copy of dst with src blitted onto it @@ -423,7 +423,7 @@ local function premul(img: Image, un: boolean): (Image?, Err.Err?) if ch then K.premultiply(out.pixels, img.width * img.height, ch, K.KIND[img.format], un) end - return out + return out, nil end -- premultiply(img) / unpremultiply(img) -> a new Image (formats without alpha are copied unchanged) @@ -543,7 +543,7 @@ function ImageOps.setPixel(img: Image, x: number, y: number, r: number, g: numbe else wu8(px, i, q(al)) end - return true + return true, nil end -- fill(img, r, g, b, a?) -> img (in place) with every pixel set (values as setPixel) @@ -567,7 +567,7 @@ function ImageOps.fill(img: Image, r: number, g: number, b: number, a: number?): buffer.copy(img.pixels, filled, img.pixels, 0, c) filled += c end - return img + return img, nil end -- ------------------------------------------------------------------------------------------------ animation @@ -602,7 +602,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?) end local frames = img.frames if type(frames) ~= "table" or #frames == 0 then - return { img } + return { img }, nil end local fmt = if RGBA[img.format] then img.format else "rgba8" local canvas = Types.Image.new(img.width, img.height, fmt :: any) @@ -647,7 +647,7 @@ function ImageOps.coalesce(img: Image): ({ Image }?, Err.Err?) canvas.pixels = saved end end - return out + return out, nil end -- fromFrames(images, delays?) -> an animated Image: img.frames = the images as full-canvas frames (x = y = 0, @@ -683,7 +683,7 @@ function ImageOps.fromFrames(images: { Image }, delays: (number | { number })?): buffer.copy(out.pixels, 0, first.pixels, 0, buffer.len(out.pixels)) out.frames = frames :: any out.loops = 0 - return out + return out, nil end return ImageOps diff --git a/src/Formats/vector/tiled/Data.luau b/src/Formats/vector/tiled/Data.luau index 7338b74..927e7ea 100644 --- a/src/Formats/vector/tiled/Data.luau +++ b/src/Formats/vector/tiled/Data.luau @@ -23,19 +23,19 @@ end function Data.base64(text: string, compression: string?): ({ number }?, string?, boolean?) local raw, err = Base64.decode(text) if not raw then - return nil, err + return nil, err, nil end local bytes: buffer = raw if compression == "zlib" then local z, zerr = Deflate.zlib(bytes) if not z then - return nil, "zlib: " .. tostring(zerr) + return nil, "zlib: " .. tostring(zerr), nil end bytes = z elseif compression == "gzip" then local z, zerr = Deflate.gunzip(bytes) if not z then - return nil, "gzip: " .. tostring(zerr) + return nil, "gzip: " .. tostring(zerr), nil end bytes = z elseif compression ~= nil and compression ~= "" then diff --git a/src/Formats/vector/tiled/Tmj.luau b/src/Formats/vector/tiled/Tmj.luau index ce23c8a..db8eab7 100644 --- a/src/Formats/vector/tiled/Tmj.luau +++ b/src/Formats/vector/tiled/Tmj.luau @@ -87,11 +87,11 @@ local function gids(v: any, encoding: string?, compression: string?): ({ number local out = {} for _, g in list(v) do if type(g) ~= "number" or g < 0 or g >= 4294967296 then - return nil, "bad gid" + return nil, "bad gid", nil end table.insert(out, g) end - return out, nil + return out, nil, nil end local function object(o: any): any diff --git a/src/Formats/vector/tiled/Tmx.luau b/src/Formats/vector/tiled/Tmx.luau index b415329..8d9dd72 100644 --- a/src/Formats/vector/tiled/Tmx.luau +++ b/src/Formats/vector/tiled/Tmx.luau @@ -79,7 +79,7 @@ local function gids(d: Node, encoding: string?, compression: string?): ({ number for _, t in Xml.children(d, "tile") do table.insert(out, num(t, "gid", 0) :: number) end - return out, nil + return out, nil, nil end return nil, "unknown encoding " .. encoding, true end