-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Josh Matthews <[email protected]>
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |