Skip to content

Commit

Permalink
Add test for new Rootable usage.
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Matthews <[email protected]>
  • Loading branch information
jdm committed Dec 25, 2024
1 parent 36814f6 commit b34a211
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions mozjs/tests/rootable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![cfg(feature = "debugmozjs")]

use std::ptr;

use mozjs::jsapi::{
GetRealmObjectPrototype, JS_NewGlobalObject, SetGCZeal,
};
use mozjs::jsapi::{JSAutoRealm, OnNewGlobalHookOption, Value, JSTracer};
use mozjs::jsval::ObjectValue;
use mozjs::rooted;
use mozjs::rust::{JSEngine, RealmOptions, Runtime, SIMPLE_GLOBAL_CLASS};

impl mozjs::gc::Rootable for ContainsGCValue {}
unsafe impl mozjs::gc::Traceable for ContainsGCValue {
unsafe fn trace(&self, trc: *mut JSTracer) {
self.val.trace(trc);
}
}

impl mozjs::gc::Initialize for ContainsGCValue {
unsafe fn initial() -> Option<ContainsGCValue> { None }
}

struct ContainsGCValue {
val: Value,
}

#[test]
fn rooting() {
let engine = JSEngine::init().unwrap();
let runtime = Runtime::new(engine.handle());
let context = runtime.cx();
let h_option = OnNewGlobalHookOption::FireOnNewGlobalHook;
let c_option = RealmOptions::default();

unsafe {
SetGCZeal(context, 2, 1);
rooted!(in(context) let global = JS_NewGlobalObject(
context,
&SIMPLE_GLOBAL_CLASS,
ptr::null_mut(),
h_option,
&*c_option,
));
let _ac = JSAutoRealm::new(context, global.get());

rooted!(in(context) let prototype_proto = GetRealmObjectPrototype(context));
rooted!(in(context) let some_container = ContainsGCValue {
val: ObjectValue(prototype_proto.get())
});
rooted!(in(context) let some_optional_container = Some(ContainsGCValue {
val: ObjectValue(prototype_proto.get())
}));
assert_eq!(some_container.val.to_object(), prototype_proto.get());
assert_eq!(some_container.val, some_optional_container.as_ref().unwrap().val);
}
}

0 comments on commit b34a211

Please sign in to comment.