From e105edee011146b69ecfe73481707f346d09fb9a Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 24 Apr 2026 22:25:51 +0000 Subject: [PATCH] lua: method-call binds obj to temp (no more double-eval); chaining works +1 test --- lib/lua/scoreboard.json | 38 +++++++++++++++++++++----------------- lib/lua/scoreboard.md | 33 +++++++++++++++++---------------- lib/lua/test.sh | 7 +++++++ lib/lua/transpile.sx | 15 ++++++++++----- plans/lua-on-sx.md | 1 + 5 files changed, 56 insertions(+), 38 deletions(-) diff --git a/lib/lua/scoreboard.json b/lib/lua/scoreboard.json index 43c39613..d0fe953b 100644 --- a/lib/lua/scoreboard.json +++ b/lib/lua/scoreboard.json @@ -11,12 +11,16 @@ "top_failure_modes": [ [ "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - 10 + 9 ], [ "timeout", 4 ], + [ + "undefined symbol: fat\\", + 1 + ], [ "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to call non-functio", 1 @@ -39,19 +43,19 @@ "name": "attrib.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 6273 + "ms": 6224 }, { "name": "big.lua", "status": "timeout", "reason": "per-test timeout", - "ms": 8007 + "ms": 8006 }, { "name": "calls.lua", "status": "fail", - "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 5140 + "reason": "undefined symbol: fat\\", + "ms": 5070 }, { "name": "checktable.lua", @@ -63,7 +67,7 @@ "name": "closure.lua", "status": "timeout", "reason": "per-test timeout", - "ms": 8007 + "ms": 8004 }, { "name": "code.lua", @@ -87,13 +91,13 @@ "name": "errors.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 3390 + "ms": 3359 }, { "name": "events.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 7706 + "ms": 7590 }, { "name": "files.lua", @@ -111,13 +115,13 @@ "name": "literals.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 2026 + "ms": 1893 }, { "name": "locals.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"lua: attempt to call non-functio", - "ms": 1782 + "ms": 1727 }, { "name": "main.lua", @@ -129,43 +133,43 @@ "name": "math.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 4348 + "ms": 4317 }, { "name": "nextvar.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 7508 + "ms": 7303 }, { "name": "pm.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 6682 + "ms": 5956 }, { "name": "sort.lua", "status": "timeout", "reason": "per-test timeout", - "ms": 8007 + "ms": 8003 }, { "name": "strings.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 4479 + "ms": 4024 }, { "name": "vararg.lua", "status": "fail", "reason": "other: Unhandled exception: \\\"Unhandled exception: \\\\\\\"assertion failed!\\\\\\\"\\", - "ms": 2660 + "ms": 2235 }, { "name": "verybig.lua", "status": "pass", "reason": "", - "ms": 632 + "ms": 566 } ] } \ No newline at end of file diff --git a/lib/lua/scoreboard.md b/lib/lua/scoreboard.md index 0b350aa0..5c6f5a7d 100644 --- a/lib/lua/scoreboard.md +++ b/lib/lua/scoreboard.md @@ -5,8 +5,9 @@ fail=11 timeout=4 skip=8 total=24 ## Top failure modes -- **10x** other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ +- **9x** other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ - **4x** timeout +- **1x** undefined symbol: fat\ - **1x** other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to call non-functio ## Per-test results @@ -15,25 +16,25 @@ fail=11 timeout=4 skip=8 total=24 |---|---|---|---:| | all.lua | skip | driver uses dofile to chain other tests | 0 | | api.lua | skip | requires testC (C debug library) | 0 | -| attrib.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6273 | -| big.lua | timeout | per-test timeout | 8007 | -| calls.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 5140 | +| attrib.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6224 | +| big.lua | timeout | per-test timeout | 8006 | +| calls.lua | fail | undefined symbol: fat\ | 5070 | | checktable.lua | skip | internal debug helpers | 0 | -| closure.lua | timeout | per-test timeout | 8007 | +| closure.lua | timeout | per-test timeout | 8004 | | code.lua | skip | bytecode inspection via debug library | 0 | | constructs.lua | timeout | per-test timeout | 8008 | | db.lua | skip | debug library | 0 | -| errors.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3390 | -| events.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7706 | +| errors.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 3359 | +| events.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7590 | | files.lua | skip | io library | 0 | | gc.lua | skip | collectgarbage / finalisers | 0 | -| literals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 2026 | -| locals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to call non-functio | 1782 | +| literals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 1893 | +| locals.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"lua: attempt to call non-functio | 1727 | | main.lua | skip | standalone interpreter driver | 0 | -| math.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 4348 | -| nextvar.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7508 | -| pm.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 6682 | -| sort.lua | timeout | per-test timeout | 8007 | -| strings.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 4479 | -| vararg.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 2660 | -| verybig.lua | pass | - | 632 | +| math.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 4317 | +| nextvar.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 7303 | +| pm.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 5956 | +| sort.lua | timeout | per-test timeout | 8003 | +| strings.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 4024 | +| vararg.lua | fail | other: Unhandled exception: \"Unhandled exception: \\\"assertion failed!\\\"\ | 2235 | +| verybig.lua | pass | - | 566 | diff --git a/lib/lua/test.sh b/lib/lua/test.sh index 8b427201..db744837 100755 --- a/lib/lua/test.sh +++ b/lib/lua/test.sh @@ -954,6 +954,10 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 1863) (eval "(lua-eval-ast \"local i = 0 repeat i = i + 1 if i >= 3 then break end until false return i\")") +;; ── Method-call chaining (obj evaluated once) ──────────────── +(epoch 1870) +(eval "(lua-eval-ast \"local a = {x=0} function a:add(x) self.x = self.x+x return self end return a:add(10):add(20):add(30).x\")") + EPOCHS OUTPUT=$(timeout 60 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) @@ -1443,6 +1447,9 @@ check 1861 "break in for-num" '55' check 1862 "break in for-in" '30' check 1863 "break in repeat" '3' +# ── Method-call chaining ───────────────────────────────────── +check 1870 "a:add():add():add().x chain" '60' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "ok $PASS/$TOTAL Lua-on-SX tests passed" diff --git a/lib/lua/transpile.sx b/lib/lua/transpile.sx index ab7c60fa..8042d465 100644 --- a/lib/lua/transpile.sx +++ b/lib/lua/transpile.sx @@ -130,11 +130,16 @@ ((obj (lua-tx (nth node 1))) (name (nth node 2)) (args (nth node 3))) - (cons - (make-symbol "lua-call") - (cons - (list (make-symbol "lua-get") obj name) - (cons obj (map lua-tx args))))))) + (let + ((tmp (make-symbol "__obj"))) + (list + (make-symbol "let") + (list (list tmp obj)) + (cons + (make-symbol "lua-call") + (cons + (list (make-symbol "lua-get") tmp name) + (cons tmp (map lua-tx args))))))))) (define lua-tx-field diff --git a/plans/lua-on-sx.md b/plans/lua-on-sx.md index 51cb53f3..990d8e81 100644 --- a/plans/lua-on-sx.md +++ b/plans/lua-on-sx.md @@ -82,6 +82,7 @@ Each item: implement → tests → tick box → update progress log. _Newest first. Agent appends on every commit._ +- 2026-04-24: lua: scoreboard iteration — **method-call double-evaluation bug**. `lua-tx-method-call` emitted `(lua-call (lua-get OBJ name) OBJ args…)` which evaluated OBJ TWICE, so `a:add(10):add(20):add(30).x` computed `110` instead of `60` (side effects applied twice). Fixed by `(let ((__obj OBJ)) (lua-call (lua-get __obj name) __obj args…))`. 373/373 green (+1 chaining test). - 2026-04-24: lua: **🎉 FIRST PASSING PUC-Rio TEST — 1/16 runnable (6.2%)**. `verybig.lua` now passes: needed `io.output`/`io.input`/`io.stdout`/`io.stderr` stubs, made `os.remove` return `true` (test asserts on it), and added `dofile`/`loadfile` stubs. All cumulative fixes (returns/break/scoping/escapes/precedence/vararg/tonumber-trim) combined make this test's full happy path work end-to-end. 372 unit tests. Failure mix: 10× assertion / 4× timeout / 1× call-non-fn. - 2026-04-24: lua: scoreboard iteration — **proper `break` via guard+raise sentinel** (`lua-brk`) + auto-first multi-values in arith/concat. Loop break dispatch was previously a no-op (emitted bare `'lua-break-marker` symbol that nothing caught); converted to raise+catch pattern, wrapping the OUTER invocation of `_while_loop`/`_for_loop`/`_repeat_loop`/`__for_loop` in a break-guard (wrapping body doesn't work — break would just be caught and loop keeps recursing). Also `lua-arith`/`lua-concat`/`lua-concat-coerce` now `lua-first` their operands so multi-returns auto-truncate at scalar boundaries. 372/372 green (+4 break tests). Scoreboard: 10×assert / 4×timeout / 2×call-non-fn (no more undef-symbol or compare-incompat). - 2026-04-24: lua: scoreboard iteration — **proper early-return via guard+raise sentinel**. Fixes long-logged limitation: `if cond then return X end ...rest` now exits the enclosing function; `rest` is skipped. `lua-tx-return` raises `(list 'lua-ret value)`; every function body and the top-level chunk + loadstring'd chunks wrap in a guard that catches the sentinel and returns its value. Eliminates "compare incompatible types" from constructs.lua (past line 40). 368/368 green (+3 early-return tests).