Skip to content
12 changes: 4 additions & 8 deletions src/bundler/bundle_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6938,15 +6938,11 @@ pub mod bv2_impl {
}

impl<'a> BundleV2<'a> {
pub fn on_notify_defer(&mut self) {
self.thread_lock.assert_locked();
self.graph.deferred_pending += 1;
self.decrement_scan_counter();
}

pub fn on_notify_defer_mini(load: &mut jsc_api::JSBundler::Load, this: &mut BundleV2) {
pub fn on_notify_defer(load: &mut jsc_api::JSBundler::Load, this: &mut BundleV2) {
this.thread_lock.assert_locked();
load.deferred = true;
this.on_notify_defer();
this.graph.deferred_pending += 1;
this.decrement_scan_counter();
}

pub(crate) fn on_parse_task_complete(
Expand Down
16 changes: 11 additions & 5 deletions src/runtime/api/JSBundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,10 @@ pub mod js_bundler {
.expect("BundleV2.linker.loop must be set before plugins run");
match &mut *any_loop.as_ptr() {
bun_event_loop::AnyEventLoop::Js { .. } => {
let ct =
ConcurrentTask::from_callback(ctx.as_mut_ptr(), on_notify_defer_raw);
let ct = ConcurrentTask::from_callback(
std::ptr::from_mut::<Load>(self),
on_notify_defer_raw,
);
let poster = (*ctx.as_mut_ptr())
.js_poster
.as_ref()
Expand All @@ -1548,16 +1550,20 @@ pub mod js_bundler {
}
}

fn on_notify_defer_raw(ctx: *mut BundleV2<'static>) -> bun_event_loop::JsResult<()> {
bv2_mut(ctx).on_notify_defer();
fn on_notify_defer_raw(load: *mut Load) -> bun_event_loop::JsResult<()> {
// SAFETY: `load` is arena-allocated and linked in `outstanding_loads`
// until `on_load` runs, which the plugin can only trigger after this
// notify was posted; the arena lives as long as `load.bv2`.
let load = unsafe { &mut *load };
BundleV2::on_notify_defer(load, bv2_mut(load.bv2));
Ok(())
}

fn on_notify_defer_mini_wrap(load: *mut Load, ctx: *mut BundleV2<'static>) {
// SAFETY: callback contract — `load` was passed as the `Context` arg to
// `enqueue_task_concurrent_with_extra_ctx`; `ctx` is the bundle-thread
// `BundleV2` backref the mini loop's tick supplies as `ParentContext`.
BundleV2::on_notify_defer_mini(unsafe { &mut *load }, unsafe { &mut *ctx });
BundleV2::on_notify_defer(unsafe { &mut *load }, unsafe { &mut *ctx });
}

/// # Safety
Expand Down