Skip to content

Commit

Permalink
feat(jstz_engine): define the Trace and Finalize traits
Browse files Browse the repository at this point in the history
This commit also implements the `Trace` and `Finalize` trait for many common types.
  • Loading branch information
johnyob committed Jan 6, 2025
1 parent c31494b commit f41ee13
Show file tree
Hide file tree
Showing 3 changed files with 413 additions and 3 deletions.
3 changes: 3 additions & 0 deletions crates/jstz_engine/src/gc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
//! For further details, see the [GC Implementation Guide](https://udn.realityripple.com/docs/Mozilla/Projects/SpiderMonkey/Internals/Garbage_collection).
mod ptr;
mod trace;

pub use trace::{Finalize, Trace, Tracer};
18 changes: 15 additions & 3 deletions crates/jstz_engine/src/gc/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use mozjs::{
jsapi::{
jsid, HeapBigIntWriteBarriers, HeapObjectWriteBarriers, HeapScriptWriteBarriers,
HeapStringWriteBarriers, HeapValueWriteBarriers, JSFunction, JSObject, JSScript,
JSString, JS::BigInt as JSBigInt, JS::Symbol as JSSymbol,
JSString,
JS::{BigInt as JSBigInt, Symbol as JSSymbol},
},
jsid::VoidId,
jsval::{JSVal, UndefinedValue},
Expand Down Expand Up @@ -45,7 +46,7 @@ pub unsafe trait WriteBarrieredPtr: Copy {
///
/// # Safety
///
/// `GcPtr<T>` should only be used by values on the heap. Garbage collected pointers
/// [`GcPtr<T>`] should only be used by values on the heap. Garbage collected pointers
/// on the stack should be rooted.
pub struct GcPtr<T: WriteBarrieredPtr> {
// # Safety
Expand Down Expand Up @@ -92,14 +93,25 @@ impl<T: WriteBarrieredPtr> GcPtr<T> {
///
/// # Safety
///
/// the caller must guarantee that the pointer is valid for reads and
/// The caller must guarantee that the pointer is valid for reads and
/// points to a valid `js::gc::Cell`.
pub unsafe fn get(&self) -> T {
// Note: read_unaligned is used since SpiderMonkey doesn't
// guarantee the expected alignment of Rust pointers.
self.inner_ptr.get().read_unaligned()
}

/// Returns the raw pointer to the internal cell of [`GcPtr`].
///
/// # Notes
///
/// While the operation itself is not unsafe, the caller must guarantee
/// that any writes to the pointer are barriered and that the [`GcPtr`]
/// isn't moved for the lifetime of the pointer.
pub fn get_unsafe(&self) -> *mut T {
self.inner_ptr.get()
}

/// Sets the pointer to a new value
pub fn set(self: Pin<&Self>, next: T) {
let self_ptr = self.inner_ptr.get();
Expand Down
Loading

0 comments on commit f41ee13

Please sign in to comment.