merge ws/rbxchunks2

This commit is contained in:
4DBug committed 2026-11-03 03:32:28 -06:00
commit a9874e9f54
21 files changed
+8793 -6602

No files matched your search

+19 -9
View File
@@ -1,13 +1,15 @@
#!/usr/bin/env python3
# Brotli static data generator (python3 -I tools/brotli_static.py [brotli-source-dir]): writes
# src/Rosetta/codecs/compress/Brotli/Static.luau from the official brotli C sources (MIT; licenses/brotli-LICENSE.txt):
# the 122784-byte RFC 7932 static dictionary (c/common/dictionary_inc.h, stored deflated + base64), the context
# lookup table (c/common/context.c) and the 121 word transforms (c/common/transform.c).
# src/Rosetta/codecs/compress/Brotli/Dictionary.luau and Static.luau from the official brotli C sources (MIT;
# licenses/brotli-LICENSE.txt): the 122784-byte RFC 7932 static dictionary (c/common/dictionary_inc.h, stored
# deflated + base64; a module of its own, loaded on the first dictionary reference), and its size and CRC-32, the
# context lookup table (c/common/context.c) and the 121 word transforms (c/common/transform.c).
# Without an argument the source comes from `nix build nixpkgs#brotli.src`.
import base64, os, re, subprocess, sys, zlib
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
OUT = os.path.join(ROOT, "src", "Rosetta", "codecs", "Brotli", "Static.luau")
OUT = os.path.join(ROOT, "src", "Rosetta", "codecs", "compress", "Brotli", "Static.luau")
OUT_DICT = os.path.join(ROOT, "src", "Rosetta", "codecs", "compress", "Brotli", "Dictionary.luau")
def c_array(text, name):
@@ -49,18 +51,26 @@ def main():
trans = c_array(tr_text, "kTransformsData")
assert len(psmap) == 50 and len(trans) == 363
z = zlib.compress(dictionary, 9)
dlines = [
"--!strict",
"-- codecs/compress/Brotli/Dictionary: GENERATED by tools/brotli_static.py from the brotli C sources (MIT, Google; see",
"-- licenses/brotli-LICENSE.txt) - do not edit. The RFC 7932 static dictionary (122784 bytes, deflated, base64).",
"return {",
"\tdictionary = {",
]
dlines += ['\t\t"%s",' % c for c in b64_chunks(z)] + ["\t},", "}", ""]
with open(OUT_DICT, "w") as f:
f.write("\n".join(dlines))
lines = [
"--!strict",
"-- codecs/compress/Brotli/Static: GENERATED by tools/brotli_static.py from the brotli C sources (MIT, Google; see",
"-- licenses/brotli-LICENSE.txt) - do not edit. The RFC 7932 static dictionary (122784 bytes, deflated, base64),",
"-- the context lookup table (Appendix B, 4 modes x 512) and the 121 word transforms (Appendix B).",
"-- licenses/brotli-LICENSE.txt) - do not edit. The RFC 7932 static dictionary's size and CRC-32 (the dictionary is in",
"-- Brotli/Dictionary), the context lookup table (Appendix B, 4 modes x 512) and the 121 word transforms (Appendix B).",
"return {",
"\tdictionarySize = %d," % len(dictionary),
"\tdictionaryCrc = %d," % zlib.crc32(dictionary),
"\tdictionary = {",
]
lines += ['\t\t"%s",' % c for c in b64_chunks(z)]
lines += ["\t},", "\tcontext = {"]
lines += ["\tcontext = {"]
lines += ['\t\t"%s",' % c for c in b64_chunks(ctx)]
lines += ["\t},"]
lines += ['\tprefixSuffix = "%s",' % base64.b64encode(ps).decode()]
+84 -24
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,22 +264,28 @@ 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"]),
("MpegTs", ["containers/MpegTs", "containers/MpegEs", "containers/MpegPs"]),
("Dct", ["codecs/image/Dct"]),
("Jpeg", ["codecs/image/Jpeg"]),
("Vp8", ["codecs/image/Vp8", "codecs/image/Vp8L"]),
("Vp8", ["codecs/image/Vp8"]),
("Vp8L", ["codecs/image/Vp8L"]),
("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"]),
("Mpa", ["codecs/audio/Mpa"]),
("OpusEncoder", ["codecs/audio/Opus/CeltEncoder"]),
("Opus", ["codecs/audio/Opus"]),
("Vorbis", ["codecs/audio/Vorbis"]),
("AacSbr", ["codecs/audio/Aac/" + m for m in ("Sbr", "SbrBits", "SbrDct", "SbrHf", "SbrQmf", "SbrTables", "Ps")]),
("Aac", ["codecs/audio/Aac"]),
("Flac", ["codecs/audio/Flac"]),
("Musepack", ["codecs/audio/Musepack"]),
@@ -297,13 +301,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 +356,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 +565,77 @@ 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 and AAC cases read
# tests/gen (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"]),
]
FIXTURE_CASES = [ # (fixture module, file, what opens it, the first value's field, its value, the chunks besides Detect)
("h264_containers", "baseline.mp4", "video", "width", "176", [
"video_mp4", "containers_IsoBmff", "containers_Demux", "codecs_video_Video", "codecs_image_Yuv", "H264",
"H264Cavlc"]),
("h264_containers", "cabac_b.mp4", "video", "width", "176", [
"video_mp4", "containers_IsoBmff", "containers_Demux", "codecs_video_Video", "codecs_image_Yuv", "H264",
"H264Cabac"]),
("m4a_aac", "aac_stereo_128k.m4a", "audio", "n", "4096", [
"audio_m4a", "codecs_audio_Pcm", "codecs_audio_Stream", "containers_IsoBmff", "Aac", "core_Bits",
"codecs_audio_Mdct"]),
("m4a_he", "he_aac_v2.m4a", "audio", "n", "4096", [
"audio_m4a", "codecs_audio_Pcm", "codecs_audio_Stream", "containers_IsoBmff", "Aac", "core_Bits",
"codecs_audio_Mdct", "AacSbr"]),
]
LOAD_FIXTURE = """(function()
for _, c in require("../../../../tests/gen/%s") do
if c.name == "%s" then
local d = assert(F.%s.open(buffer.fromstring(c.data)))
if "%s" == "width" then
local f: any = assert(d:nextFrame())
return (f.image or f).width
end
local _, n = d:read(4096)
return n
end
end
return nil
end)()"""
def load_check(d):
cases = list(LOAD_CASES)
for mod, name, cat, field, want, chunks in FIXTURE_CASES:
if os.path.isfile(os.path.join(ROOT, "tests", "gen", mod + ".luau")):
cases.append((name, LOAD_FIXTURE % (mod, name, cat, field), want, ["Detect"] + chunks))
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):