Skip to content

Commit 83cfbb0

Browse files
committed
Add tests
1 parent a4892da commit 83cfbb0

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

core/engine/src/builtins/finalization_registry/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ use crate::{
2323

2424
use super::{BuiltInConstructor, BuiltInObject, IntrinsicObject, builder::BuiltInBuilder};
2525

26+
#[cfg(test)]
27+
mod tests;
28+
2629
/// On GG collection, sends a message to a [`FinalizationRegistry`] indicating that it needs to
2730
/// be collected.
2831
#[derive(Trace)]
29-
#[boa_gc(unsafe_empty_trace)]
30-
struct CleanupSignaler(Cell<Option<async_channel::WeakSender<()>>>);
32+
struct CleanupSignaler(#[unsafe_ignore_trace] Cell<Option<async_channel::WeakSender<()>>>);
3133

3234
impl Finalize for CleanupSignaler {
3335
fn finalize(&self) {
@@ -313,12 +315,7 @@ impl FinalizationRegistry {
313315
let _key = cell.target.key();
314316
// TODO: it might be better to add a special ref for the value that
315317
// also preserves the original key instead.
316-
// SAFETY: the original key is alive per our previous call to `key`,
317-
// so if this returns `Some`, then the value cannot be collected
318-
// until `key` gets dropped.
319-
unsafe {
320-
cell.target.value_ref().and_then(|v| v.0.take());
321-
}
318+
cell.target.value().and_then(|v| v.0.take());
322319

323320
// ii. Set removed to true.
324321
removed = true;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use indoc::indoc;
2+
3+
use crate::{TestAction, run_test_actions};
4+
5+
#[test]
6+
fn finalization_registry_simple() {
7+
run_test_actions([
8+
TestAction::run(indoc! {r#"
9+
let counter = 0;
10+
const registry = new FinalizationRegistry(() => {
11+
counter++;
12+
});
13+
14+
registry.register(["foo"]);
15+
"#}),
16+
TestAction::assert_eq("counter", 0),
17+
TestAction::inspect_context(|_| boa_gc::force_collect()),
18+
TestAction::inspect_context(|ctx| ctx.run_jobs().unwrap()),
19+
TestAction::assert_eq("counter", 1),
20+
]);
21+
}

core/engine/src/builtins/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ pub(crate) fn set_default_global_bindings(context: &mut Context) -> JsResult<()>
447447
global_binding::<WeakSet>(context)?;
448448
global_binding::<IteratorConstructor>(context)?;
449449
global_binding::<Atomics>(context)?;
450+
global_binding::<FinalizationRegistry>(context)?;
450451

451452
#[cfg(feature = "annex-b")]
452453
{

0 commit comments

Comments
 (0)