diff --git a/mozjs-sys/src/trace.rs b/mozjs-sys/src/trace.rs index 6f06c84e3b..ee42573a2d 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) {