ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py

- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
This commit is contained in:
4DBug committed 2026-10-31 12:00:59 -05:00
1 parent 3243404374
commit 763f4a7c04
44 files changed
+374 -370

No files matched your search

+3 -10
View File
@@ -86,16 +86,9 @@ function Brotli.decompress(buf: buffer | string, offset: number?, length: number
end
local out, size, consumed = Decode.decompress(b, o, l, maxBytes, S, large)
if not out then
local msg = tostring(size)
local code = Err.CORRUPT
if string.find(msg, "truncated", 1, true) then
code = Err.TRUNCATED
elseif string.sub(msg, 1, 6) == "limit:" then
code = Err.LIMIT
elseif string.sub(msg, 1, 12) == "unsupported:" then
code = Err.UNSUPPORTED
end
return nil, Err.new(code, "brotli: " .. msg, o)
local e = Err.from(size, o)
e.message = "brotli: " .. e.message
return nil, e
end
local res = out
if buffer.len(out) ~= size then
+1 -9
View File
@@ -19,14 +19,6 @@ export type Options = {
maxBytes: number?, -- refuse output larger than this (default 2^30)
}
local KINDS = {
truncated = Err.TRUNCATED,
corrupt = Err.CORRUPT,
checksum = Err.CHECKSUM,
limit = Err.LIMIT,
unsupported = Err.UNSUPPORTED,
}
-- decompress(buf, offset?, length?, opts?) -> buffer, consumedEnd | nil, Err
function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?, opts: Options?): (buffer?, any)
local b, o, l, aerr = Bytes.span("Bzip2.decompress", buf, offset, length)
@@ -39,7 +31,7 @@ function Bzip2.decompress(buf: buffer | string, offset: number?, length: number?
end
local out, nOut, consumed = Decode.decode(b, o, o + l, maxBytes)
if not out then
return nil, Err.new(KINDS[consumed] or Err.CORRUPT, "bzip2: " .. tostring(nOut), o)
return nil, Err.from("bzip2: " .. tostring(nOut), o, consumed)
end
local res = out
if buffer.len(out) ~= nOut then
+2 -4
View File
@@ -72,8 +72,6 @@ export type Info = {
avi1: number?, -- the AVI1 field polarity, when the segment comes before the frame header
}
local CODES = { corrupt = Err.CORRUPT, unsupported = Err.UNSUPPORTED, limit = Err.LIMIT, truncated = Err.TRUNCATED, arg = Err.ARG }
local function range(buf: buffer, off: number?, len: number?): (number, number)
local n = buffer.len(buf)
local o = math.clamp(math.floor(tonumber(off) or 0), 0, n)
@@ -87,7 +85,7 @@ local function parse(buf: buffer, o: number, l: number, opts: any, headerOnly: b
return nil, Err.new(Err.CORRUPT, "internal error: " .. tostring(st))
end
if not st then
return nil, Err.new(CODES[code :: string] or Err.CORRUPT, msg or "decode failed", o)
return nil, Err.from(msg or "decode failed", o, code)
end
return st, nil
end
@@ -187,7 +185,7 @@ local function decodeOne(buf: buffer, o: number, l: number, opts: DecodeOptions?
return nil, Err.new(Err.CORRUPT, "internal error: " .. tostring(w))
end
if not w then
return nil, Err.new(CODES[h :: string] or Err.CORRUPT, tostring(px), o)
return nil, Err.from(px, o, h)
end
space = sp
if opts and opts.orient then
+20
View File
@@ -150,6 +150,26 @@ function Pcm.collect(src: Source, maxFrames: number?): (Types.Audio?, Err.Err?)
return a, nil
end
-- drain(dec, source?, maxFrames?) -> Types.Audio | nil, Err: a format's decode() through its open(): every frame of
-- the Decoder (dec.frames is only a hint: short or long streams keep what decodes), dec.tags, meta.source = source
-- (default dec.source). A decoder that recorded a failure in dec.error (an Err) fails the drain.
function Pcm.drain(dec: any, source: { [string]: any }?, maxFrames: number?): (Types.Audio?, Err.Err?)
local src, err = Pcm.source(dec)
if not src then
return nil, err
end
local a, cerr = Pcm.collect(src, maxFrames)
if not a then
return nil, cerr
end
if Err.is(dec.error) then
return nil, dec.error
end
a.tags = dec.tags or {}
a.meta.source = source or dec.source
return a, nil
end
-- ------------------------------------------------------------------------------------------------ decoders
-- rawDecoder(buf, offset, frames, channels, sampleRate, fmt, tags?) -> Types.Decoder | nil, Err.ARG: a streaming
-- decoder over frames * channels samples of fmt at buf[offset] (the caller has checked the range is inside buf)
+1 -9
View File
@@ -21,14 +21,6 @@ export type Options = {
verify: boolean?, -- check the content checksum when a frame has one (default true)
}
local CODES: { [string]: string } = {
TRUNCATED = Err.TRUNCATED,
CORRUPT = Err.CORRUPT,
CHECKSUM = Err.CHECKSUM,
UNSUPPORTED = Err.UNSUPPORTED,
LIMIT = Err.LIMIT,
}
-- magic numbers: a zstd frame, and skippable frames (0x184D2A50 .. 0x184D2A5F)
Zstd.MAGIC = "\40\181\47\253"
@@ -56,7 +48,7 @@ function Zstd.decompress(buf: buffer | string, offset: number?, length: number?,
local verify = not (opts and opts.verify == false)
local out, len, consumed, at = Decode.decompress(b, o, o + l, maxBytes, verify)
if not out then
return nil, Err.new(CODES[len] or Err.CORRUPT, tostring(consumed), at)
return nil, Err.from(consumed, at, len)
end
if buffer.len(out) ~= len then
local r = buffer.create(len)