merge ws/cm-core: comments to specs only in core / containers / host / ops / api / Types / compat; STYLE comment policy

This commit is contained in:
4DBug committed 2026-11-01 01:33:57 -06:00
commit 5e2fe5ab6e
49 files changed
+156 -1095

No files matched your search

+12 -41
View File
@@ -1,21 +1,9 @@
-- compat/luajit.lua: the Luau standard library pieces rosetta uses, for LuaJIT 2.1 (plain luajit and LÖVE).
-- tools/build.py copies this file to build/_modules/luajit/rosetta/_compat.lua (tools/bundle.py inlines it at the top
-- of build/rosetta.lua), and every built module starts with
-- local __compat = require("rosetta._compat") local buffer, bit32, utf8, typeof, type, __iter = ...
-- on its first line (so line numbers match the source). Nothing here is installed as a global; the table/math/string
-- additions (table.create, math.clamp, string.split, ...) are added to the standard libraries only where missing.
--
-- Semantics follow Luau (checked by tests/compat.luau under both runtimes):
-- bit32 unsigned 32-bit results; shift counts outside -31..31 give 0; extract/replace/countlz/countrz/byteswap
-- (fractional operands: see the bit32 section).
-- buffer fixed-size byte arrays on FFI memory, little-endian, zero-filled; out-of-bounds access throws
-- "buffer access out of bounds" like Luau. A buffer is a table { p = uint8_t*, n = length } with a
-- private metatable: typeof(b) and the injected type(b) return "buffer".
-- __iter Luau's generalized iteration (`for k, v in t do`): build.py rewrites such loops to
-- `for k, v in __iter(t) do`. Functions pass through; arrays iterate with ipairs (JIT-compiled) when the
-- table has no keys past #t, anything else with next.
-- LuaJIT-only extensions (not in Luau; host/love uses them when present): buffer.toffi(b) -> uint8_t* (valid while b
-- is alive), buffer.fromffi(ptr, n) -> a new buffer with a copy of n bytes.
-- compat/luajit.lua: the Luau standard library pieces rosetta uses, for LuaJIT 2.1 (plain and LÖVE), with Luau
-- semantics (tests/compat.luau). tools/build.py copies it to build/_modules/luajit/rosetta/_compat.lua and prepends
-- `local __compat = require("rosetta._compat") ...` to every built module's first line; tools/bundle.py inlines it at
-- the top of build/rosetta.lua. Nothing is installed globally; table / math / string additions only fill gaps.
-- buffer = { p = uint8_t*, n = length } with a private metatable (typeof / type say "buffer"); LuaJIT-only
-- buffer.toffi(b) / buffer.fromffi(ptr, n).
local ffi = require("ffi")
local bit = require("bit")
local floor = math.floor
@@ -25,17 +13,13 @@ local TWO32 = 4294967296
local M = {}
-- ---------------------------------------------------------------------------------------------------- bit32
-- LuaJIT's bit.* take any number in +-2^51 (reduced mod 2^32) and return a signed int32; Luau returns the uint32.
-- Performance (inflate, pinned to one core): the sign fix written inline as `if r < 0 then r = r + 2^32 end` runs
-- about 45% faster than `r % 2^32`; operands are passed to bit.* as they are (no truncation step).
-- Divergence from Luau: a fractional operand is rounded by bit.tobit where Luau truncates it. The library never
-- passes fractions to bit32 (tests/compat.luau checks integers only).
-- The inline `if r < 0 then r = r + 2^32 end` sign fix runs ~45% faster than `r % 2^32` (inflate).
-- Divergence: bit.tobit rounds a fractional operand where Luau truncates (the library never passes fractions).
do
local band, bor, bxor, bnot, lshift, rshift, arshift, bswap =
bit.band, bit.bor, bit.bxor, bit.bnot, bit.lshift, bit.rshift, bit.arshift, bit.bswap
local B = {}
-- the 2- and 3-argument forms are the hot ones: no varargs on that path
function B.band(a, b, c, d, ...)
local r
if b == nil then
@@ -186,7 +170,6 @@ do
M.bit32 = B
end
-- ---------------------------------------------------------------------------------------------------- buffer
do
ffi.cdef([[
void *memmove(void *dst, const void *src, size_t n);
@@ -208,9 +191,8 @@ do
BUF.__tostring = function(b) return ("buffer: %p"):format(b.p) end
M.BUFFER_MT = BUF
-- Small buffers live in GC memory (ffi.new); big ones in calloc'd memory, so they do not count against the
-- 2 GB GC heap of non-GC64 builds (LÖVE). The GC does not see calloc'd bytes, so a full collection runs every
-- 512 MB of them.
-- big buffers are calloc'd so they don't count against the 2 GB GC heap of non-GC64 builds (LÖVE); a full
-- collection runs every 512 MB of them since the GC doesn't see those bytes
local BIG = 262144
local external = 0
@@ -380,7 +362,6 @@ do
end
end
-- LuaJIT-only extensions
function Buf.toffi(b) return b.p end
function Buf.fromffi(ptr, n)
local b = create(n)
@@ -390,7 +371,6 @@ do
M.buffer = Buf
end
-- ---------------------------------------------------------------------------------------------------- typeof / type
do
local BUF = M.BUFFER_MT
function M.type(v)
@@ -404,7 +384,6 @@ do
M.isbuffer = function(v) return getmetatable(v) == BUF end
end
-- ---------------------------------------------------------------------------------------------------- __iter
local function arrayIter(s, i)
local t, n = s[1], s[2]
i = i + 1
@@ -430,7 +409,6 @@ function M.__iter(t, s, c)
return next, t, nil
end
-- ---------------------------------------------------------------------------------------------------- table
do
local okn, tnew = pcall(require, "table.new")
local okc, tclear = pcall(require, "table.clear")
@@ -475,11 +453,9 @@ do
end
end
-- ---------------------------------------------------------------------------------------------------- math
do
-- math.min / math.max are LuaJIT fast functions (branch-free minsd / maxsd in a trace); the two data-dependent
-- branches of `if x < lo ... elseif x > hi` spawned side traces in hot loops (codecs/Dct, colour conversion) and
-- ran ~3.5x slower. NaN stays NaN, as in Luau (maxsd would return lo).
-- math.min / math.max are branch-free in a trace; an if / elseif clamp spawned side traces and ran ~3.5x slower.
-- NaN stays NaN, as in Luau (maxsd would return lo).
local mmin, mmax = math.min, math.max
math.clamp = math.clamp or function(x, lo, hi)
if lo > hi then error("max must be greater than or equal to min", 2) end
@@ -507,7 +483,6 @@ do
end
end
-- ---------------------------------------------------------------------------------------------------- string
string.split = string.split or function(s, sep)
sep = sep or ","
local out = {}
@@ -527,7 +502,6 @@ string.split = string.split or function(s, sep)
end
end
-- ---------------------------------------------------------------------------------------------------- utf8
do
-- Lua 5.1 patterns cannot hold a NUL byte: %z stands for it
local U = { charpattern = "[%z\x01-\x7F\xC2-\xFD][\x80-\xBF]*" }
@@ -546,7 +520,6 @@ do
for i = 1, n do out[i] = enc((select(i, ...))) end
return table.concat(out)
end
-- decode at byte i: codepoint, length; nil on invalid UTF-8
local function dec(s, i)
local c = byte(s, i)
if not c then return nil end
@@ -593,7 +566,6 @@ do
end
return unpack(out, 1, k)
end
-- len(s, i, j) -> count | nil, position of the first invalid byte
function U.len(s, i, j)
i = i or 1
j = j or #s
@@ -644,7 +616,6 @@ do
M.utf8 = utf8 or U
end
-- install(): put the Luau globals in _G for code outside the library (a LÖVE game that wants buffer / bit32 itself)
function M.install(env)
env = env or _G
env.buffer = env.buffer or M.buffer