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

Commit

Permalink
Auto merge of #451 - longwusha:master, r=jdm
Browse files Browse the repository at this point in the history
[#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 -->
  • Loading branch information
bors-servo authored Dec 22, 2018
2 parents 2afcfa4 + d01b430 commit e2d07dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ macro_rules! rooted {
};
(in($cx:expr) let $name:ident: $type:ty) => {
let mut __root = $crate::jsapi::Rooted::new_unrooted();
let $name = $crate::rust::RootedGuard::new($cx, &mut __root, $type::default());
let $name = $crate::rust::RootedGuard::new($cx, &mut __root, <$type as $crate::rust::GCMethods>::initial());
};
(in($cx:expr) let mut $name:ident: $type:ty) => {
let mut __root = $crate::jsapi::Rooted::new_unrooted();
let mut $name = $crate::rust::RootedGuard::new($cx, &mut __root, $type::default());
let mut $name = $crate::rust::RootedGuard::new($cx, &mut __root, <$type as $crate::rust::GCMethods>::initial());
};
}

Expand Down
14 changes: 14 additions & 0 deletions tests/rooting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use mozjs::jsapi::JSPROP_ENUMERATE;
use mozjs::jsapi::JS_SetGCZeal;
use mozjs::jsapi::OnNewGlobalHookOption;
use mozjs::jsapi::Value;
use mozjs::jsapi::{JSObject, JSString, JSFunction};
use mozjs::jsval::JSVal;
use mozjs::rust::{Runtime, SIMPLE_GLOBAL_CLASS, define_methods};
use std::ptr;

Expand All @@ -45,6 +47,18 @@ fn rooting() {
&CLASS as *const _,
prototype_proto.handle().into()));
define_methods(cx, proto.handle(), METHODS).unwrap();

rooted!(in(cx) let root : JSVal);
assert_eq!(root.get().is_undefined(), true);

rooted!(in(cx) let root : *mut JSObject);
assert_eq!(root.get().is_null(), true);

rooted!(in(cx) let root : *mut JSString);
assert_eq!(root.get().is_null(), true);

rooted!(in(cx) let root : *mut JSFunction);
assert_eq!(root.get().is_null(), true);
}
}

Expand Down

0 comments on commit e2d07dc

Please sign in to comment.