Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jstz_engine): define the Trace and Finalize traits #662

Open
wants to merge 1 commit into
base: ajob410@jstz-194-add-gcptr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 align 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