Clearer code for controlling maximum registers

Plus, added a test to check that limit.
This commit is contained in:
Roberto Ierusalimschy
2024-06-26 14:46:44 -03:00
parent c1dc08e8e8
commit fb7e5b76c9
3 changed files with 28 additions and 10 deletions

View File

@@ -445,5 +445,20 @@ do -- string constants
assert(T.listk(f2)[1] == nil)
end
do -- check number of available registers
-- 1 register for local + 1 for function + 252 arguments
local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)"
local prog = T.listcode(assert(load(source)))
-- maximum valid register is 254
for i = 1, 254 do
assert(string.find(prog[2 + i], "MOVE%s*" .. i))
end
-- one more argument would need register #255 (but that is reserved)
source = "local a; return a(" .. string.rep("a, ", 253) .. "a)"
local _, msg = load(source)
assert(string.find(msg, "too many registers"))
end
print 'OK'