Skip to content

Commit

Permalink
Support using Rooted<T> for more non-JS types.
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Matthews <[email protected]>
  • Loading branch information
jdm committed Nov 9, 2024
1 parent dffd12e commit ed06722
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions mozjs-sys/src/jsgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::jsid::VoidId;
use std::cell::UnsafeCell;
use std::ffi::{c_char, c_void};
use std::mem;
use std::ops::DerefMut;
use std::ptr;

/// A trait for JS types that can be registered as roots.
Expand Down Expand Up @@ -114,6 +115,21 @@ pub unsafe trait TraceableTrace: Sized {
unsafe fn do_trace(&mut self, trc: *mut JSTracer);
}

unsafe impl<T: TraceableTrace> TraceableTrace for Option<T> {
unsafe fn do_trace(&mut self, trc: *mut JSTracer) {
match self {
Some(ref mut s) => s.do_trace(trc),
None => {}
}
}
}

unsafe impl<T: TraceableTrace> TraceableTrace for Box<T> {
unsafe fn do_trace(&mut self, trc: *mut JSTracer) {
self.deref_mut().do_trace(trc)
}
}

unsafe impl TraceableTrace for JS::PropertyDescriptor {
unsafe fn do_trace(&mut self, trc: *mut JSTracer) {
CallPropertyDescriptorTracer(trc, self);
Expand Down Expand Up @@ -153,6 +169,14 @@ pub trait GCMethods {
unsafe fn post_barrier(v: *mut Self, prev: Self, next: Self);
}

impl<T> GCMethods for Option<T> {
unsafe fn initial() -> Self {
None
}

unsafe fn post_barrier(_v: *mut Self, _prev: Self, _next: Self) {}
}

impl GCMethods for *mut JSObject {
unsafe fn initial() -> *mut JSObject {
ptr::null_mut()
Expand Down
2 changes: 1 addition & 1 deletion mozjs/src/gc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use crate::gc::collections::*;
pub use crate::gc::custom::*;
pub use crate::gc::root::*;
pub use crate::gc::trace::*;
pub use mozjs_sys::jsgc::{GCMethods, RootKind};
pub use mozjs_sys::jsgc::{GCMethods, RootKind, TraceableTrace};

mod collections;
mod custom;
Expand Down

0 comments on commit ed06722

Please sign in to comment.