From e52d0bd58080905e90801eb758b8f1267a06c7c2 Mon Sep 17 00:00:00 2001 From: Narfinger Date: Mon, 17 Nov 2025 10:24:24 +0100 Subject: [PATCH] Added traceable for std::rc::Weak Signed-off-by: Narfinger --- mozjs-sys/src/trace.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mozjs-sys/src/trace.rs b/mozjs-sys/src/trace.rs index 6f06c84e3b6..ee42573a2df 100644 --- a/mozjs-sys/src/trace.rs +++ b/mozjs-sys/src/trace.rs @@ -27,7 +27,7 @@ use std::num::{ }; use std::ops::Range; use std::path::PathBuf; -use std::rc::Rc; +use std::rc::{Rc, Weak}; use std::sync::atomic::{ AtomicBool, AtomicI16, AtomicI32, AtomicI64, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU64, AtomicU8, AtomicUsize, @@ -137,6 +137,17 @@ unsafe impl Traceable for Rc { } } +unsafe impl Traceable for Weak { + #[inline] + unsafe fn trace(&self, trc: *mut JSTracer) { + // A rc::Weak needs to be reachable by the JS. + // It could happen that we upgrade a `Weak` on the stack while the original `Rc` + // is getting dropped. Then this could lead to the Gc cleaning up the upgraded `Rc` + // while it is still in use via the upgraded `Weak`. + self.upgrade().trace(trc); + } +} + unsafe impl Traceable for Box { #[inline] unsafe fn trace(&self, trc: *mut JSTracer) {