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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
use crate::bundled_ast::Flags as AstFlags;
use crate::mal_prelude::*;
use bun_alloc::Arena as Bump; // bumpalo::Bump re-export (AST crate: arenas are load-bearing)
use bun_alloc::ArenaVecExt as _;
use bun_collections::{BoundedArray, VecExt};
use bun_js_printer::renamer;
use bun_js_printer::{self as js_printer, PrintResult, PrintResultSuccess};
use crate::generic_path_with_pretty_initialized;
use crate::linker_context_mod::{StmtList, StmtListWhich};
use crate::options::Format as OutputFormat;
use crate::{Chunk, DeclInfo, DeclInfoKind, Index, LinkerContext, Part, PartRange, WrapKind};
use bun_ast::StoreRef;
use bun_ast::binding::ToExprWrapper;
use bun_ast::{B, Binding, E, Expr, G, Ref, S, Stmt};
use bun_js_parser::lexer as js_lexer;
use super::convert_stmts_for_chunk::convert_stmts_for_chunk;
use super::convert_stmts_for_chunk_for_dev_server::convert_stmts_for_chunk_for_dev_server;
// PORT NOTE: MultiArrayList column access — Zig `list.items(.field)` is mapped here as
// `list.items_field()` method calls (codegen'd accessors on the SoA wrappers).
#[allow(clippy::too_many_arguments)]
pub fn generate_code_for_file_in_chunk_js<'r, 'src>(
c: &mut LinkerContext,
writer: &mut js_printer::BufferWriter,
r: renamer::Renamer<'r, 'src>,
chunk: &mut Chunk,
part_range: PartRange,
to_common_js_ref: Ref,
to_esm_ref: Ref,
runtime_require_ref: Option<Ref>,
stmts: &mut StmtList,
arena: &Bump,
temp_arena: &Bump,
decl_collector: Option<&mut DeclCollector>,
) -> js_printer::PrintResult {
let source_index = part_range.source_index.get() as usize;
// PORT NOTE: reshaped for borrowck — grab raw pointers to the SoA columns up front so
// subsequent `&mut c` borrows (convert_stmts_for_chunk, print_code_for_file_in_chunk_js)
// don't conflict. Matches Zig which slices once at the top.
// SAFETY: the underlying MultiArrayList storage is not resized for the duration of this
// function (linking has already sized everything).
let parts: *mut [Part] = {
let list = &mut c.graph.ast.items_parts_mut()[source_index];
core::ptr::addr_of_mut!(
list.as_mut_slice()
[part_range.part_index_begin as usize..part_range.part_index_end as usize]
)
};
let flags: crate::js_meta::Flags = c.graph.meta.items_flags()[source_index];
let wrapper_part_index = if flags.wrap != WrapKind::None {
c.graph.meta.items_wrapper_part_index()[source_index]
} else {
Index::INVALID
};
// referencing everything by array makes the code a lot more annoying :(
//
// PORT NOTE: `MultiArrayList::get` returns `ManuallyDrop<BundledAst>` — the
// storage retains ownership of every Drop field (`parts`, `symbols`,
// `named_imports`, …). The local `flags` mutation below is intentional and
// stays scoped to this read view.
let mut ast = c.graph.ast.get(source_index);
// For HMR, part generation is entirely special cased.
// - export wrapping is already done.
// - imports are split from the main code.
// - one part range per file
if c.options.output_format == OutputFormat::InternalBakeDev {
'brk: {
if part_range.source_index.is_runtime() {
// PERF(port): was @branchHint(.cold)
debug_assert!(c.dev_server.is_none());
break 'brk; // this is from `bun build --format=internal_bake_dev`
}
let hmr_api_ref = ast.wrapper_ref;
// SAFETY: see `parts` raw-pointer note above.
for part in unsafe { (*parts).iter() } {
let part_stmts: &[Stmt] = part.stmts.slice();
if let Err(err) =
convert_stmts_for_chunk_for_dev_server(c, stmts, part_stmts, arena, &mut ast)
{
return PrintResult::Err(err.into());
}
}
let main_stmts_len =
stmts.inside_wrapper_prefix.stmts.len() + stmts.inside_wrapper_suffix.len();
let all_stmts_len = main_stmts_len + stmts.outside_wrapper_prefix.len() + 1;
stmts.all_stmts.reserve(all_stmts_len);
// PERF(port): was appendSliceAssumeCapacity
stmts
.all_stmts
.extend_from_slice(stmts.inside_wrapper_prefix.stmts.as_slice());
stmts
.all_stmts
.extend_from_slice(stmts.inside_wrapper_suffix.as_slice());
// PORT NOTE: reshaped for borrowck — capture pointer/len now, re-slice after pushes.
// SAFETY: `inner` aliases the first `main_stmts_len` elements of `all_stmts`;
// subsequent pushes only append past this range and capacity was reserved above
// so no reallocation occurs. Matches Zig which slices then continues appending.
let inner = bun_ast::StoreSlice::new_mut(
&mut stmts.all_stmts.as_mut_slice()[0..main_stmts_len],
);
let mut clousure_args: BoundedArray<G::Arg, 3> = BoundedArray::default();
clousure_args.append_assume_capacity(G::Arg {
binding: Binding::alloc(
temp_arena,
B::Identifier { r#ref: hmr_api_ref },
bun_ast::Loc::EMPTY,
),
..Default::default()
});
if ast
.flags
.intersects(AstFlags::USES_MODULE_REF | AstFlags::USES_EXPORTS_REF)
{
clousure_args.append_assume_capacity(G::Arg {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.module_ref,
},
bun_ast::Loc::EMPTY,
),
..Default::default()
});
clousure_args.append_assume_capacity(G::Arg {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.exports_ref,
},
bun_ast::Loc::EMPTY,
),
..Default::default()
});
}
// PERF(port): was temp_arena.dupe — `G::Arg` is not Copy in Rust
let dup_args: &mut [G::Arg] = {
let mut v = bun_alloc::ArenaVec::with_capacity_in(
clousure_args.const_slice().len(),
temp_arena,
);
for a in clousure_args.slice().iter_mut() {
v.push(core::mem::take(a));
}
v.into_bump_slice_mut()
};
// PERF(port): was appendAssumeCapacity
stmts.all_stmts.push(Stmt::allocate_expr(
temp_arena,
Expr::init(
E::Function {
func: G::Fn {
args: bun_ast::StoreSlice::new_mut(dup_args),
body: G::FnBody {
stmts: inner,
loc: bun_ast::Loc::EMPTY,
},
..Default::default()
},
},
bun_ast::Loc::EMPTY,
),
));
// PERF(port): was appendSliceAssumeCapacity
stmts
.all_stmts
.extend_from_slice(stmts.outside_wrapper_prefix.as_slice());
ast.flags.insert(AstFlags::USES_MODULE_REF);
// TODO: there is a weird edge case where the pretty path is not computed
// it does not reproduce when debugging.
let source_ref = c.get_source(source_index as u32);
// PORT NOTE: reshaped for borrowck — Zig copies the `Source` by value,
// mutates `.path`, and passes `&source`. `bun_ast::Source` is not `Clone`
// (its `Cow` fields would deep-copy `Owned` data); instead, build a
// borrowed-field shadow only when the path needs fixing.
let source_storage: bun_ast::Source;
let source: &bun_ast::Source = if core::ptr::eq(
source_ref.path.text.as_ptr(),
source_ref.path.pretty.as_ptr(),
) {
let top_level_dir = bun_resolver::fs::FileSystem::get().top_level_dir;
let new_path = bun_core::handle_oom(generic_path_with_pretty_initialized(
&source_ref.path,
c.options.target,
top_level_dir,
arena,
));
source_storage = bun_ast::Source {
path: new_path,
// SAFETY: `source_ref` is `&'static Source`, so re-borrowing its
// `Cow` payloads as `&'static [u8]` is sound regardless of arm.
contents: std::borrow::Cow::Borrowed(unsafe {
&*std::ptr::from_ref::<[u8]>(source_ref.contents.as_ref())
}),
contents_is_recycled: source_ref.contents_is_recycled,
// SAFETY: `source_ref` is `&'static Source`, so re-borrowing its
// `Cow` payload as `&'static [u8]` is sound regardless of arm.
identifier_name: std::borrow::Cow::Borrowed(unsafe {
&*std::ptr::from_ref::<[u8]>(source_ref.identifier_name.as_ref())
}),
index: source_ref.index,
};
&source_storage
} else {
source_ref
};
return c.print_code_for_file_in_chunk_js(
r,
arena,
writer,
&mut stmts.all_stmts[main_stmts_len..],
&ast,
flags,
Ref::NONE,
Ref::NONE,
None,
part_range.source_index,
source,
);
}
}
let mut needs_wrapper = false;
let namespace_export_part_index = bun_ast::NAMESPACE_EXPORT_PART_INDEX;
stmts.reset();
let part_index_for_lazy_default_export: u32 = 'brk: {
if ast.flags.contains(AstFlags::HAS_LAZY_EXPORT) {
if let Some(default) =
c.graph.meta.items_resolved_exports()[source_index].get(b"default")
{
break 'brk c
.graph
.top_level_symbol_to_parts(source_index as u32, default.data.import_ref)[0];
}
}
u32::MAX
};
let output_format = c.options.output_format;
// The top-level directive must come first (the non-wrapped case is handled
// by the chunk generation code, although only for the entry point)
if flags.wrap != WrapKind::None
&& ast
.flags
.contains(AstFlags::HAS_EXPLICIT_USE_STRICT_DIRECTIVE)
&& !chunk.is_entry_point()
&& !output_format.is_always_strict_mode()
{
stmts
.inside_wrapper_prefix
.append_non_dependency(Stmt::alloc(
S::Directive {
value: bun_ast::StoreStr::new(b"use strict"),
},
bun_ast::Loc::EMPTY,
))
.expect("unreachable");
}
// `convert_stmts_for_chunk` takes `&mut c` inside the loop body, so capture
// the per-file bitset as a BackRef once. The `parts_live` Vec doesn't
// reallocate after `tree_shaking_and_code_splitting` initializes it.
let parts_live: bun_ptr::BackRef<bun_collections::AutoBitSet> =
bun_ptr::BackRef::new(&c.graph.parts_live[source_index]);
// TODO: handle directive
if namespace_export_part_index >= part_range.part_index_begin
&& namespace_export_part_index < part_range.part_index_end
&& parts_live.is_set(namespace_export_part_index as usize)
{
// SAFETY: see `parts` raw-pointer note above; index bounded by the range check just above.
let ns_part_stmts: &[Stmt] =
unsafe { (*parts)[namespace_export_part_index as usize].stmts }.slice();
if let Err(err) = convert_stmts_for_chunk(
c,
source_index as u32,
stmts,
ns_part_stmts,
chunk,
temp_arena,
flags.wrap,
&ast,
) {
// TODO(port): bun.handleErrorReturnTrace — no Rust equivalent
return PrintResult::Err(err);
}
match flags.wrap {
WrapKind::Esm => {
// PORT NOTE: reshaped for borrowck — append_slice borrows `stmts` mutably while
// also reading from a sibling field.
let suffix = core::mem::take(&mut stmts.inside_wrapper_suffix);
stmts.append_slice(StmtListWhich::OutsideWrapperPrefix, suffix.as_slice());
stmts.inside_wrapper_suffix = suffix;
}
_ => {
let suffix = core::mem::take(&mut stmts.inside_wrapper_suffix);
stmts
.inside_wrapper_prefix
.append_non_dependency_slice(suffix.as_slice())
.expect("unreachable");
stmts.inside_wrapper_suffix = suffix;
}
}
stmts.inside_wrapper_suffix.clear();
}
// Add all other parts in this chunk
// SAFETY: see `parts` raw-pointer note above.
let parts_len = unsafe { (&*parts).len() };
for index_ in 0..parts_len {
let index = part_range.part_index_begin + (index_ as u32);
if !parts_live.is_set(index as usize) {
// Skip the part if it's not in this chunk
continue;
}
// SAFETY: index in bounds.
let part: &Part = unsafe { &(*parts)[index_] };
if index == namespace_export_part_index {
// Skip the namespace export part because we already handled it above
continue;
}
if index == wrapper_part_index.get() {
// Skip the wrapper part because we already handled it above
needs_wrapper = true;
continue;
}
let mut single_stmts_list: [Stmt; 1] = [Stmt::empty()];
let mut part_stmts: &[Stmt] = part.stmts.slice();
// If this could be a JSON or TOML file that exports a top-level object literal, go
// over the non-default top-level properties that ended up being imported
// and substitute references to them into the main top-level object literal.
// So this JSON file:
//
// {
// "foo": [1, 2, 3],
// "bar": [4, 5, 6],
// }
//
// is initially compiled into this:
//
// export var foo = [1, 2, 3];
// export var bar = [4, 5, 6];
// export default {
// foo: [1, 2, 3],
// bar: [4, 5, 6],
// };
//
// But we turn it into this if both "foo" and "default" are imported:
//
// export var foo = [1, 2, 3];
// export default {
// foo,
// bar: [4, 5, 6],
// };
//
if index == part_index_for_lazy_default_export {
debug_assert!(index != u32::MAX);
let stmt = part_stmts[0];
let StmtData::SExportDefault(default_export) = stmt.data else {
panic!("expected Lazy default export to be an export default statement");
};
let mut default_expr = match &default_export.value {
bun_ast::StmtOrExpr::Expr(e) => *e,
bun_ast::StmtOrExpr::Stmt(_) => {
panic!("expected Lazy default export value to be an expression")
}
};
// Be careful: the top-level value in a JSON file is not necessarily an object
if let ExprData::EObject(e_object) = default_expr.data {
// PORT NOTE: Zig `properties.clone(temp_arena)` is a memcpy into the
// temp arena. `G::Property` is not `Clone` (it embeds a `Vec`), so
// mirror the Zig bitwise copy directly. JSON object properties carry no
// owned heap data (`ts_decorators` is always empty, `class_static_block`
// is `None`), so the duplicated bits do not alias any allocation.
let src_len = e_object.properties.len();
let mut new_properties = Vec::<G::Property>::init_capacity(src_len);
// SAFETY: `new_properties` has capacity `src_len`; source slice is live
// arena memory of length `src_len`; see note above re: no owned heap data.
unsafe {
core::ptr::copy_nonoverlapping(
e_object.properties.as_ptr(),
new_properties.as_mut_ptr(),
src_len,
);
new_properties.set_len((src_len as u32) as usize);
}
let resolved_exports = &c.graph.meta.items_resolved_exports()[source_index];
// If any top-level properties ended up being imported directly, change
// the property to just reference the corresponding variable instead
for prop in new_properties.slice_mut() {
if prop.key.is_none()
|| !matches!(
prop.key.as_ref().expect("infallible: prop has key").data,
ExprData::EString(_)
)
|| prop.value.is_none()
{
continue;
}
let name = match &mut prop.key.as_mut().unwrap().data {
ExprData::EString(s) => s.slice(temp_arena),
_ => unreachable!(),
};
if name == b"default" || name == b"__esModule" || !js_lexer::is_identifier(name)
{
continue;
}
if let Some(export_data) = resolved_exports.get(name) {
let export_ref = export_data.data.import_ref;
let part_idx = c
.graph
.top_level_symbol_to_parts(source_index as u32, export_ref)[0]
as usize;
if parts_live.is_set(part_idx) {
// PTR_AUDIT(#1): `*prop` is a bitwise copy of
// `e_object.properties[i]` (see `copy_nonoverlapping`
// above). A plain `*prop = …` would run `Drop` on the
// aliased old value — specifically `prop.ts_decorators:
// Vec<Expr>`, which (if non-empty) would free the
// *original AST's* allocation. The "JSON ⇒ ts_decorators
// empty" invariant makes that drop a no-op today, but
// `ptr::write` enforces it structurally.
let key = prop.key;
let value_loc =
prop.value.as_ref().expect("infallible: prop has value").loc;
// SAFETY: `prop` is a valid `&mut G::Property` slot;
// the overwritten old value aliases AST-owned data and
// MUST NOT be dropped (PTR_AUDIT.md class #1).
unsafe {
core::ptr::write(
prop,
G::Property {
key,
value: Some(Expr::init_identifier(export_ref, value_loc)),
..Default::default()
},
);
}
}
}
}
default_expr = Expr::allocate(
temp_arena,
E::Object {
properties: Vec::move_from_list(new_properties),
..Default::default()
},
default_expr.loc,
);
}
single_stmts_list[0] = Stmt::allocate(
temp_arena,
S::ExportDefault {
default_name: default_export.default_name,
value: bun_ast::StmtOrExpr::Expr(default_expr),
},
stmt.loc,
);
part_stmts = &single_stmts_list[..];
}
if let Err(err) = convert_stmts_for_chunk(
c,
source_index as u32,
stmts,
part_stmts,
chunk,
temp_arena,
flags.wrap,
&ast,
) {
return PrintResult::Err(err);
}
}
// Hoist all import statements before any normal statements. ES6 imports
// are different than CommonJS imports. All modules imported via ES6 import
// statements are evaluated before the module doing the importing is
// evaluated (well, except for cyclic import scenarios). We need to preserve
// these semantics even when modules imported via ES6 import statements end
// up being CommonJS modules.
stmts
.all_stmts
.reserve(stmts.inside_wrapper_prefix.stmts.len() + stmts.inside_wrapper_suffix.len());
// PERF(port): was appendSliceAssumeCapacity
stmts
.all_stmts
.extend_from_slice(stmts.inside_wrapper_prefix.stmts.as_slice());
stmts
.all_stmts
.extend_from_slice(stmts.inside_wrapper_suffix.as_slice());
stmts.inside_wrapper_prefix.reset();
stmts.inside_wrapper_suffix.clear();
if c.options.minify_syntax {
merge_adjacent_local_stmts(&mut stmts.all_stmts, temp_arena);
}
let mut out_stmts = bun_ast::StoreSlice::new_mut(stmts.all_stmts.as_mut_slice());
// Optionally wrap all statements in a closure
if needs_wrapper {
match flags.wrap {
WrapKind::Cjs => {
// Only include the arguments that are actually used
let mut args: bun_alloc::ArenaVec<'_, G::Arg> =
bun_alloc::ArenaVec::with_capacity_in(
if ast
.flags
.intersects(AstFlags::USES_MODULE_REF | AstFlags::USES_EXPORTS_REF)
{
2
} else {
0
},
temp_arena,
);
if ast
.flags
.intersects(AstFlags::USES_MODULE_REF | AstFlags::USES_EXPORTS_REF)
{
// PERF(port): was appendAssumeCapacity
args.push(G::Arg {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.exports_ref,
},
bun_ast::Loc::EMPTY,
),
..Default::default()
});
if ast.flags.contains(AstFlags::USES_MODULE_REF) {
// PERF(port): was appendAssumeCapacity
args.push(G::Arg {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.module_ref,
},
bun_ast::Loc::EMPTY,
),
..Default::default()
});
}
}
// TODO: variants of the runtime functions
let body_stmts = bun_ast::StoreSlice::new_mut(stmts.all_stmts.as_mut_slice());
let cjs_args = Vec::<Expr>::from_slice(&[Expr::init(
E::Arrow {
args: bun_ast::StoreSlice::new(args.into_bump_slice()),
body: G::FnBody {
stmts: body_stmts,
loc: bun_ast::Loc::EMPTY,
},
..Default::default()
},
bun_ast::Loc::EMPTY,
)]);
let commonjs_wrapper_definition = Expr::init(
E::Call {
target: Expr::init(
E::Identifier {
ref_: c.cjs_runtime_ref,
..Default::default()
},
bun_ast::Loc::EMPTY,
),
args: Vec::move_from_list(cjs_args),
..Default::default()
},
bun_ast::Loc::EMPTY,
);
// "var require_foo = __commonJS(...);"
{
let decls = G::DeclList::from_slice(&[G::Decl {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.wrapper_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(commonjs_wrapper_definition),
}]);
stmts.append(
StmtListWhich::OutsideWrapperPrefix,
Stmt::alloc(
S::Local {
decls,
..Default::default()
},
bun_ast::Loc::EMPTY,
),
);
}
}
WrapKind::Esm => {
// The wrapper only needs to be "async" if there is a transitive async
// dependency. For correctness, we must not use "async" if the module
// isn't async because then calling "require()" on that module would
// swallow any exceptions thrown during module initialization.
let is_async = flags.is_async_or_has_async_dependency;
struct ExportHoist {
decls: Vec<G::Decl>,
// BackRef: the arena is the caller's `temp_arena: &Bump`,
// which strictly outlives this local helper struct.
arena: bun_ptr::BackRef<Bump>,
}
impl ExportHoist {
fn wrap_identifier(&mut self, loc: bun_ast::Loc, ref_: Ref) -> Expr {
// Copy the BackRef so the `&Bump` borrow is detached
// from `&mut self` (needed for `self.decls.push`).
let arena = self.arena;
self.decls.push(G::Decl {
binding: Binding::alloc(
arena.get(),
B::Identifier { r#ref: ref_ },
loc,
),
value: None,
});
Expr::init_identifier(ref_, loc)
}
/// Trampoline matching `ToExprWrapper`'s erased fn-pointer signature.
fn wrap_trampoline(
ctx: *mut core::ffi::c_void,
loc: bun_ast::Loc,
ref_: Ref,
) -> Expr {
// SAFETY: `ctx` is `&mut ExportHoist` derived at the call site.
let this = unsafe { bun_ptr::callback_ctx::<ExportHoist>(ctx) };
this.wrap_identifier(loc, ref_)
}
}
let mut hoist = ExportHoist {
decls: Vec::new(),
arena: bun_ptr::BackRef::new(temp_arena),
};
let hoist_wrapper = ToExprWrapper::new(temp_arena, ExportHoist::wrap_trampoline);
let mut inner_stmts = bun_ast::StoreSlice::new_mut(stmts.all_stmts.as_mut_slice());
// Hoist all top-level "var" and "function" declarations out of the closure
{
let mut end: usize = 0;
// PORT NOTE: reshaped for borrowck — iterate by index since we mutate
// `inner_stmts[end]` and call `stmts.append(...)` inside the loop.
'hoist: for i in 0..stmts.all_stmts.len() {
let stmt = stmts.all_stmts[i];
let transformed = match stmt.data {
StmtData::SLocal(local) => 'stmt: {
// "using" / "await using" declarations have disposal
// side-effects tied to the scope they appear in, so
// they must stay inside the closure rather than being
// hoisted to `var` + assignment.
if local.kind.is_using() {
break 'stmt stmt;
}
// Convert the declarations to assignments
let mut value = Expr::EMPTY;
for decl in local.decls.slice() {
if let Some(initializer) = decl.value {
let can_be_moved = initializer.can_be_moved();
if can_be_moved {
// if the value can be moved, move the decl directly to preserve destructuring
// ie `const { main } = class { static main() {} }` => `var {main} = class { static main() {} }`
hoist.decls.push(G::Decl {
binding: decl.binding,
value: decl.value,
});
} else {
// if the value cannot be moved, add every destructuring key separately
// ie `var { append } = { append() {} }` => `var append; __esm(() => ({ append } = { append() {} }))`
let binding = Binding::to_expr(
&decl.binding,
(&raw mut hoist).cast::<core::ffi::c_void>(),
hoist_wrapper,
);
value = value.join_with_comma(Expr::assign(
binding,
initializer,
));
}
} else {
let _ = Binding::to_expr(
&decl.binding,
(&raw mut hoist).cast::<core::ffi::c_void>(),
hoist_wrapper,
);
}
}
if value.is_empty() {
continue 'hoist;
}
break 'stmt Stmt::allocate_expr(temp_arena, value);
}
StmtData::SFunction(_) => {
stmts.append(StmtListWhich::OutsideWrapperPrefix, stmt);
continue 'hoist;
}
StmtData::SClass(mut class) => 'stmt: {
// PORT NOTE: `class` is `StoreRef<S::Class>` — an arena-owned pointer.
// `&mut class.class` (via DerefMut) yields a `&mut G::Class` into arena
// memory, so wrapping it in a StoreRef for `EClass` is sound and matches
// Zig's `&class.class`.
if class.class.can_be_moved() {
stmts.append(StmtListWhich::OutsideWrapperPrefix, stmt);
continue 'hoist;
}
let class_name_loc = class.class.class_name.unwrap().loc;
let class_name_ref = class
.class
.class_name
.unwrap()
.ref_
.expect("infallible: ref bound");
let lhs = hoist.wrap_identifier(class_name_loc, class_name_ref);
let class_ref: StoreRef<E::Class> =
StoreRef::from_bump(&mut class.class);
break 'stmt Stmt::allocate_expr(
temp_arena,
Expr::assign(
lhs,
Expr {
data: ExprData::EClass(class_ref),
loc: stmt.loc,
},
),
);
}
_ => stmt,
};
// `inner_stmts` aliases `stmts.all_stmts.items` which is not
// resized in this loop; `end <= i < len`.
inner_stmts.slice_mut()[end] = transformed;
end += 1;
}
inner_stmts.truncate(end);
}
if !hoist.decls.is_empty() {
stmts.append(
StmtListWhich::OutsideWrapperPrefix,
Stmt::alloc(
S::Local {
decls: G::DeclList::move_from_list(core::mem::take(
&mut hoist.decls,
)),
..Default::default()
},
bun_ast::Loc::EMPTY,
),
);
hoist.decls.clear();
}
let inner_len = inner_stmts.len();
if inner_len > 0 {
// See the comment in needsWrapperRef for why the symbol
// is sometimes not generated.
debug_assert!(!ast.wrapper_ref.is_empty()); // js_parser's needsWrapperRef thought wrapper was not needed
// "__esm(() => { ... })"
let esm_args = Vec::<Expr>::from_slice(&[Expr::init(
E::Arrow {
is_async,
body: G::FnBody {
stmts: inner_stmts,
loc: bun_ast::Loc::EMPTY,
},
..Default::default()
},
bun_ast::Loc::EMPTY,
)]);
// "var init_foo = __esm(...);"
let value = Expr::init(
E::Call {
target: Expr::init_identifier(c.esm_runtime_ref, bun_ast::Loc::EMPTY),
args: Vec::move_from_list(esm_args),
..Default::default()
},
bun_ast::Loc::EMPTY,
);
let decls = G::DeclList::from_slice(&[G::Decl {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.wrapper_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(value),
}]);
stmts.append(
StmtListWhich::OutsideWrapperPrefix,
Stmt::alloc(
S::Local {
decls,
..Default::default()
},
bun_ast::Loc::EMPTY,
),
);
} else {
// // If this fails, then there will be places we reference
// // `init_foo` without it actually existing.
// debug_assert!(ast.wrapper_ref.is_empty());
// TODO: the edge case where we are wrong is when there
// are references to other ESM modules, but those get
// fully hoisted. The look like side effects, but they
// are removed.
//
// It is too late to retroactively delete the
// wrapper_ref, since printing has already begun. The
// most we can do to salvage the situation is to print
// an empty arrow function.
//
// This is marked as a TODO, because this can be solved
// via a count of external modules, decremented during
// linking.
if !ast.wrapper_ref.is_empty() {
let value = Expr::init(
E::Arrow {
is_async,
body: G::FnBody {
stmts: inner_stmts,
loc: bun_ast::Loc::EMPTY,
},
..Default::default()
},
bun_ast::Loc::EMPTY,
);
stmts.append(
StmtListWhich::OutsideWrapperPrefix,
Stmt::alloc(
S::Local {
decls: G::DeclList::from_slice(&[G::Decl {
binding: Binding::alloc(
temp_arena,
B::Identifier {
r#ref: ast.wrapper_ref,
},
bun_ast::Loc::EMPTY,
),
value: Some(value),
}]),
..Default::default()
},
bun_ast::Loc::EMPTY,
),
);
}
}
}
_ => {}
}
out_stmts = bun_ast::StoreSlice::new_mut(stmts.outside_wrapper_prefix.as_mut_slice());
}
// `out_stmts` aliases either `stmts.all_stmts` or `stmts.outside_wrapper_prefix`,
// both of which remain live for the rest of this function.
let out_stmts: &mut [Stmt] = out_stmts.slice_mut();
if out_stmts.is_empty() {
return PrintResult::Result(PrintResultSuccess {
code: Box::new([]),
source_map: None,
});
}
// Collect top-level declarations from the converted statements.
// This is done here (after convertStmtsForChunk) rather than in
// postProcessJSChunk, because convertStmtsForChunk transforms the AST
// (e.g. export default expr → var, export stripping) and the converted
// statements reflect what actually gets printed.
let mut r = r;
if let Some(dc) = decl_collector {
dc.collect_from_stmts(out_stmts, &mut r, c);
}
// `get_source` returns `&'static Source` (parse_graph SoA is append-only and
// outlives the link step), so it does not borrow `c` — no split-borrow needed
// across the `&mut self` call below.
let source: &bun_ast::Source = c.get_source(source_index as u32);
c.print_code_for_file_in_chunk_js(
r,
arena,
writer,
out_stmts,
&ast,
flags,
to_esm_ref,
to_common_js_ref,
runtime_require_ref,
part_range.source_index,
source,
)
}
pub struct DeclCollector {
pub decls: Vec<DeclInfo>,
pub arena: *const Bump,
}
impl Default for DeclCollector {
fn default() -> Self {
Self {
decls: Vec::new(),
arena: core::ptr::null(),
}
}
}
impl DeclCollector {
/// Collect top-level declarations from **converted** statements (after
/// `convertStmtsForChunk`). At that point, export statements have already
/// been transformed:
/// - `s_export_default` → `s_local` / `s_function` / `s_class`
/// - `s_export_clause` → removed entirely
/// - `s_export_from` / `s_export_star` → removed or converted to `s_import`
///
/// Remaining `s_import` statements (external, non-bundled) don't need
/// handling here; their bindings are recorded separately in
/// `postProcessJSChunk` by scanning the original AST import records.
pub fn collect_from_stmts(
&mut self,
stmts: &[Stmt],
r: &mut renamer::Renamer<'_, '_>,
c: &LinkerContext,
) {
for stmt in stmts {
match stmt.data {
StmtData::SLocal(s) => {
let kind: DeclInfoKind = if s.kind == LocalKind::KVar {
DeclInfoKind::Declared
} else {
DeclInfoKind::Lexical
};
for decl in s.decls.slice() {
self.collect_from_binding(decl.binding, kind, r, c);
}
}
StmtData::SFunction(s) => {
if let Some(name_loc_ref) = s.func.name {
if let Some(name_ref) = name_loc_ref.ref_ {
self.add_ref(name_ref, DeclInfoKind::Lexical, r, c);
}
}
}
StmtData::SClass(s) => {
if let Some(class_name) = s.class.class_name {
if let Some(name_ref) = class_name.ref_ {
self.add_ref(name_ref, DeclInfoKind::Lexical, r, c);
}
}
}
_ => {}
}
}
}
fn collect_from_binding(
&mut self,
binding: Binding,
kind: DeclInfoKind,
r: &mut renamer::Renamer<'_, '_>,
c: &LinkerContext,
) {
match binding.data {
BindingData::BIdentifier(b) => {
self.add_ref(b.r#ref, kind, r, c);
}
BindingData::BArray(b) => {
for item in b.items() {
self.collect_from_binding(item.binding, kind, r, c);
}
}
BindingData::BObject(b) => {
for prop in b.properties() {
self.collect_from_binding(prop.value, kind, r, c);
}
}
BindingData::BMissing(_) => {}
}
}
fn add_ref(
&mut self,
ref_: Ref,
kind: DeclInfoKind,
r: &mut renamer::Renamer<'_, '_>,
c: &LinkerContext,
) {
let followed = c.graph.symbols.follow(ref_);
let name = r.name_for_symbol(followed);
if name.is_empty() {
return;
}
// Zig: `catch return` — silently drop on alloc failure. With std Vec
// push aborts on OOM, so there is nothing to catch.
self.decls.push(DeclInfo {
name: name.to_vec().into_boxed_slice(),
kind,
});
}
}
fn merge_adjacent_local_stmts(stmts: &mut Vec<Stmt>, _arena: &Bump) {
if stmts.is_empty() {
return;
}
let mut did_merge_with_previous_local = false;
let mut end: usize = 1;
// PORT NOTE: reshaped for borrowck — iterate by index because we read `stmts[i]`
// and write `stmts[end - 1]` / `stmts[end]` in the same loop body.
for i in 1..stmts.len() {
let stmt = stmts[i];
// Try to merge with the previous variable statement
if let StmtData::SLocal(after) = stmt.data {
if let StmtData::SLocal(mut before) = stmts[end - 1].data {
// It must be the same kind of variable statement (i.e. let/var/const)
if before.can_merge_with(&after) {
if did_merge_with_previous_local {
// Avoid O(n^2) behavior for repeated variable declarations
// Appending to this decls list is safe because did_merge_with_previous_local is true
before.decls.append_slice(after.decls.slice());
} else {
// Append the declarations to the previous variable statement
did_merge_with_previous_local = true;
let mut clone =
Vec::<G::Decl>::init_capacity(before.decls.len() + after.decls.len());
// PERF(port): was appendSliceAssumeCapacity
clone.append_slice_assume_capacity(before.decls.slice());
clone.append_slice_assume_capacity(after.decls.slice());
// we must clone instead of overwrite in-place incase the same S.Local is used across threads
// https://github.com/oven-sh/bun/issues/2942
let prev_loc = stmts[end - 1].loc;
stmts[end - 1] = Stmt::allocate(
_arena,
S::Local {
decls: Vec::move_from_list(clone),
is_export: before.is_export,
was_commonjs_export: before.was_commonjs_export,
was_ts_import_equals: before.was_ts_import_equals,
kind: before.kind,
},
prev_loc,
);
}
continue;
}
}
}
did_merge_with_previous_local = false;
stmts[end] = stmt;
end += 1;
}
stmts.truncate(end);
}
// Type aliases / re-imports for readability of match arms (mirrors Zig naming).
use bun_ast::LocalKind;
use bun_ast::binding::Data as BindingData;
use bun_ast::expr::Data as ExprData;
use bun_ast::stmt::Data as StmtData;
// ported from: src/bundler/linker_context/generateCodeForFileInChunkJS.zig