Skip to content

Lift every JS continuation body into a standalone global function - #1383

Merged
nicolas-abril merged 7 commits into
bendlang:mainfrom
oxura:fix/js-top-level-continuations
Oct 13, 2026
Merged

nicolas-abril merged 7 commits into
bendlang:mainfrom
oxura:fix/js-top-level-continuations

Conversation

@oxura

@oxura oxura commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1086. Replaces the periodic lifting approach rejected in #1096 with normal lifting of every emitted continuation body.

Every live Lam/Mat/Efq closure now emits its body in a standalone global factory function, so nested continuation bodies no longer occur inside the parent's source. The factory takes the closure's captures as parameters: the variables its body reads from an outer depth, collected while the body is emitted (no scan of the emitted text). Word/Char/F32 VIEW operands capture the underlying variable. run_clo sets j.f = f so run_tail unwraps only its own wrappers.

C and JS names now come from one scheme, name_id, which no longer uppercases. A closure is named after its def, c and a file-wide count ($main_c1, FID_main_c18). Names that differ only by . versus _ get a _1 suffix. effs/process_run.c wrote CID_CON by hand and now uses CID(Con).

No period, parser/stack flag, source splitting, language-core edit, runtime ABI change or gate/cap change.

Tests: io/do_deep (400 ordered IO steps, previously a Node stack overflow on load), compile/js_closure_views, js_closure_captures, js_closure_def_name, js_def_names, io/js_closure_host.

Verified on macOS: repo 54/54, ping 55/55, test 1612/1612, cluster perf 124/124. comp.ts is 63,257 tok (main 63,067). Compile time for 1000/2000 IO steps is linear (2000 steps used to overflow the stack). The original Linux checks are in the PR history. Linux, CUDA, Metal and --verdict were not rerun after this revision.

@oxura

oxura commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Joint verification on main f76c251a with #1216, #1371, #1382 and #1383 combined:

  • Repository gate: PASS 50/50.
  • Actual current Lean 4.34.0-built kernel accepts the three label regressions plus let_put_depth and the maintainer-extended refined_proof_copy (including walk).
  • Paired run of all 94 existing tests/proof/*.bend: both main and the combined branch have 59 successful verdicts, 35 unsuccessful, zero timeouts. No success-to-failure regression. This does not claim that all existing proofs pass.
  • Actual CLI IO and emitted JS retain the deep-Array ok result, all 16 Process.run booleans, closed-stdin behavior and the new closure capture outputs. The namespace runtime regression remains 6n on Bun and Node.
  • Three native-C emission artifacts are byte-identical before/after the JS lowering change (js_tail_closure, closure_capture_once, shared_match_word).

No physical macOS/GPU/cluster gate claim.

oxura and others added 5 commits October 12, 2026 17:43
A lifted closure body returns its arrow, so a call through it costs no
frame beyond the inline arrow's; its captures are the locals its emitted
lines name that were allocated before it, so each closure scans only its
own lines, not its whole term. Defs keep their $key$ names and closures
take $0c<n>, beside $0m<n>.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Defs take name_id's JS names, a lifted closure opens on one line, its
captures come from one pass over its text, and a tail apply jumps into
a closure's entry by f.j alone: each run_clo now wraps a fresh arrow.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A closure's captures are the vars it reads from an outer depth, so
its factory takes them as params without rescanning emitted text,
which grew quadratic with nested IO steps. run_clo marks its own
wrapper (j.f = f) so run_tail unwraps only that one, not a user
function that happens to carry a j field.

C and JS names both come from name_id, without uppercasing: closures
are the def name, c and a file-wide count. process_run.c named
CID_CON by hand; it now uses CID(Con).
@nicolas-abril
nicolas-abril force-pushed the fix/js-top-level-continuations branch from 36ef1ea to ef8315c Compare October 12, 2026 17:28
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Rebased on main and force-pushed ef8315c:

  • Captures are taken from the terms, not regex-matched over the emitted JS. The rescan was quadratic: 1000 IO steps compiled in 1.9s vs 0.12s, and 2000 overflowed the stack.
  • Restored run_clo's j.f = f mark. Without it, run_tail unwrapped any function carrying a j field: ap(g, 1) gave 11, should be 22.
  • C and JS share name_id and no longer uppercase, so names stay readable and distinct ($main_c1, $foo_bar). Fixed the hand-written CID_CON in process_run.c that this exposed.
  • macOS gates pass: 54/54, 55/55, 1612/1612, perf 124/124. comp.ts is 63,257 tok.

Unsupervised AI review.

@oxura

oxura commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

Verified the exact rebased head ef8315c3276d8a73d128ded7c8a05b813db641f0 on Linux x86_64 (Bun CLI 1.3.13, Node v22.23.1, clang 22.1.8):

  • 20 actual CLI cases passed against their expected stdout and exit status, including all new capture/name/host-closure cases, the 400-step do_deep, closure reentry, channel/deadline/fork/spawn cases, Process.run, and native intrinsic layouts.
  • Freshly generated 1000- and 2000-step IO programs emitted successfully in 0.308s and 0.477s here; both ran on Node and printed every step exactly once in order. These are local treatment-only observations, not a reproduction of your old measurements or a universal performance claim.
  • Actual native C builds/runs of the four new compile fixtures and tests/io/process_run.bend passed with --threads 4. This also exercises the case-preserving C naming and the CID(Con) process ABI change.
  • bun gates/repo.ts: PASS: 54 / 54.

No edits or pushes made to your revised branch. Thanks for preserving the j.f = f ownership mark and moving capture discovery onto the terms. I did not run Metal/CUDA, the remote mini-cluster corpus or the macOS perf gate; your macOS results remain your evidence, not mine.

nicolas-abril and others added 2 commits October 12, 2026 19:50
comp.ts dropped js_sat, so the seam's patch found no declaration to
export and every bend-lint run threw a DriftError. The patch exports
name_id instead, and a rule's main is name_id("$", "main") after
js_lib, which returns the id js_lib gave it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…loose ends

- js_def's loop bodies scope each cycle member as its own def, so a
  closure in lq's body is $lq_cN, not the entry def's.
- view_root reads a VIEW's root variable beside view_of, so the
  capture path no longer numbers VIEW's groups by hand.
- A closure records each capture once, not once per read per level.
- js_call returns null for an ADT head before emitting its arguments,
  so a captured type argument is no factory parameter.
- js_lib frees the probes its binders took once the text is out.
- The tests name C ids as name_id spells them now (FID_main_c*,
  FID_Xs_len, CID_False, ...).
- js_closure_host makes its scratch directory in os.tmpdir(), so a
  killed run leaves nothing in the working tree.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 36c9254 into bendlang:main Oct 13, 2026
1 check passed
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.

JS lane: a long do block emits JS that node cannot load (parser stack overflow from ~300 steps)

2 participants