Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ libc = "0.2"
memchr = "2"
rustix = { version = "0.38", default-features = false, features = ["std", "fs", "event", "process", "net"] }
bitflags = "2"
# Compile-time-checked (typestate) builders for structs and functions: named
# setters for same-typed positional args, required fields enforced by the type
# system. Zero runtime cost (the required/optional state lives in the types).
bon = "3"
thiserror = "2"
smallvec = "1"
bumpalo = { version = "3", features = ["collections", "boxed"] }
Expand Down
1 change: 1 addition & 0 deletions src/bundler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ path = "lib.rs"
workspace = true

[dependencies]
bon.workspace = true
bytemuck = "1"
bun_opaque.workspace = true
bun_dispatch.workspace = true
Expand Down
71 changes: 36 additions & 35 deletions src/bundler/LinkerContext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,13 @@ impl<'a> LinkerContext<'a> {

// When --splitting is enabled, we have to make sure we import the __jsonParse function.
self.graph
.generate_symbol_import_and_use(
html_import,
Index::part(1u32).get(),
actual_ref,
1,
Index::RUNTIME,
)
.generate_symbol_import_and_use()
.source_index(html_import)
.part_index(Index::part(1u32).get())
.ref_(actual_ref)
.use_count(1)
.source_index_to_import_from(Index::RUNTIME)
.call()
.expect("OOM");
}
}
Expand Down Expand Up @@ -3174,13 +3174,13 @@ impl<'a> LinkerContext<'a> {
// Bake uses a wrapping approach that does not use __commonJS
if self.options.output_format != Format::InternalBakeDev {
self.graph
.generate_symbol_import_and_use(
source_index,
part_index,
self.cjs_runtime_ref,
1,
crate::Index::RUNTIME,
)
.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(part_index)
.ref_(self.cjs_runtime_ref)
.use_count(1)
.source_index_to_import_from(crate::Index::RUNTIME)
.call()
.expect("unreachable");
}
}
Expand Down Expand Up @@ -3277,25 +3277,25 @@ impl<'a> LinkerContext<'a> {
*wrapper_part_index = crate::Index::part(part_index);
if wrapper_ref.is_valid() && self.options.output_format != Format::InternalBakeDev {
self.graph
.generate_symbol_import_and_use(
source_index,
part_index,
self.esm_runtime_ref,
1,
crate::Index::RUNTIME,
)
.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(part_index)
.ref_(self.esm_runtime_ref)
.use_count(1)
.source_index_to_import_from(crate::Index::RUNTIME)
.call()
.expect("OOM");

// Only mark __promiseAll as used if we have multiple async dependencies
if needs_promise_all {
self.graph
.generate_symbol_import_and_use(
source_index,
part_index,
self.promise_all_runtime_ref,
1,
crate::Index::RUNTIME,
)
.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(part_index)
.ref_(self.promise_all_runtime_ref)
.use_count(1)
.source_index_to_import_from(crate::Index::RUNTIME)
.call()
.expect("OOM");
}
}
Expand Down Expand Up @@ -4037,13 +4037,14 @@ impl<'a> LinkerContext<'a> {
},
)?;

self.graph.generate_symbol_import_and_use(
source_index,
part_index,
module_ref,
1,
crate::Index::init(source_index),
)?;
self.graph
.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(part_index)
.ref_(module_ref)
.use_count(1)
.source_index_to_import_from(crate::Index::init(source_index))
.call()?;
let top_level = &mut self
.graph
.meta
Expand Down
23 changes: 16 additions & 7 deletions src/bundler/LinkerGraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,13 @@ impl<'a> LinkerGraph<'a> {
);

let ref_ = self.runtime_function(name);
self.generate_symbol_import_and_use(
source_index,
entry_point_part_index.get(),
ref_,
count,
Index::RUNTIME,
)
self.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(entry_point_part_index.get())
.ref_(ref_)
.use_count(count)
.source_index_to_import_from(Index::RUNTIME)
.call()
}

pub fn add_part_to_file(&mut self, id: u32, part: Part) -> Result<u32, bun_alloc::AllocError> {
Expand All @@ -605,7 +605,14 @@ impl<'a> LinkerGraph<'a> {
part,
)
}
}

// Separate impl block so `#[bon::bon]` only re-emits this one method.
#[bon::bon]
impl<'a> LinkerGraph<'a> {
/// Named setters: `source_index`, `part_index`, and `use_count` are all
/// `u32`; positional arguments could transpose any pair of them.
#[builder]
pub fn generate_symbol_import_and_use(
&mut self,
source_index: u32,
Expand All @@ -631,7 +638,9 @@ impl<'a> LinkerGraph<'a> {
source_index_to_import_from,
)
}
}

impl<'a> LinkerGraph<'a> {
pub fn top_level_symbol_to_parts(&self, id: u32, ref_: Ref) -> &[u32] {
top_level_symbol_to_parts(
self.meta.items_top_level_symbol_to_parts_overlay(),
Expand Down
15 changes: 8 additions & 7 deletions src/bundler/linker_context/generateCodeForLazyExport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,14 @@ pub fn generate_code_for_lazy_export(
),
expr,
);
this.graph.generate_symbol_import_and_use(
source_index,
0,
module_ref,
1,
Index::init(source_index),
)?;
this.graph
.generate_symbol_import_and_use()
.source_index(source_index)
.part_index(0)
.ref_(module_ref)
.use_count(1)
.source_index_to_import_from(Index::init(source_index))
.call()?;

// If this is a .napi addon and it's not node, we need to generate a require() call to the runtime
if matches!(expr.data, ExprData::ECall(ref c)
Expand Down
Loading
Loading