Skip to content

fix: correct numeric string coercion with leading zeros#535

Draft
ljluestc wants to merge 1 commit intoyuin:masterfrom
ljluestc:private/issue-478-dostring-coercion
Draft

fix: correct numeric string coercion with leading zeros#535
ljluestc wants to merge 1 commit intoyuin:masterfrom
ljluestc:private/issue-478-dostring-coercion

Conversation

@ljluestc
Copy link
Copy Markdown

@ljluestc ljluestc commented May 4, 2026

What problem does this PR solve?

Issue Number: close #478
L.DoString returns an incorrect result for string-to-number coercion in arithmetic expressions.

Repro script:

a = "00001" + "00011"
print(a)

Expected: 12
Actual: 10

This is inconsistent with Lua 5.1 arithmetic coercion expectations for numeric strings.

What changed and how does it work?

This PR updates numeric coercion during arithmetic evaluation for string operands:

  1. Parse decimal integer strings using base 10 first, so leading-zero values are treated as decimal (not octal).
  2. Preserve hex-prefixed parsing behavior for values like 0x7d6.
  3. Keep existing fallback behavior for floating-point parsing and non-numeric failures.
  4. Add regression coverage in both Go tests and _glua-tests/issues.lua.

Why this approach?

  • Aligns runtime behavior with Lua 5.1 coercion semantics.
  • Fixes the reported user-visible bug with a narrowly scoped change.
  • Adds regression coverage to prevent future breakage.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
    • Executed:
      • go test -run 'TestDoStringNumericString(Coercion|HexCoercion)$' -count=1
      • go test -run TestGlua -count=1
    • Covered repro:
      • a = "00001" + "00011" -> 12
  • No need to test

Documentation

  • Affects user behaviors
  • Additional docs required

Release note

Fix incorrect string numeric coercion in arithmetic so expressions like "00001" + "00011" evaluate correctly.

Use decimal-first parsing for numeric string coercion to avoid unintended octal interpretation for values like "00011", while preserving hex support (for example "0x7d6").

Add regression coverage for issue yuin#478 in both DoString-focused tests and glua issue script tests.

Fixes yuin#478

Co-Authored-By: Oz <oz-agent@warp.dev>
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.

Result of L.DoString with coercion is not correct

1 participant