diff --git a/src/rust.rs b/src/rust.rs index c7012b863..d7eb1ab6e 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -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()); }; } diff --git a/tests/rooting.rs b/tests/rooting.rs index b2f558094..7a109bea3 100644 --- a/tests/rooting.rs +++ b/tests/rooting.rs @@ -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; @@ -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); } }