Repository navigation
fix(vm,mathlib): return integers from #, select('#') and the math library - #97
Merged
Merged
Conversation
skhaz
approved these changes
Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect
The length operator,
select('#')and the math library return floats where Lua 5.4 returns integers, andmath.randomwith a full-width range fails:Cause
OP_LENstores string and table lengths withSetNumber, andbaseSelectpushes the'#'count asLNumber.mathAbs,mathFloor,mathCeil,mathFmod,mathModf,mathMaxandmathMinread every argument withCheckNumber, which converts integers to float, and pushLNumber.mathRandomcomputesn-m+1inint(overflows for wide ranges, andrand.IntNpanics on the negative result), reads bounds with the truncatingCheckInt, and pushesLNumber.Fix
The library functions follow
lmathlib.c:floor/ceilreturn integer arguments unchanged and convert the rounded float to an integer when it fits (pushnumint);modfdoes the same for its integral part, and its fractional part is0.0when the argument is integral or infinite.abskeeps integers (wrapping atmath.mininteger),fmodon two integers uses truncating integer remainder with the "zero" argument error and the-1special case.max/minreturn the selected argument itself, compared with the VM's<, so the subtype of the winner is kept.random(m, n)/random(n)read bounds withluaL_checkintegersemantics, draw uniformly over the full[m, n]range with unsigned arithmetic, and return an integer;random(0)returns a random integer; argument errors match Lua 5.4.OP_LENstores string and table lengths as integers and the__lenresult as returned.select('#')pushes an integer.luaL_checkintegeralready existed asformatIntegerinstringlib_format.go; it moves toauxlib.goascheckIntegerand bothstring.formatandmath.randomuse it.Tests
integer_subtype_results_test.gochecks the value and subtype of#(tables, strings,__lenreturning integer, float and non-number),select('#'), and every math function above for integer, float, mixed, numeric-string, out-of-range, negative-zero, infinity andmath.minintegerarguments, therandombounds over repeated draws, and the argument errors offmod,random,maxandmin. Expected values come from the Lua 5.4 reference interpreter. 37 of the 48 result cases, all 9 error cases and the bounds test fail onmain.Not covered here (tracked in the conformance issue):
next/pairsarray keys, the publicCheckInthelpers, and exact mixed integer/float<, whichmax/mininherit once it lands. String library results are in #95.Verification
go vet ./...,golangci-lintv2.8.0 with the CI flags,go test ./...(full fixture corpus) and the coverage collector run pass.The runtime picks this up through a go-lua dependency bump after merge.