Skip to content

fix(lexer): convert long numerals in linear time without raising - #444

Open
adriencohen wants to merge 1 commit into
deviceio:mainfrom
adriencohen:fix/lexer-long-numerals
Open

adriencohen wants to merge 1 commit into
deviceio:mainfrom
adriencohen:fix/lexer-long-numerals

Conversation

@adriencohen

@adriencohen adriencohen commented Nov 27, 2026 •

Copy link
Copy Markdown

A long numeric literal made Lua.parse_chunk/1 raise a host ArithmeticError instead of returning {:error, _}, and tonumber on a long string did quadratic bignum work outside the instruction budget before failing.

n digits before after
tonumber, 200,000 13.7 ms, then raises 0.5 ms
tonumber, 1,000,000 160 ms, then raises 2.4 ms
literal, 200,000 13.6 ms, then raises 0.9 ms
literal, 1,000,000 161 ms, then raises 4.2 ms

Median of 5 runs, Apple M2, OTP 29.

The lexer and every string-to-number conversion now share one linear-time converter that follows Lua 5.3: an overflowing decimal integer becomes a float, hex integers and tonumber(s, base) wrap modulo 2^64, and a float past the double range becomes the 1.0e308 stand-in. Malformed hex numerals (0x., 0x1p) are now parse errors, and hex floats round correctly. Most of the math.lua suite lines skipped for long numerals now pass.

A very long numeric literal makes Lua.parse_chunk/1 raise a host
ArithmeticError, and tonumber on a long string does quadratic bignum
work outside the instruction budget before failing.
@adriencohen
adriencohen force-pushed the fix/lexer-long-numerals branch from 921208f to c12d67f Compare November 27, 2026 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant