Repository navigation
fix(lua): convert strings to numbers as lua_stringtonumber does - #92
Merged
Merged
Conversation
String-to-number conversion had five independent implementations (the
runtime's parseNumber and parseNumberValue, tonumber's own parser, the
parser's literal parser and numparse for the type checker), and none
followed Lua: parseNumber always produced floats, so tonumber("10"),
"10" + 0 and -"10" were floats; tonumber rejected exponents ("1e5"),
hex floats, hex integers that wrap, numerals outside the double range and
whitespace other than space, tab and newline; tonumber with a base
returned floats and accepted out-of-range bases and numbers; the lexer
stopped hexadecimal numerals at '.' and 'p', and a numeral the compiler
could not parse became NaN instead of a compile error.
numparse.Parse now implements l_str2int and l_str2d from Lua 5.4 and
numparse.ParseBase implements tonumber's base conversion. Every
conversion uses them: tonumber, arithmetic and bitwise coercion of string
operands (including // and unary minus, which keep the integer subtype),
CheckNumber, ToInt, LVAsNumber, math.tointeger, the lexer (which now reads
numerals as read_numeral does and rejects malformed ones), literal
compilation and the type checker's literal helpers.
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
String-to-number conversion does not follow Lua 5.4 (
lua_stringtonumber):tonumber("10"),"10" + 0,-"10"10.0(float)10(integer)tonumber("1e5")nil100000.0tonumber("1e400")nilinftonumber("0x1p4"),tonumber("0x1.8")nil16.0,1.5tonumber("0xffffffffffffffffff")nil-1(hex integers wrap)tonumber("\t\v\f\r 5 \n"),tonumber("+0x5")nil5tonumber(7)7.07tonumber("ff", 16)255.0255tonumber("ffffffffffffffff", 16)nil-1tonumber("10", 99),tonumber(10, 16)nil,10.0" 7 " // 230xffffffffffffffff,1e400(literals)NaN-1,inf0x1p4,0xA.8p0,0x.8(literals)16.0,10.5,0.5return 3x,return 1.2.3malformed numberCause
There were five separate conversions, and none matched the reference:
parseNumber(used by arithmetic coercion, unary minus,CheckNumber,ToInt,LVAsNumberandmath.tointeger) always returned a float, and used Go'sstrconvsyntax.parseNumberValue(literals, bitwise coercion) kept the subtype but usedstrconv, which rejects wrapping hex integers and hex floats withoutp.tonumberhad its own parser: it switched to float only on., so exponents failed, and it always returned floats..andp. The compiler turned any numeral it could not parse intoNaNinstead of reporting an error.numparse) each had anotherstrconv-based parser.Fix
numparse.Parseimplementsl_str2intandl_str2dfrom Lua 5.4lobject.c:isspacetrimming and an optional sign.strtodfloat syntax, including hex mantissas with an optional fraction andpexponent.inf/nanare rejected.numparse.ParseBaseimplementstonumber's base conversion. The other conversions are removed, and every caller uses these two:parseNumberValuewrapsnumparse.Parse.parseNumberis removed.objectArith,//, unary minus) keeps the integer subtype of numeric strings:+ - * %on two integers give integers, the same as the VM fast paths.tonumberfollowsluaB_tonumber. Numbers keep their subtype, strings useParse, and with a base the first argument must be a string, the base must be in 2..36, and the result is an integer.CheckNumber,ToInt/ToInt64,LVAsNumberandLString.Formatconvert throughparseNumberValue.math.tointegerisexactInteger, somath.tointeger(2.0^63)isnil(it used to overflow).read_numeraldoes (hex digits,., exponent marks with a sign, and one touching letter) and rejects malformed ones withmalformed number. The compiler raises a compile error instead of emittingNaN, and the constant folder treats an unparsable numeral as non-constant.numparse's literal helpers (ParseIntegerLiteral,ParseFloatLiteral,ParseNumberLiteral,ParseIntegralLiteral) are built onParse.parser.gois regenerated with goyacc. Apart from the prologue, only the//linecomments change.Tests
types/numparse:TestParse(65 cases) andTestParseBase(21 cases). They cover integers, overflow to float, wrapping hex, decimal and hex floats, infinities, denormals, whitespace, signs and the rejected forms.string_to_number_test.go:tonumberwith and without a base, string coercion for every arithmetic operator, bitwise operators and unary minus,math.tointeger, literals, and the error cases. 43 of these fail onmain.compiler/parse/lexer_number_test.go: numeral tokens, where numerals stop before operators, and malformed numerals.Expected values come from the Lua 5.4 reference interpreter.
Not in this PR
CheckInt/OptInt(theluaL_checkintegerequivalents) still reject numeric strings and truncate floats, sostring.rep("x", "3")raises.OP_IDIVdoes not dispatch__idivmetamethods.Verification
go vet ./...,golangci-lintv2.8.0 with the CI flags,go test ./...(full fixture corpus), the coverage collector run, and the race "other" shard (go test -raceon all non-fixture tests and the non-regression fixture categories) pass. The regression-fixture race shards are left to CI.The runtime picks this up through a go-lua dependency bump after merge.