use crate::{
ecmascript::{execution::WeakKey, types::OrdinaryObject},
engine::bindable_handle,
heap::{CompactionLists, HeapMarkAndSweep, WorkQueues},
};
#[derive(Default, Debug, Clone)]
pub(crate) struct WeakRefHeapData<'a> {
pub(super) object_index: Option<OrdinaryObject<'a>>,
pub(super) weak_ref_target: Option<WeakKey<'a>>,
pub(super) kept_alive: bool,
}
impl WeakRefHeapData<'_> {
pub(crate) fn clear_kept_objects(&mut self) {
self.kept_alive = false;
}
}
bindable_handle!(WeakRefHeapData);
impl HeapMarkAndSweep for WeakRefHeapData<'static> {
fn mark_values(&self, queues: &mut WorkQueues) {
let Self {
object_index,
weak_ref_target: value,
kept_alive: is_strong,
} = self;
object_index.mark_values(queues);
if *is_strong {
value.mark_values(queues);
}
}
fn sweep_values(&mut self, compactions: &CompactionLists) {
let Self {
object_index,
weak_ref_target: value,
kept_alive: _,
} = self;
object_index.sweep_values(compactions);
value.sweep_values(compactions);
}
}