Skip to content

feat: PUC Rio correctness fixes - #57

Merged
danielsreichenbach merged 11 commits into
mainfrom
feat/puc-rio-correctness-fixes
Oct 3, 2026
Merged

danielsreichenbach merged 11 commits into
mainfrom
feat/puc-rio-correctness-fixes

Conversation

@danielsreichenbach

Copy link
Copy Markdown
Member

Pull Request

Summary

This PR fixes a handful or issues where rilua showed a different behavior compared to the original lua 5.1 implementation.

Changes

  • Fix modulo precision: use v1 - floor(v1/v2)*v2 instead of mul_add to match PUC-Rio arithmetic
  • Accept unknown escapes as themselves (\z → z) — Lua 5.1 behavior, not rejected until 5.2+
  • Reject nested [[...]] at level 0 (PUC-Rio LUA_COMPAT_LSTR == 1)
  • Reject ... outside vararg functions and main chunk
  • Byte-exact port of luaO_chunkid for source IDs — fixes truncation logic
  • Prefix builtin function errors with caller location (get_where(self, 1))
  • Clamp search init to string length in find/match — prevents underflow
  • Disable debug hooks during __gc finalizers (PUC-Rio GCTM() behavior)

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing
    functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test improvements
  • Build system/dependency changes

Tested On

  • Linux
  • macOS
  • Windows

Quality Checklist

  • Code follows project style guidelines
  • Self-review of code completed
  • Tests pass locally
  • Documentation updated (if applicable)

By submitting this PR, I confirm that:

Replace mul_add-based modulo with v1 - floor(v1/v2)*v2 to match
PUC-Rio's arithmetic and avoid fused-multiply-add precision differences.
…ssages

Lua 5.1 (llex.c) treats unrecognized escape sequences as the literal
character ('\z' is 'z', '\?' is '?'); only Lua 5.2+ rejects them.

Also quote partial string content in syntax errors to match PUC-Rio's
txtToken behavior, improving debugging for unterminated strings.
PUC-Rio 5.1 llex.c with LUA_COMPAT_LSTR == 1 rejects nesting of
[[...]] inside a level-0 long string or comment. Different delimiter
levels (e.g. [=[ ... ]=]) are still allowed.
Lua 5.1 only allows '...' in the main chunk and functions explicitly
declared with varargs. PUC-Rio checks fs->f->is_vararg; rilua was
missing this validation.
Replace heuristic chunk ID generation with a byte-exact port of
luaO_chunkid from lobject.c. Fixes truncation logic for filenames,
string sources (truncates at first newline), and literal names.
CI[0] is never a valid stack level (PUC-Rio: if level == 0 && ci >
L->base_ci). The old special-case for CI[0] was an artifact of lua.c's
lua_cpcall wrapper; with the real C API that frame exists naturally.
… compat)

Library functions that raise Runtime errors now get their message
prefixed with the calling Lua function's location, matching PUC-Rio's
luaL_where(L, 1) behavior. Excludes VM-style errors ('attempt to ...',
'loop in ...') and already-prefixed messages.
PUC-Rio's str_find_aux clamps init to the subject length: if (init >
l1) init = l1. Without this, negative indices beyond string length
cause underflow when converting to 0-based.
PUC-Rio's GCTM() disables hooks before calling __gc metamethods to
prevent re-entrancy issues. Without this, a __gc finalizer that
triggers GC could cause infinite recursion or corrupt state.
@danielsreichenbach danielsreichenbach self-assigned this Oct 3, 2026
@danielsreichenbach
danielsreichenbach merged commit 64ccc45 into main Oct 3, 2026
6 checks passed
@danielsreichenbach
danielsreichenbach deleted the feat/puc-rio-correctness-fixes branch October 3, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant