1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* 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 https://mozilla.org/MPL/2.0/. */

#![recursion_limit = "128"]

use quote::{quote, TokenStreamExt};

/// First field of DomObject must be either reflector or another dom_struct,
/// all other fields must not implement DomObject
#[proc_macro_derive(DomObject)]
pub fn expand_token_stream(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = syn::parse(input).unwrap();
    expand_dom_object(input).into()
}

fn expand_dom_object(input: syn::DeriveInput) -> proc_macro2::TokenStream {
    let fields = if let syn::Data::Struct(syn::DataStruct { ref fields, .. }) = input.data {
        fields.iter().collect::<Vec<&syn::Field>>()
    } else {
        panic!("#[derive(DomObject)] should only be applied on proper structs")
    };

    let (first_field, fields) = fields
        .split_first()
        .expect("#[derive(DomObject)] should not be applied on empty structs");

    let first_field_name = first_field.ident.as_ref().unwrap();
    let mut field_types = vec![];
    for field in fields {
        if !field_types.contains(&&field.ty) {
            field_types.push(&field.ty);
        }
    }

    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
    let mut items = quote! {
        impl #impl_generics ::js::conversions::ToJSValConvertible for #name #ty_generics #where_clause {
            #[allow(unsafe_code)]
            unsafe fn to_jsval(&self,
                                cx: *mut js::jsapi::JSContext,
                                rval: js::rust::MutableHandleValue) {
                let object = crate::DomObject::reflector(self).get_jsobject();
                object.to_jsval(cx, rval)
            }
        }

        impl #impl_generics crate::DomObject for #name #ty_generics #where_clause {
            #[inline]
            fn reflector(&self) -> &crate::Reflector {
                self.#first_field_name.reflector()
            }
        }

        impl #impl_generics crate::MutDomObject for #name #ty_generics #where_clause {
            unsafe fn init_reflector(&self, obj: *mut js::jsapi::JSObject) {
                self.#first_field_name.init_reflector(obj);
            }
        }

        impl #impl_generics Eq for #name #ty_generics #where_clause {}

        impl #impl_generics PartialEq for #name #ty_generics #where_clause {
            fn eq(&self, other: &Self) -> bool {
                crate::DomObject::reflector(self) == crate::DomObject::reflector(other)
            }
        }

        // Generic trait with a blanket impl over `()` for all types.
        // becomes ambiguous if impl
        trait NoDomObjectInDomObject<A> {
            // Required for actually being able to reference the trait.
            fn some_item() {}
        }

        impl<T: ?Sized> NoDomObjectInDomObject<()> for T {}

        // Used for the specialized impl when DomObject is implemented.
        #[allow(dead_code)]
        struct Invalid;
        // forbids DomObject
        impl<T> NoDomObjectInDomObject<Invalid> for T where T: ?Sized + crate::DomObject {}
    };

    let mut params = proc_macro2::TokenStream::new();
    params.append_separated(
        input.generics.type_params().map(|param| &param.ident),
        quote! {,},
    );

    items.append_all(field_types.iter().enumerate().map(|(i, ty)| {
        let s = syn::Ident::new(&format!("S{i}"), proc_macro2::Span::call_site());
        quote! {
            struct #s<#params>(#params);

            impl #impl_generics #s<#params> #where_clause {
                fn f() {
                    // If there is only one specialized trait impl, type inference with
                    // `_` can be resolved and this can compile. Fails to compile if
                    // ty implements `NoDomObjectInDomObject<Invalid>`.
                    let _ = <#ty as NoDomObjectInDomObject<_>>::some_item;
                }
            }
        }
    }));

    let dummy_const = syn::Ident::new(
        &format!("_IMPL_DOMOBJECT_FOR_{}", name),
        proc_macro2::Span::call_site(),
    );
    let tokens = quote! {
        #[allow(non_upper_case_globals)]
        const #dummy_const: () = { #items };
    };

    tokens
}