chunked build: one chunk per unit (no neighbour packing: host/Roblox alone, PNG no longer pulls Blend / Cpio); H.264 entropy coders in chunks of their own (Decoder requires CABAC or CAVLC on the first slice; Cabac / Cavlc tables out of Tables); Brotli dictionary in Brotli/Dictionary (its own chunk, on the first dictionary reference); load check: unknown detect, PNG load, H.264 CAVLC / CABAC open each require exactly their chunks

This commit is contained in:
4DBug committed 2026-11-03 02:28:48 -06:00
1 parent 8fd5a4c3f2
commit 05a44e1193
18 files changed
+5986 -3906

No files matched your search

+66 -23
View File
@@ -38,7 +38,7 @@
# --check-rbxm: lune reads build/Rosetta.rbxm, .rbxmx and rojo's build of default.project.json; same tree and sources.
# --where <Rosetta.luau|rosetta.lua> <line> | <Rosetta.rbxm> <chunk> <line>: the source file and line of a bundle
# line (a Roblox error in the chunked build reads "...Rosetta.<chunk>:<line>").
import json, os, re, shutil, subprocess, sys
import json, os, re, shutil, struct, subprocess, sys, zlib
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
@@ -250,15 +250,13 @@ def bundle_roblox():
# `script`, require("@self/" .. name) over the same files in build/_modules/chunked/Rosetta). So requiring the root
# loads Core (what the root runs at load time) and every other chunk on first use, per Actor.
# 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; F.detect compiles Detect alone
# (detect_check).
# format folder, a codec or container with its submodules, a core or host module), named after it (image_bmp,
# containers_Riff, host_Roblox). So opening a format compiles its own modules and what they use, nothing packed
# beside them (load_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], ...]}).
CHUNK_SRC_MAX = 256 * 1024
UNIT_PACK = 32 * 1024
CHUNK_IR_MAX = 1 << 18
CHUNK_ASM_MAX = 4 << 20
CHUNKED = os.path.join(MODULES, "chunked", "Rosetta")
@@ -266,6 +264,8 @@ DETECT = ["Sniffs"] # F.detect: Sniffs.luau and what it requires at load time (
HOT = [
("Mpeg2", ["codecs/video/Mpeg2", "codecs/video/Mpeg1", "codecs/video/Dsp"]),
("H263", ["codecs/video/H263"]),
("H264Cabac", ["codecs/video/H264/Cabac", "codecs/video/H264/CabacTables"]), # the entropy coder a stream uses
("H264Cavlc", ["codecs/video/H264/Cavlc", "codecs/video/H264/CavlcTables"]),
("H264", ["codecs/video/H264"]),
("Theora", ["codecs/video/Theora"]),
("Legacy", ["codecs/video/Legacy"]),
@@ -276,6 +276,7 @@ HOT = [
("Bcn", ["codecs/image/Bcn"]),
("Png", ["image/png"]),
("Deflate", ["codecs/compress/Deflate"]),
("BrotliDictionary", ["codecs/compress/Brotli/Dictionary"]), # on the first dictionary reference
("Brotli", ["codecs/compress/Brotli"]),
("Lzma", ["codecs/compress/Lzma"]),
("Zstd", ["codecs/compress/Zstd"]),
@@ -297,13 +298,6 @@ def under(key, prefix):
return key == prefix or key.startswith(prefix + "/")
def family(key):
parts = key.split("/")
if len(parts) == 1:
return "Root"
return "_".join(parts[:2]) if parts[0] == "codecs" and len(parts) > 2 else parts[0]
def eager(src):
"""the includes a module runs at load time: those on a line with no indentation (a top-level statement; style)"""
return {m.group(1) for line in src.split("\n") if line[:1] not in ("\t", " ") for m in INCLUDES.finditer(line)}
@@ -359,16 +353,8 @@ def plan_chunks(mods):
units = {}
for k in left:
units.setdefault(unit(k), []).append(k)
run, size = [], 0
for u in sorted(units, key=lambda u: (family(u), u)):
sz = sum(len(mods[k][0]) for k in units[u])
if run and (family(run[0]) != family(u) or size + sz > UNIT_PACK):
pack(run[0].replace("/", "_"), [k for x in run for k in units[x]])
run, size = [], 0
run.append(u)
size += sz
if run:
pack(run[0].replace("/", "_"), [k for x in run for k in units[x]])
for u in sorted(units):
pack(u.replace("/", "_"), units[u])
return chunks
@@ -576,6 +562,63 @@ def detect_check(root, sources):
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))
load_check(d)
# load_check: the first load of a format, each in a fresh VM, requires exactly the chunks listed (its own units and
# what they use; a chunk that shows up here unasked is packed or required too eagerly). The H.264 cases read
# tests/gen/h264_containers (skipped without the fixtures).
LOAD_PNG = bytes([137, 80, 78, 71, 13, 10, 26, 10]) + b"".join(
struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d))
for t, d in [(b"IHDR", struct.pack(">IIBBBBB", 16, 16, 8, 6, 0, 0, 0)),
(b"IDAT", zlib.compress(b"".join(b"\1" + bytes(range(64)) for _ in range(16)))), (b"IEND", b"")])
LOAD_CASES = [ # (name, Luau expression whose value is printed, expected value, the chunks it may require)
("unknown", 'F.detect(buffer.fromstring(string.rep("\\201\\7\\99\\3\\250\\17\\0\\44", 8)), { name = "x.bin" })',
"macbinary", ["Detect", "Brotli", "core_Crc32", "core_Base64"]),
("png", "(F.image.load(buffer.fromstring(%s)) :: any).width" % "".join(
'"' + "".join("\\%03d" % c for c in LOAD_PNG) + '"'), "16",
["Detect", "Png", "Deflate", "core_Adler32", "core_Crc32", "core_Bits", "codecs_entropy_Huffman", "containers_Chunks"]),
]
H264_CASES = [("baseline.mp4", "H264Cavlc"), ("cabac_b.mp4", "H264Cabac")]
LOAD_H264 = """(function()
local Fx = require("../../../../tests/gen/h264_containers")
for _, c in Fx do
if c.name == "%s" then
local v = assert(F.video.open(buffer.fromstring(c.data)))
local f: any = assert(v:nextFrame())
return (f.image or f).width
end
end
return nil
end)()"""
def load_check(d):
cases = list(LOAD_CASES)
if os.path.isfile(os.path.join(ROOT, "tests", "gen", "h264_containers.luau")):
for name, coder in H264_CASES:
cases.append(("h264 " + name, LOAD_H264 % name, "176", [
"Detect", "video_mp4", "containers_IsoBmff", "containers_Demux", "codecs_video_Video",
"codecs_image_Yuv", "H264", coder]))
bad = []
for name, expr, want, allowed in cases:
script = os.path.join(d, "load.luau")
with open(script, "w") as f:
f.write('local F = require("./Rosetta")\nlocal n = #F._chunksLoaded\nlocal v = %s\n'
'print(tostring(v))\nprint(table.concat(F._chunksLoaded, " ", n + 1))\n' % expr)
r = subprocess.run(tool("luau") + ["load.luau"], capture_output=True, text=True, cwd=d)
out = r.stdout.strip().split("\n")
if r.returncode != 0 or len(out) != 2:
bad.append("%s: %s" % (name, (r.stdout + r.stderr)[-500:]))
continue
if out[0] != want:
bad.append("%s: got %s, want %s" % (name, out[0], want))
got = out[1].split()
if sorted(got) != sorted(allowed):
bad.append("%s loads %s, want %s" % (name, " ".join(got), " ".join(allowed)))
if bad:
sys.exit("bundle: load check:\n " + "\n ".join(bad))
print("bundle: Rosetta.rbxm first loads require only their own chunks (%s)" % ", ".join(c[0] for c in cases))
def long_string(s):