chunked build: detect check (F.detect on common headers loads the Detect chunk only, Detect under 160 KB); sniff extraction skips table and type keys (Aac.adts no longer pulls core/Bits into Detect); guide: chunk layout

This commit is contained in:
4DBug committed 2026-11-03 01:54:24 -06:00
1 parent 62fcfa7e93
commit 45f6cbc7a8
11 files changed
+1610 -1571

No files matched your search

+69 -1
View File
@@ -252,7 +252,8 @@ def bundle_roblox():
# Chunks: Core; Detect (DETECT: what F.detect runs, apart from Core); one per hot codec (HOT); then one per unit (a
# format folder, a codec or container with its submodules), small neighbours of a family (top folder, or
# codecs/<category>) packed up to UNIT_PACK bytes; a chunk is named after its first unit (image_png, containers_Riff).
# So opening a format compiles its own modules and what they use, not its family.
# So opening a format compiles its own modules and what they use, not its family; F.detect compiles Detect alone
# (detect_check).
# A --!native module's functions get @native (a chunk can mix native and non-native modules); a chunk with no native
# module has none. Outputs: build/Rosetta.rbxm (LZ4, written by model/rbxm under the luau CLI), build/Rosetta.rbxmx,
# build/Rosetta.rbxm.linemap.json ({chunk: [[first line, source file], ...]}).
@@ -462,6 +463,7 @@ def bundle_chunked(mods, native, deps):
if not text.startswith("--!optimize 2\n"):
sys.exit("bundle: chunk %s does not start with --!optimize 2" % name)
codegen_check([("Rosetta", root)] + sources, chunks, native)
detect_check(root, sources)
write_rbxm(root, sources)
@@ -509,6 +511,72 @@ def codegen_check(sources, chunks, native):
if bad:
sys.exit("bundle: chunk budget:\n " + "\n ".join(bad))
# ------------------------------------------------------------------------------------------------ detect check
# F.detect on common inputs loads the Detect chunk only (DETECT_CASES: a header and a name), and Detect stays under
# DETECT_MAX source bytes: requiring the root and a first F.detect compile Core + Detect, nothing else. Run under the
# luau CLI on a copy of the chunked tree whose root records the chunks it requires.
DETECT_MAX = 160 * 1024
DETECT_CASES = [ # (format, header, file name)
("mp4", "\\0\\0\\0\\32ftypisom\\0\\0\\2\\0isomiso2avc1mp41\\0\\0\\0\\8free", "x.mp4"),
("png", "\\137PNG\\r\\n\\26\\n\\0\\0\\0\\13IHDR\\0\\0\\0\\1\\0\\0\\0\\1\\8\\6\\0\\0\\0", "x.png"),
("jpeg", "\\255\\216\\255\\224\\0\\16JFIF\\0\\1\\1\\0\\0\\1\\0\\1\\0\\0", "x.jpg"),
("wav", "RIFF\\36\\0\\0\\0WAVEfmt \\16\\0\\0\\0\\1\\0\\1\\0\\68\\172\\0\\0\\136\\88\\1\\0\\2\\0\\16\\0", "x.wav"),
("mp3", "ID3\\3\\0\\0\\0\\0\\0\\0\\255\\251\\144\\100", "x.mp3"),
("vorbis", "OggS\\0\\2\\0\\0\\0\\0\\0\\0\\0\\0\\1\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\1\\30\\1vorbis", "x.ogg"),
("gltf", "glTF\\2\\0\\0\\0\\20\\0\\0\\0", "x.glb"),
("gif", "GIF89a\\1\\0\\1\\0\\0\\0\\0", "x.gif"),
("zip", "PK\\3\\4\\20\\0\\0\\0\\0\\0", "x.zip"),
("flac", "fLaC\\0\\0\\0\\34", "x.flac"),
]
DETECT_CHECK = """local F = require("./Rosetta")
local __ROSETTA_CHUNKS = F._chunksLoaded
local out = {}
for _, c in __CASES__ do
local before = #__ROSETTA_CHUNKS
local name = F.detect(buffer.fromstring(c[2]), { name = c[3] })
local got = {}
for i = before + 1, #__ROSETTA_CHUNKS do
table.insert(got, __ROSETTA_CHUNKS[i])
end
table.insert(out, c[1] .. "\\t" .. tostring(name) .. "\\t" .. table.concat(got, " "))
end
print(table.concat(out, "\\n"))
"""
def detect_check(root, sources):
d = os.path.join(MODULES, "chunked", "detect_check")
shutil.rmtree(d, ignore_errors=True)
shutil.copytree(CHUNKED, os.path.join(d, "Rosetta"))
hook = "\trequire(m)(__FILES, __include)\n"
if hook not in root:
sys.exit("bundle: detect check: the root's chunk require changed")
with open(os.path.join(d, "Rosetta", "init.luau"), "w") as f:
f.write(root.replace(hook, "\ttable.insert(__CHUNKS_LOADED, name)\n" + hook).replace(
"local __FILES = {}", "local __CHUNKS_LOADED = {}\nlocal __FILES = {}", 1).replace(
"\nreturn Rosetta\n", "\nRosetta._chunksLoaded = __CHUNKS_LOADED\nreturn Rosetta\n"))
cases = "{ " + ", ".join('{ "%s", "%s", "%s" }' % c for c in DETECT_CASES) + " }"
with open(os.path.join(d, "check.luau"), "w") as f:
f.write(DETECT_CHECK.replace("__CASES__", cases))
r = subprocess.run(tool("luau") + ["check.luau"], capture_output=True, text=True, cwd=d)
if r.returncode != 0:
sys.exit("bundle: detect check failed:\n" + (r.stdout + r.stderr)[-3000:])
bad = []
for line in r.stdout.strip().split("\n"):
case, name, got = (line.split("\t") + [""])[:3]
if name != case:
bad.append("%s detected as %s" % (case, name))
if [x for x in got.split() if x != "Detect"]:
bad.append("detect %s loads chunks %s (only Detect: copy what its sniff reads, tools/sniff_extract.py)"
% (case, got))
size = len(dict(sources)["Detect"])
if size > DETECT_MAX:
bad.append("Detect: %d source bytes > %d" % (size, DETECT_MAX))
if bad:
sys.exit("bundle: detect check:\n " + "\n ".join(bad))
print("bundle: Rosetta.rbxm detect loads Detect only (%s), %.0f KB" % (
", ".join(c[0] for c in DETECT_CASES), size / 1024))
def long_string(s):
n = 0
+3 -1
View File
@@ -85,7 +85,9 @@ def statements(text):
def free_names(code):
"""identifiers read by code that are not field / method names (an over-approximation: locals are included)"""
"""identifiers read by code that are not field / method names or table / type keys ({ name = ..., name: T }; an
over-approximation: locals are included)"""
code = re.sub(r"([{,]\s*)" + NAME + r"(\s*(=(?!=)|:\s))", r"\1\2", code)
return {m.group(1) for m in re.finditer(r"(?<![.:\w])\s*(" + NAME + r")", code)} - KEYWORDS