mozjs/gc/
macros.rs

1#[macro_export]
2macro_rules! rooted {
3    (&in($cx:expr) $($t:tt)*) => {
4        rooted!(in(unsafe {$cx.raw_cx_no_gc()}) $($t)*);
5    };
6	(in($cx:expr) let $($var:ident)+ = $init:expr) => {
7        let mut __root = ::std::mem::MaybeUninit::uninit();
8        let $($var)+ = $crate::gc::RootedGuard::new($cx, &mut __root, $init);
9    };
10	(in($cx:expr) let $($var:ident)+: $type:ty = $init:expr) => {
11        let mut __root = ::std::mem::MaybeUninit::uninit();
12        let $($var)+: $crate::gc::RootedGuard<$type> = $crate::gc::RootedGuard::new($cx, &mut __root, $init);
13    };
14	(in($cx:expr) let $($var:ident)+: $type:ty) => {
15        let mut __root = ::std::mem::MaybeUninit::uninit();
16        // SAFETY:
17        // We're immediately storing the initial value in a rooted location.
18        let $($var)+: $crate::gc::RootedGuard<$type> = $crate::gc::RootedGuard::new(
19            $cx,
20            &mut __root,
21            unsafe { <$type as $crate::gc::GCMethods>::initial() },
22        );
23    };
24}
25
26#[macro_export]
27macro_rules! rooted_vec {
28    (let mut $name:ident) => {
29        let mut __root = $crate::gc::RootableVec::new_unrooted();
30        let mut $name = $crate::gc::RootedVec::new(&mut __root);
31    };
32    (let $name:ident <- $iter:expr) => {
33        let mut __root = $crate::gc::RootableVec::new_unrooted();
34        let $name = $crate::gc::RootedVec::from_iter(&mut __root, $iter);
35    };
36    (let mut $name:ident <- $iter:expr) => {
37        let mut __root = $crate::gc::RootableVec::new_unrooted();
38        let mut $name = $crate::gc::RootedVec::from_iter(&mut __root, $iter);
39    };
40}
41
42#[macro_export]
43macro_rules! auto_root {
44    (&in($cx:expr) $($t:tt)*) => {
45        auto_root!(in(unsafe {$cx.raw_cx_no_gc()}) $($t)*);
46    };
47    (in($cx:expr) let $($var:ident)+ = $init:expr) => {
48        let mut __root = $crate::gc::CustomAutoRooter::new($init);
49        let $($var)+ = __root.root($cx);
50    };
51	(in($cx:expr) let $($var:ident)+: $type:ty = $init:expr) => {
52        let mut __root = $crate::gc::CustomAutoRooter::new($init);
53        let $($var)+: $crate::rust::CustomAutoRootedGuard<$type> = __root.root($cx);
54    };
55}