js2zig-core 0.18.0

Core JS-to-Zig transpiler library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
// zigir/emit/stmt/control_flow.rs
// If, while, do-while, for, for-in, for-of, switch, and try statement emission.

use crate::zigir::emit::Emitter;
use crate::zigir::emit::helpers::EmitterHelpers;
use crate::zigir::ident::IrIdent;
use crate::zigir::types::{IrBlock, IrExpr, IrForInKind, IrForOfKind, IrStmt, IrSwitchCase};

/// Parameters for `emit_for_of_stmt`, bundled to avoid too-many-arguments.
pub(super) struct ForOfInfo<'a> {
    pub var: &'a IrIdent,
    pub destructure_vars: &'a [IrIdent],
    pub iterable: &'a IrExpr,
    pub iterable_is_arraylist: bool,
    pub body: &'a IrBlock,
    pub kind: &'a IrForOfKind,
    pub is_async: bool,
    pub label: &'a Option<String>,
}

/// Parameters for `emit_try_stmt`, bundled to avoid too-many-arguments.
pub(super) struct TryInfo<'a> {
    pub try_block: &'a IrBlock,
    pub catch_var: &'a Option<IrIdent>,
    pub catch_var_referenced: bool,
    pub finally: &'a Option<IrBlock>,
    pub has_throw: bool,
    pub has_nested_try: bool,
    pub catch_block: &'a IrBlock,
}

impl Emitter {
    /// Emit label prefix if present: `lbl: ` (used by while/do-while/for/for-in/for-of).
    fn emit_label_prefix(&mut self, label: &Option<String>) {
        if let Some(lbl) = label {
            self.write(&format!("{}: ", lbl));
        }
    }

    /// Check if a block always exits (return/throw/break/continue).
    fn block_always_exits(block: &IrBlock) -> bool {
        block.stmts.last().is_some_and(|s| {
            matches!(
                s,
                IrStmt::Return { .. }
                    | IrStmt::Throw { .. }
                    | IrStmt::Break { .. }
                    | IrStmt::Continue { .. }
            )
        })
    }

    /// Recursively check if a block contains `return` or `throw` statements.
    /// Used to detect finally blocks that can't be emitted as `defer { }`
    /// (Zig doesn't allow return/throw inside defer).
    fn block_has_return_or_throw(stmts: &[IrStmt]) -> bool {
        fn ir_stmt_has_return_or_throw(stmt: &IrStmt) -> bool {
            match stmt {
                IrStmt::Return { .. } | IrStmt::Throw { .. } => true,
                IrStmt::If { then, else_, .. } => {
                    Emitter::block_has_return_or_throw(&then.stmts)
                        || else_
                            .as_ref()
                            .is_some_and(|e| Emitter::block_has_return_or_throw(&e.stmts))
                }
                IrStmt::While { body, .. }
                | IrStmt::DoWhile { body, .. }
                | IrStmt::For { body, .. }
                | IrStmt::ForOf { body, .. }
                | IrStmt::ForIn { body, .. } => Emitter::block_has_return_or_throw(&body.stmts),
                IrStmt::Block(b) => Emitter::block_has_return_or_throw(&b.stmts),
                IrStmt::Switch { cases, .. } => cases
                    .iter()
                    .any(|c| Emitter::block_has_return_or_throw(&c.body)),
                IrStmt::Try {
                    try_block,
                    catch_block,
                    finally,
                    ..
                } => {
                    Emitter::block_has_return_or_throw(&try_block.stmts)
                        || Emitter::block_has_return_or_throw(&catch_block.stmts)
                        || finally
                            .as_ref()
                            .is_some_and(|f| Emitter::block_has_return_or_throw(&f.stmts))
                }
                _ => false,
            }
        }
        stmts.iter().any(ir_stmt_has_return_or_throw)
    }

    pub(super) fn emit_if_stmt(&mut self, cond: &IrExpr, then: &IrBlock, else_: &Option<IrBlock>) {
        self.write_indent();
        self.write("if (");
        self.emit_expr_as_bool(cond);
        self.write(") {\n");
        self.indent_push();
        self.emit_block_stmts_unlabeled(then);
        self.indent_pop();
        self.emit_else_chain(else_);
    }

    /// Recursively emit else / else-if chains.
    pub(super) fn emit_else_chain(&mut self, else_: &Option<IrBlock>) {
        if let Some(else_block) = else_ {
            // Check for else-if chain
            if else_block.stmts.len() == 1
                && let IrStmt::If {
                    cond: inner_cond,
                    then: inner_then,
                    else_: inner_else,
                } = &else_block.stmts[0]
            {
                self.write_indent();
                self.write("} else if (");
                self.emit_expr_as_bool(inner_cond);
                self.write(") {\n");
                self.indent_push();
                self.emit_block_stmts_unlabeled(inner_then);
                self.indent_pop();
                self.emit_else_chain(inner_else);
                return;
            }
            self.writeln("} else {");
            self.indent_push();
            self.emit_block_stmts_unlabeled(else_block);
            self.indent_pop();
        }
        self.writeln("}");
    }

    pub(super) fn emit_while_stmt(
        &mut self,
        cond: &IrExpr,
        body: &IrBlock,
        label: &Option<String>,
    ) {
        self.write_indent();
        self.emit_label_prefix(label);
        self.write("while (");
        self.emit_expr_as_bool(cond);
        self.write(") {\n");
        self.indent_push();
        self.emit_block_stmts_unlabeled(body);
        self.indent_pop();
        self.writeln("}");
    }

    pub(super) fn emit_do_while_stmt(
        &mut self,
        body: &IrBlock,
        cond: &IrExpr,
        label: &Option<String>,
    ) {
        // JS do-while: body always runs at least once, then cond is checked.
        // `continue` must re-evaluate cond (not jump to loop top).
        // Use a first-iteration flag as the while condition so that
        // continue → continue expr (flag=false) → re-check cond.
        //
        // P1-10: Use a unique per-iteration flag name (`__dw_first_N`) instead
        // of a hardcoded `__dw_first`. Zig 0.16 forbids local-variable
        // shadowing across nesting scopes, so an inner do-while nested in
        // the body of an outer do-while would otherwise fail ast-check
        // ("local variable '__dw_first' shadows local variable from outer
        // scope").
        let flag = self.next_do_while_flag();
        self.writeln("{");
        self.indent_push();
        self.writeln(&format!("var {flag}: bool = true;", flag = flag));
        self.write_indent();
        self.emit_label_prefix(label);
        self.write("while (");
        self.write(&format!("{flag} or (", flag = flag));
        self.emit_expr_as_bool(cond);
        self.write(&format!(")) : ({flag} = false) {{\n", flag = flag));
        self.indent_push();
        self.emit_block_stmts_unlabeled(body);
        self.indent_pop();
        self.writeln("}");
        self.indent_pop();
        self.writeln("}");
    }

    pub(super) fn emit_for_stmt(
        &mut self,
        init: &Option<Box<IrStmt>>,
        cond: &Option<IrExpr>,
        update: &Option<Box<IrStmt>>,
        body: &IrBlock,
        label: &Option<String>,
    ) {
        // Wrap the entire for loop in a block scope:
        //   { init; label: while (cond) : ({ update; }) { body } }
        // The label MUST be on the `while`, not on the enclosing block —
        // Zig `continue :label` only targets a labeled loop, and a label on
        // a plain block labels that block (which `continue` cannot target).
        self.write_indent();
        self.write("{\n");
        self.indent_push();

        // Emit init statement
        if let Some(init_stmt) = init {
            self.emit_stmt(init_stmt);
        }

        // While loop with optional update continuation
        self.write_indent();
        // Place the label directly before `while` so `continue :label`
        // (and `break :label`) target this loop rather than the wrapping block.
        self.emit_label_prefix(label);
        self.write("while (");
        if let Some(c) = cond {
            self.emit_expr_as_bool(c);
        } else {
            self.write("true");
        }
        self.write(")");

        // Zig while continuation syntax for the update expression.
        // We emit the update inline inside a block expression: `: ({ update; })`
        // NOTE: must NOT wrap in extra parentheses — `+=` is a statement in Zig,
        // and `(i += 1)` is invalid. Use statement-level emission (no parens).
        if let Some(update_stmt) = update {
            self.write(" : ({ ");
            match update_stmt.as_ref() {
                IrStmt::Expr(expr) => {
                    // For Update expressions (i++, i--), force statement-level
                    // output (no wrapping parentheses).
                    if let IrExpr::Update { op, target, .. } = expr {
                        self.emit_assign_target_inner(target);
                        self.write(&format!(
                            " {}",
                            crate::zigir::emit::helpers::update_op_to_zig(*op)
                        ));
                    } else {
                        self.emit_expr(expr);
                    }
                }
                IrStmt::Assign { target, op, value } => {
                    // Use emit_assign_stmt logic but without indent/newline
                    self.emit_assign_inline(target, *op, value);
                }
                _ => {
                    // Fallback: emit the full statement (rare)
                    self.emit_stmt(update_stmt);
                }
            }
            self.write("; })");
        }

        self.write(" {\n");
        self.indent_push();
        self.emit_block_stmts_unlabeled(body);
        self.indent_pop();

        self.write_indent();
        self.write("}\n");

        // Close the enclosing block scope
        self.indent_pop();
        self.writeln("}");
    }

    pub(super) fn emit_for_in_stmt(
        &mut self,
        var: &IrIdent,
        iterable: &IrExpr,
        body: &IrBlock,
        kind: &IrForInKind,
        label: &Option<String>,
    ) {
        match kind {
            IrForInKind::HashMapIter => {
                // `var __it = obj.iterator(); label: while (__it.next()) |__kv| { ... }`
                // Label goes directly on while (not a wrapping block) so that
                // `continue :label` targets the loop, not a block.
                self.write_indent();
                let it_name = "__it";
                self.write("var ");
                self.write(it_name);
                self.write(" = ");
                self.emit_expr(iterable);
                self.write(".iterator();\n");
                self.write_indent();
                if let Some(lbl) = label {
                    self.write(&format!("{}: ", lbl));
                }
                self.write("while (");
                self.write(it_name);
                self.write(".next()) |__kv| {\n");
                self.indent_push();
                self.writeln(&format!("const {} = __kv.key_ptr.*;", var.zig_name));
                self.emit_block_stmts_unlabeled(body);
                self.indent_pop();
                self.writeln("}");
            }
            IrForInKind::MapIter => {
                // `var __it = obj.inner.iterator(); label: while (__it.next()) |__kv| { ... }`
                self.write_indent();
                let it_name = "__it";
                self.write("var ");
                self.write(it_name);
                self.write(" = ");
                self.emit_expr(iterable);
                self.write(".inner.iterator();\n");
                self.write_indent();
                if let Some(lbl) = label {
                    self.write(&format!("{}: ", lbl));
                }
                self.write("while (");
                self.write(it_name);
                self.write(".next()) |__kv| {\n");
                self.indent_push();
                self.writeln(&format!("const {} = __kv.key_ptr.*;", var.zig_name));
                self.emit_block_stmts_unlabeled(body);
                // Suppress unused-variable error when key is not referenced in body
                self.writeln(&format!("_ = &{};", var.zig_name));
                self.indent_pop();
                self.writeln("}");
            }
            IrForInKind::StructUnroll { fields } => {
                // Unrolled: one iteration per field. When a label is present
                // we wrap ALL iterations in a single labeled block so that
                // `break :label` exits the entire for-in (previously the label
                // was attached only to the first iteration's block, so a break
                // would skip just that iteration and silently fall through to
                // the remaining ones).
                if let Some(lbl) = label {
                    self.write_indent();
                    self.write(lbl);
                    self.write(": {\n");
                    self.indent_push();
                }
                for field_name in fields.iter() {
                    self.write_indent();
                    self.write("{\n");
                    self.indent_push();
                    self.writeln(&format!("const {} = \"{}\";", var.zig_name, field_name));
                    self.emit_block_stmts_unlabeled(body);
                    self.indent_pop();
                    self.writeln("}");
                }
                if label.is_some() {
                    self.indent_pop();
                    self.writeln("}");
                }
            }
            IrForInKind::Unsupported => {
                if let Some(lbl) = label {
                    self.write_indent();
                    self.write(&format!(
                        "{}: @compileError(\"for-in on this type is not supported\");\n",
                        lbl
                    ));
                } else {
                    self.writeln("@compileError(\"for-in on this type is not supported\");");
                }
            }
        }
    }

    pub(super) fn emit_for_of_stmt(&mut self, info: &ForOfInfo<'_>) {
        let var = info.var;
        let destructure_vars = info.destructure_vars;
        let iterable = info.iterable;
        let iterable_is_arraylist = info.iterable_is_arraylist;
        let body = info.body;
        let kind = info.kind;
        let is_async = info.is_async;
        let label = info.label;

        if is_async {
            self.writeln("@compileError(\"for-await-of is not supported\");");
            return;
        }

        match kind {
            IrForOfKind::Array => {
                self.write_indent();
                self.emit_label_prefix(label);
                self.write("for (");
                self.emit_expr(iterable);
                if iterable_is_arraylist {
                    self.write(".items");
                }
                self.write(") |");
                self.write(&var.zig_name);
                self.write("| {\n");
                self.indent_push();
                self.emit_block_stmts_unlabeled(body);
                self.indent_pop();
                self.writeln("}");
            }
            IrForOfKind::MapSetIter { is_map } => {
                // `var __it = obj.inner.iterator(); label: while (__it.next()) |__kv| { ... }`
                // Label goes directly on while (not a wrapping block) so that
                // `continue :label` targets the loop, not a block.
                self.write_indent();
                let it_name = "__it";
                self.write("var ");
                self.write(it_name);
                self.write(" = ");
                self.emit_expr(iterable);
                self.write(".inner.iterator();\n");
                self.write_indent();
                if let Some(lbl) = label {
                    self.write(&format!("{}: ", lbl));
                }
                self.write("while (");
                self.write(it_name);
                self.write(".next()) |__kv| {\n");
                self.indent_push();
                if *is_map && !destructure_vars.is_empty() {
                    // Map destructure: const [key, val] = ...
                    self.writeln(&format!(
                        "const {} = __kv.key_ptr.*;",
                        destructure_vars[0].zig_name
                    ));
                    if destructure_vars.len() > 1 {
                        self.writeln(&format!(
                            "const {} = __kv.value_ptr.*;",
                            destructure_vars[1].zig_name
                        ));
                    }
                } else {
                    self.writeln(&format!("const {} = __kv.key_ptr.*;", var.zig_name));
                }
                self.emit_block_stmts_unlabeled(body);
                // Suppress unused-variable errors for destructured vars that
                // the body doesn't reference (e.g., `for (const [k, v] of m)`
                // where only v is used). Taking the address marks it as "used".
                if *is_map && !destructure_vars.is_empty() {
                    for dv in destructure_vars {
                        self.writeln(&format!("_ = &{};", dv.zig_name));
                    }
                } else {
                    self.writeln(&format!("_ = &{};", var.zig_name));
                }
                self.indent_pop();
                self.writeln("}");
            }
            IrForOfKind::Str { var_used } => {
                // JS `for (x of str)` iterates over Unicode code points (UTF-8
                // scalar values), not raw bytes. Zig's `for (str)` iterates u8
                // bytes, so we emit a std.unicode.Utf8View while-loop instead.
                self.write_indent();
                self.write("var __iter = std.unicode.Utf8View.initUnchecked(");
                self.emit_expr(iterable);
                self.writeln(").iterator();");
                self.write_indent();
                self.emit_label_prefix(label);
                self.write("while (__iter.nextCodepoint()) ");
                if *var_used {
                    // Bind to a temp var, then cast u21 → i64 for the user-facing name.
                    self.write("|__cp| {\n");
                    self.indent_push();
                    self.writeln(&format!("const {} = @as(i64, __cp);", var.zig_name));
                } else {
                    // Unused capture: use |_| to avoid Zig 0.16 unused-capture error.
                    self.write("|_| {\n");
                    self.indent_push();
                }
                self.emit_block_stmts_unlabeled(body);
                self.indent_pop();
                self.writeln("}");
            }
            IrForOfKind::AsyncUnsupported => {
                self.writeln("@compileError(\"for-await-of is not supported\");");
            }
        }
    }

    pub(super) fn emit_switch_stmt(&mut self, expr: &IrExpr, cases: &[IrSwitchCase]) {
        // Detect a string-keyed switch: Zig's `switch` cannot operate on
        // []const u8 (only integers/enums/bools/union tags). If any case's
        // test lowers to `IrExpr::StringLiteral`, lower the whole switch as
        // an `if`/`else if` chain using `std.mem.eql(u8, ...)` (R6-2).
        let is_string_switch = cases.iter().any(|c| {
            c.test
                .as_ref()
                .is_some_and(|t| matches!(t, crate::zigir::types::IrExpr::StringLiteral(_)))
        });
        if is_string_switch {
            self.emit_string_switch(expr, cases);
            return;
        }

        self.write_indent();
        self.write("switch (");
        self.emit_expr(expr);
        self.write(") {\n");
        self.indent_push();
        let mut has_default = false;
        for case in cases {
            self.write_indent();
            if let Some(test) = &case.test {
                self.emit_expr(test);
            } else {
                has_default = true;
                self.write("else");
            }
            self.write(" => {\n");
            self.indent_push();
            for stmt in &case.body {
                self.emit_stmt(stmt);
            }
            self.indent_pop();
            self.write_indent();
            self.write("},\n");
        }
        // Zig switch must be exhaustive; add empty else if no default
        if !has_default {
            self.writeln("else => {},");
        }
        self.indent_pop();
        self.writeln("}");
    }

    /// Emit a string-keyed switch as an if/else-if chain using std.mem.eql.
    /// Used when any case test is a `StringLiteral` — Zig's `switch` cannot
    /// operate on `[]const u8`.
    fn emit_string_switch(&mut self, expr: &IrExpr, cases: &[IrSwitchCase]) {
        // Bind the discriminant to a const so it is evaluated only once,
        // avoiding repeated side-effects if the expression is non-trivial.
        // `_ = &__sw;` suppresses "unused variable" when all cases are default.
        self.write_indent();
        self.write("const __sw = ");
        self.emit_expr(expr);
        self.write(";\n");
        self.write_indent();
        self.write("_ = &__sw;\n");

        let mut written_first = false;
        let mut has_default = false;
        for case in cases {
            match &case.test {
                Some(test @ crate::zigir::types::IrExpr::StringLiteral(_)) => {
                    self.write_indent();
                    if !written_first {
                        self.write("if (");
                        written_first = true;
                    } else {
                        self.write("} else if (");
                    }
                    self.write("std.mem.eql(u8, __sw, ");
                    self.emit_expr(test);
                    self.write(")) {\n");
                    // Body must be emitted INSIDE the string-literal arm —
                    // otherwise the default case body leaks into the previous
                    // case's scope (R6-2 follow-up: unreachable-code ast error).
                    self.indent_push();
                    for stmt in &case.body {
                        self.emit_stmt(stmt);
                    }
                    self.indent_pop();
                }
                _ => {
                    // default (test == None) — collect for the trailing else.
                    // Body is emitted later from the `)} else {` block below.
                    has_default = true;
                }
            }
        }
        if written_first {
            self.write_indent();
            if has_default {
                self.write("} else {\n");
                self.indent_push();
                // find default body
                for case in cases {
                    if case.test.is_none() {
                        for stmt in &case.body {
                            self.emit_stmt(stmt);
                        }
                    }
                }
                self.indent_pop();
                self.write_indent();
                self.write("}\n");
            } else {
                self.write("}\n");
            }
        } else if has_default {
            // All cases were default (no string tests) — just emit the default body inline.
            for case in cases {
                if case.test.is_none() {
                    for stmt in &case.body {
                        self.emit_stmt(stmt);
                    }
                }
            }
        }
    }

    pub(super) fn emit_try_stmt(&mut self, info: &TryInfo<'_>) {
        let try_block = info.try_block;
        let catch_var = info.catch_var;
        let catch_var_referenced = info.catch_var_referenced;
        let finally = info.finally;
        let has_throw = info.has_throw;
        let has_nested_try = info.has_nested_try;
        let catch_block = info.catch_block;

        let needs_catch = catch_var.is_some();

        // ── B1: No throw, no catch, no nested try → inline body + finally ──
        // Also applies when: no throw AND body always exits (catch is unreachable)
        if !has_throw && (!needs_catch || Self::block_always_exits(try_block)) && !has_nested_try {
            // Emit finally as defer BEFORE the body so it always runs,
            // even if the body contains return/break/continue.
            if let Some(finally_block) = finally {
                if Self::block_has_return_or_throw(&finally_block.stmts) {
                    self.writeln(
                        "@compileError(\"return/throw in finally block is not yet supported\");",
                    );
                } else {
                    self.writeln("defer {");
                    self.indent_push();
                    self.emit_block_stmts_unlabeled(finally_block);
                    self.indent_pop();
                    self.writeln("}");
                }
            }
            self.emit_block_stmts_unlabeled(try_block);
            return;
        }

        // ── Case A: Has catch (and catch may be reached), throw, or nested try
        // → full labeled block pattern ──
        // Double labeled-block pattern:
        //   outer blk (_js_try_blk_N): scope for defer (finally) + catch dispatch
        //   inner body blk (_js_try_body_blk_N): scope for throw → break to catch
        let label_id = self.next_try_label();
        let blk_label = format!("_js_try_blk_{}", label_id);
        let result_var = format!("_js_try_{}", label_id);
        let body_blk_label = format!("_js_try_body_blk_{}", label_id);
        let body_result_var = format!("_js_try_body_{}", label_id);

        // Save current inside_try_block so we can restore it later
        let saved_inside = self.inside_try_block.clone();

        // ── Outer labeled block ──
        self.write_indent();
        self.write(&format!(
            "const {}: anyerror!void = {}: {{\n",
            result_var, blk_label,
        ));
        self.indent_push();

        // ── Finally as defer (always runs, inside labeled block) ──
        if let Some(finally_block) = finally {
            if Self::block_has_return_or_throw(&finally_block.stmts) {
                self.writeln(
                    "@compileError(\"return/throw in finally block is not yet supported\");",
                );
            } else {
                self.writeln("defer {");
                self.indent_push();
                self.emit_block_stmts_unlabeled(finally_block);
                self.indent_pop();
                self.writeln("}");
            }
        }

        // ── Inner body labeled block ──
        self.write_indent();
        self.write(&format!(
            "const {}: anyerror!void = {}: {{\n",
            body_result_var, body_blk_label,
        ));
        self.indent_push();

        // Set inside_try_block so that throw/break statements emit
        // break :body_blk_label or break :blk_label
        self.inside_try_block = Some(body_blk_label.clone());

        self.emit_block_stmts_unlabeled(try_block);

        // Normal completion of try body (no throw).
        // Skip if the try body ends with a return/break/continue/throw —
        // execution never reaches this point.
        let body_always_exits = Self::block_always_exits(try_block);
        if !body_always_exits {
            self.write_indent();
            self.write(&format!("break :{} {{}};\n", body_blk_label));
        }

        self.indent_pop();
        self.writeln("};");

        // ── Catch dispatch ──
        if needs_catch {
            self.writeln(&format!("if ({}) |_| {{", body_result_var));
            // success path: nothing to do
            if catch_var_referenced {
                self.writeln("} else |__catch_err| {");
            } else {
                self.writeln("} else |_| {");
            }
            self.indent_push();
            if let Some(var) = catch_var
                && catch_var_referenced
            {
                self.writeln(&format!(
                    "const {} = js_error.JsError.fromError(__catch_err, js_allocator.allocator()) catch @panic(\"OOM: JsError\");",
                    var.zig_name
                ));
            }
            // When !catch_var_referenced: catch capture is |_| (discarded).
            // Set inside_try_block to outer label so re-throw in catch body
            // produces break :blk_label error.JsThrow
            self.inside_try_block = Some(blk_label.clone());
            self.emit_block_stmts_unlabeled(catch_block);
            self.indent_pop();
            self.writeln("}");
        }

        // Normal completion of outer block
        // When `needs_catch == false` but `has_throw == true` (try-finally with
        // no catch handler that can throw), propagate the inner body's error
        // result to the outer block. Without this, a thrown error in the body
        // is silently swallowed (R6-1).
        self.write_indent();
        if !needs_catch && has_throw {
            self.write(&format!(
                "break :{} if ({}) |_| @as(anyerror!void, {{}}) else |_| @as(anyerror!void, error.JsThrow);\n",
                blk_label, body_result_var,
            ));
        } else {
            self.write(&format!("break :{} {{}};\n", blk_label));
        }

        self.indent_pop();
        self.writeln("};");

        // ── Propagate unhandled re-throw ──
        // If the result is an error (re-throw from catch, or a throw that
        // escaped the body when there is no catch handler), propagate it.
        // Pre-fix: guarded by `if needs_catch`, which meant a try-finally
        // with NO catch handler silently dropped any throw from the body
        // (body_result_var was error.JsThrow but never inspected).
        // Post-fix: propagate whenever `needs_catch || has_throw`.
        if needs_catch || has_throw {
            if let Some(ref parent_label) = saved_inside {
                // Inside another try block → break to parent
                self.writeln(&format!(
                    "if ({0}) |_| {{}} else |_| break :{1} @as(anyerror!void, error.JsThrow);",
                    result_var, parent_label,
                ));
            } else {
                // Top-level → return error
                self.writeln(&format!(
                    "if ({}) |_| {{}} else |_| return error.JsThrow;",
                    result_var,
                ));
            }
        }

        // Restore saved inside_try_block
        self.inside_try_block = saved_inside;
    }
}