Skip to content

Commit

Permalink
feat(jstz_engine): implement JsString and JsStrs
Browse files Browse the repository at this point in the history
  • Loading branch information
zcabter committed Feb 12, 2025
1 parent b776228 commit ce2b057
Show file tree
Hide file tree
Showing 4 changed files with 687 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/jstz_engine/src/gc/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ pub trait AsRawPtr {
unsafe fn as_raw_ptr(&self) -> Self::Ptr;
}

impl<T: AsRawPtr> AsRawPtr for &T {
type Ptr = T::Ptr;

unsafe fn as_raw_ptr(&self) -> Self::Ptr {
(*self).as_raw_ptr()
}
}

pub trait AsRawHandle: AsRawPtr {
/// Retrieves a SpiderMonkey Rooted Handle to the underlying value.
///
Expand All @@ -38,6 +46,12 @@ pub trait AsRawHandle: AsRawPtr {
unsafe fn as_raw_handle(&self) -> Handle<Self::Ptr>;
}

impl<T: AsRawHandle> AsRawHandle for &T {
unsafe fn as_raw_handle(&self) -> Handle<Self::Ptr> {
(*self).as_raw_handle()
}
}

pub trait AsRawHandleMut: AsRawHandle {
/// Retrieves a SpiderMonkey Rooted Handle to the underlying value.
///
Expand All @@ -48,6 +62,12 @@ pub trait AsRawHandleMut: AsRawHandle {
unsafe fn as_raw_handle_mut(&self) -> HandleMut<Self::Ptr>;
}

impl<T: AsRawHandleMut> AsRawHandleMut for &T {
unsafe fn as_raw_handle_mut(&self) -> HandleMut<Self::Ptr> {
(*self).as_raw_handle_mut()
}
}

/// A GC barrier is a mechanism used to ensure that the garbage collector maintains
/// a valid set of reachable objects.
///
Expand Down
12 changes: 12 additions & 0 deletions crates/jstz_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod context;
pub mod gc;
mod realm;
mod script;
mod string;
mod value;

pub fn compile_and_evaluate_script(handle: JSEngineHandle, source: &str) -> JSVal {
Expand Down Expand Up @@ -45,6 +46,17 @@ mod test {
use mozjs::rust::SIMPLE_GLOBAL_CLASS;
use mozjs::rust::{JSEngine, RealmOptions, Runtime};

#[macro_export]
macro_rules! setup_cx {
($name: ident) => {
let engine = mozjs::rust::JSEngine::init().unwrap();
let rt = mozjs::rust::Runtime::new(engine.handle());
let rt_cx = &mut $crate::context::Context::from_runtime(&rt);
$crate::alloc_compartment!(c);
let mut $name = rt_cx.new_realm(c).unwrap();
};
}

#[test]
fn test_eval() {
let engine = JSEngine::init().unwrap();
Expand Down
Loading

0 comments on commit ce2b057

Please sign in to comment.