Files
rosetta/README.md
T

77 lines
4.2 KiB
Markdown

# formats-luau
Read, write and convert file formats in pure Luau, from one source tree that runs on **Roblox** (Luau, native code
generation) and on **LuaJIT / LÖVE** (through `tools/build.py`). Every format of a category maps onto one standard type
(`src/Formats/Types.luau`): a PNG and a WebP both load as an `Image`, every audio format opens as the same streaming
`Decoder`.
Status: foundation. Formats so far: PNG (read / write). Codecs: DEFLATE (inflate + compressor, zlib, gzip). See
[docs/FORMATS.md](docs/FORMATS.md) (generated) for the capability matrix.
```luau
local F = require(Formats) -- LuaJIT: require("formats"); luau CLI: require("@formats")
local img, err = F.load(src) -- src: buffer | string | path (hosts with files)
if not img then print(tostring(err)) end -- errors are values: "png: CORRUPT at 0x21: bad filter type 7"
local buf = F.save(img, nil, { format = "png" }) -- a buffer; F.save(img, "out.png") writes a file
local s = F.saveString(img, { format = "png" })
local out = F.convert(src, "png", { level = 9 })
local img2 = F.image.load(src) -- category facades: F.image / audio / video / model / font / ...
for _, f in F.formats() do print(f.name, f.caps.decode, f.caps.encode) end
```
Nothing throws on bad data or bad arguments: every call returns `nil, err` (`err.code` is one of the `F.Err` codes).
## Runtimes
| Runtime | How | Files |
|---|---|---|
| Roblox | `python3 -I tools/build.py roblox`, then `rojo build` / `rojo serve` (`default.project.json` mounts `build/roblox/Formats`) | none: pass buffers / strings; `F.host.toEditableImage(img)` |
| LuaJIT | `python3 -I tools/build.py luajit`; `package.path = "build/luajit/?.lua;build/luajit/?/init.lua;" .. package.path` | `io.open` |
| LÖVE | the LuaJIT build, copied into the game | `love.filesystem` (+ `io`); `F.host.toImageData(img)`, `F.host.fromImageData(id)`, `F.host.toSoundData(audio or decoder)` |
| luau CLI | `luau` with the `.luaurc` alias `@formats` (`src/` directly) | none |
The LuaJIT build supplies the Luau standard library pieces it needs from `compat/luajit.lua` (unsigned `bit32`,
`buffer` on FFI memory, `table.create`, `math.clamp`, `string.split`, `utf8`, ...), required by each module, without
touching globals (`require("formats._compat").install()` puts `buffer` / `bit32` / `utf8` / `typeof` in `_G` if a
game wants them).
## Development
```sh
bash tools/test.sh # fixtures, registry, build, every test under luau --codegen -O2 and LuaJIT, lint
bash tools/test.sh png # one test (both runtimes), no lint
bash tools/lint.sh # globals, headers, requires, native-throw, layers, deps, contract, registry
bash tools/bench.sh # throughput on both runtimes (pin it: taskset -c 2 bash tools/bench.sh)
python3 -I tools/build.py # build/roblox + build/luajit
python3 -I tools/gen_registry.py # src/Formats/Registry.luau + docs/FORMATS.md from the format folders
python3 -I tools/fixtures.py # tests/gen/*.luau from tools/fixtures/*.py
```
Tools: python3, and through nix: `luau` (+ `luau-analyze`), `luajit`, `darklua` 0.19, `rojo` (each script finds them
on PATH or runs them with `nix shell nixpkgs#<tool>`).
Layout, layering, the format contract, how to add a format or absorb a repo: [docs/STYLE.md](docs/STYLE.md).
```
src/Formats/ init.luau (API) Types.luau Registry.luau (generated) core/ host/ codecs/ containers/
<category>/<fmt>/ ops/
compat/luajit.lua tools/ tests/ (T.luau, <category>/<fmt>.luau, fixtures/, gen/) docs/ licenses/
```
## Performance (one Zen 5 core, `taskset -c 2 bash tools/bench.sh`)
| | luau --codegen -O2 | LuaJIT 2.1 |
|---|---|---|
| inflate, 6 MB text + binary (exact size) | ~330 MB/s | ~470 MB/s |
| zlib (inflate + Adler-32) | ~170 MB/s | ~280 MB/s |
| CRC-32 | ~310 MB/s | ~520 MB/s |
| PNG decode 1024x768 RGBA | ~52 MB/s | ~30-75 MB/s |
| deflate compress -6 | ~12 MB/s | ~8-20 MB/s |
LuaJIT numbers vary between runs: its trace recorder is randomised, and about one run in ten picks a slower trace
layout for a loop (inflate then runs at ~190 MB/s). Unpinned runs on this hybrid CPU also differ 2x between core types.
## Licence
See [licenses/NOTICE.md](licenses/NOTICE.md).