Skip to content

fix: implement Lua 5.4 patterns (P1-5b) and while pure cond re-evaluation (P1-9) - #24

Merged
esrrhs merged 1 commit into
masterfrom
fix/open-bugs
Oct 3, 2026
Merged

esrrhs merged 1 commit into
masterfrom
fix/open-bugs

Conversation

@esrrhs

@esrrhs esrrhs commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Summary

This PR resolves the two remaining critical open bugs identified during FakeFish integration:

  1. Bug 1 (P1-9): Re-evaluate while conditions for impure numeric expressions

    • Problem: When a while condition contained #t (table length), the expression was inferred as T_INT and passed the numeric gate in TryCompileNativeBoolExpr. As a result, FlLenInt was emitted outside the generated native loop, evaluating #t only once before loop entry. Mutating operations like table.remove inside the loop did not update the bound, leading to infinite loops or out-of-bounds nil access.
    • Fix: Added IsPureNativeNumericExp check in c_gen. Only pure expressions (numeric literals, local variables, and pure binary expressions) can be emitted directly as a single native while (cond) loop without external statement dependencies. Impure expressions (e.g. #t) fall back to per-iteration re-evaluation.
    • Verification: Verified on TCC, GCC, and INTERP backends with test_jitbug_while_cond_reval.lua.
  2. Bug 2 (P1-5b): Implement standard Lua 5.4 pattern matching for string library

    • Problem: string.find, match, gmatch, and gsub were backed by boost::regex ECMAScript mode. Standard Lua patterns like %.%., %d+, lazy quantifier -, frontier pattern %f[], balanced pattern %b(), and %1-%9 backreferences failed or had incorrect semantics.
    • Fix: Implemented a standalone, zero-dependency byte-pattern matcher (src/native/string/lua_pattern.cpp / lua_pattern.h) ported from Lua 5.4 lstrlib.c. Completely rewired string.find, string.match, string.gmatch, and string.gsub to use the Lua pattern engine.
    • Verification: Added comprehensive test suite test_string_lua_pattern.lua covering classes, quantifiers, captures, nested captures, frontier patterns, balanced matching, and gsub replacement forms. All 54 string unit tests pass across all backends.

Testing

  • Full unit test suite passes (test_string*, infer.test_jitbug_while_cond_reval).
  • Differential testing against PUC-Rio Lua 5.5 outputs verified on all repro scripts across TCC, GCC, and INTERP backends.

…tion (P1-9)

- P1-9: While condition purity check (IsPureNativeNumericExp):
  Prevent hoisting non-pure numeric expressions (e.g. #t table length
  which requires emitting FlLenInt statements) into single-evaluation
  outer while statements. Pure numeric expressions (literals, local variables,
  pure binary ops) continue to use native fast-path while loops.
  Add regression test test_jitbug_while_cond_reval.lua.

- P1-5b: Standard Lua 5.4 pattern matching for string.find/match/gmatch/gsub:
  Replace boost::regex ECMAScript engine with a self-contained byte-pattern
  matcher ported from Lua 5.4 lstrlib.c.
  Supports % escapes, character classes (%a, %d, %w, etc.), custom sets/ranges,
  quantifiers (*, +, -, ?), captures (), backreferences %1-%9, %b(), %f[set],
  and gsub Lua replacement templates (%0-%9, %%, function/table dispatch).
  Update tests and docs accordingly.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 93.42916% with 32 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.04%. Comparing base (b83f574) to head (e2e730b).

Files with missing lines Patch % Lines
src/compile/c_gen.cpp 77.77% 14 Missing ⚠️
src/native/string/lua_pattern.cpp 96.09% 10 Missing ⚠️
src/native/string/native_string.cpp 95.06% 8 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #24      +/-   ##
==========================================
+ Coverage   87.93%   88.04%   +0.11%     
==========================================
  Files         122      123       +1     
  Lines       24885    25216     +331     
==========================================
+ Hits        21882    22201     +319     
- Misses       3003     3015      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@esrrhs
esrrhs merged commit a55a4bf into master Oct 3, 2026
9 checks passed
@esrrhs
esrrhs deleted the fix/open-bugs branch October 3, 2026 10:09
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