Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit e2d07dc

Browse files
author
bors-servo
authored
Auto merge of #451 - longwusha:master, r=jdm
[#411] Add some tests for default-initialized roots 1. At the current stage, I only completed the test code for JSVal; I need to know how to implement several other types of default values in order to complete all the test code. 2. The default value of JSVal's current implementation is undefined, so we should use the is_undefined() method to verify the result. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-mozjs/451) <!-- Reviewable:end -->
2 parents 2afcfa4 + d01b430 commit e2d07dc

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/rust.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ macro_rules! rooted {
400400
};
401401
(in($cx:expr) let $name:ident: $type:ty) => {
402402
let mut __root = $crate::jsapi::Rooted::new_unrooted();
403-
let $name = $crate::rust::RootedGuard::new($cx, &mut __root, $type::default());
403+
let $name = $crate::rust::RootedGuard::new($cx, &mut __root, <$type as $crate::rust::GCMethods>::initial());
404404
};
405405
(in($cx:expr) let mut $name:ident: $type:ty) => {
406406
let mut __root = $crate::jsapi::Rooted::new_unrooted();
407-
let mut $name = $crate::rust::RootedGuard::new($cx, &mut __root, $type::default());
407+
let mut $name = $crate::rust::RootedGuard::new($cx, &mut __root, <$type as $crate::rust::GCMethods>::initial());
408408
};
409409
}
410410

tests/rooting.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use mozjs::jsapi::JSPROP_ENUMERATE;
2121
use mozjs::jsapi::JS_SetGCZeal;
2222
use mozjs::jsapi::OnNewGlobalHookOption;
2323
use mozjs::jsapi::Value;
24+
use mozjs::jsapi::{JSObject, JSString, JSFunction};
25+
use mozjs::jsval::JSVal;
2426
use mozjs::rust::{Runtime, SIMPLE_GLOBAL_CLASS, define_methods};
2527
use std::ptr;
2628

@@ -45,6 +47,18 @@ fn rooting() {
4547
&CLASS as *const _,
4648
prototype_proto.handle().into()));
4749
define_methods(cx, proto.handle(), METHODS).unwrap();
50+
51+
rooted!(in(cx) let root : JSVal);
52+
assert_eq!(root.get().is_undefined(), true);
53+
54+
rooted!(in(cx) let root : *mut JSObject);
55+
assert_eq!(root.get().is_null(), true);
56+
57+
rooted!(in(cx) let root : *mut JSString);
58+
assert_eq!(root.get().is_null(), true);
59+
60+
rooted!(in(cx) let root : *mut JSFunction);
61+
assert_eq!(root.get().is_null(), true);
4862
}
4963
}
5064

0 commit comments

Comments
 (0)