Skip to content

fix(stringlib): return integers for byte values, positions and counts - #95

Merged
wolfy-j merged 1 commit into
mainfrom
fix/stringlib-integer-results
Nov 15, 2026
Merged

wolfy-j merged 1 commit into
mainfrom
fix/stringlib-integer-results

Conversation

@wolfy-j

@wolfy-j wolfy-j commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Defect

The string library returns floats where Lua 5.4 returns integers:

print(math.type(string.byte("a")))                 -- float, Lua 5.4: integer
print(math.type(string.len("abc")))                -- float
print(math.type((string.find("hello", "l"))))      -- float
print(math.type(string.match("hello", "()ll")))    -- float
print(math.type(select(2, string.gsub("a", "a", "b")))) -- float

Integer-sensitive code (math.type checks, // and % on byte values, string.format("%d", ...) paths, integer table keys from position captures) sees the wrong subtype.

Cause

strByte, strLen, strFind (plain and pattern, including the empty-pattern case), position captures in strFind, strMatch, strGmatch and strGsub (function and table replacements), and the strGsub match count push LNumber.

Fix

These sites push LInteger. string.unpack float formats are unchanged.

Tests

stringlib_integer_results_test.go checks the value and the LInteger type of every result above, plus a __index lookup with a position-capture key in gsub. Expected values come from the Lua 5.4 reference interpreter. All 16 cases fail on main.

TestGsubMatchesPreviousImplementation and TestGsubSpecialCases read the gsub count with an LNumber type assertion, which encoded the float result. They now assert LInteger.

Verification

go vet ./..., golangci-lint v2.8.0 with the CI flags, go test ./... (full fixture corpus), the coverage collector run, and the race "other" shard (go test -race on 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.

string.byte, string.len, string.find, string.match, string.gmatch and
string.gsub pushed byte values, positions, position captures and
replacement counts as floats, so math.type(string.byte("a")) was
"float". Lua 5.4 returns integers for all of them.
@wolfy-j
wolfy-j requested a review from skhaz November 15, 2026 18:56
@wolfy-j
wolfy-j merged commit 1c19f1e into main Nov 15, 2026
12 checks passed
@wolfy-j
wolfy-j deleted the fix/stringlib-integer-results branch November 15, 2026 20:39
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.

2 participants