Skip to content

fix(transpiler): trailing var returns, receiver history reads, switch na fallback - #319

Open
yusufcansehirli wants to merge 1 commit into
LuxAlgo:devfrom
yusufcansehirli:fix/var-return-history-reads-switch-na
Open

yusufcansehirli wants to merge 1 commit into
LuxAlgo:devfrom
yusufcansehirli:fix/var-return-history-reads-switch-na

Conversation

@yusufcansehirli

Copy link
Copy Markdown

Three codegen fixes (same batch as #318)

All three reproduced with public Library studies running through the engine.

1. A trailing var declaration is not returned

f() => var fibs = array.from(...) returns undefined: the declaration is emitted but no implicit return is generated, so callers doing f().get(i) crash with Cannot read properties of undefined (reading 'get').
Fix: keep the declaration (hoisting/series registration) and append an implicit return <name>.

2. History reads in receiver chains stay raw

(arr[2]).get(i) inside an operand emitted $$.let.arr[2].get(i) — a raw bracket on the Series wrapper object (undefined at runtime). The same expression in a ternary consequent lowered correctly to $.get($$.let.arr, 2).get(i).
Fix: resolveCalleeObject now lowers computed receivers to $.get(obj, offset); transformOperand does the same for bare computed member operands.

3. Unmatched switch expression returns undefined

A discriminated switch expression with no matching arm (or no default case) resolved to undefined, crashing tuple destructuring on [0]. Pine semantics: na.
Fix: both switch-IIFE generators append a return na; fallback when there is no default arm, and tuple-destructuring element reads use optional chaining ($.get(temp, 0)?.[n]) so an na value resolves instead of crashing.

Verification

  • Targeted probes: trailing-var returns the array (size() == 3); unmatched-switch destructure runs; operand history read yields a finite number (no NaN).
  • tests/transpiler/ — 504 tests pass (one snapshot updated for the ?.[n] reads).
  • 13 previously-failing Library sources swept through the real engine: 11/13 OK (2 remaining failures are source-side artifacts unrelated to the engine).
  • Isolated re-runs of the touched areas are clean; intermittent streaming/timing test flakes reproduce on unpatched main in this environment.

…s in receiver chains, na-fallback for unmatched switches

- Implicit return: a trailing `var x = expr` is the function's value; keep
  the declaration and append `return x`. Previously such functions returned
  undefined and callers crashed on array ops against the result.
- Receiver chains: `(arr[2]).get(i)` emitted raw `$$.let.arr[2].get(i)` —
  indexing the Series wrapper object itself (undefined at runtime).
  resolveCalleeObject now lowers computed receivers to `$.get(arr, 2)`.
- Switch expressions without a default arm return `na` on no-match instead
  of undefined; tuple-destructuring element reads use optional chaining so
  unmatched arms resolve to na instead of crashing on `[0]`.
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.

1 participant