Derive DomObject with a proc macro

This commit is contained in:
Anthony Ramine 2017-02-14 14:13:43 +01:00
parent 1dfdd0bab8
commit c84cea995b
9 changed files with 92 additions and 91 deletions

View file

@ -15,9 +15,7 @@ pub fn expand_dom_struct(cx: &mut ExtCtxt, sp: Span, _: &MetaItem, anno: Annotat
item2.attrs.push(quote_attr!(cx, #[repr(C)]));
item2.attrs.push(quote_attr!(cx, #[derive(JSTraceable)]));
item2.attrs.push(quote_attr!(cx, #[derive(HeapSizeOf)]));
// The following attributes are only for internal usage
item2.attrs.push(quote_attr!(cx, #[_generate_reflector]));
item2.attrs.push(quote_attr!(cx, #[derive(DomObject)]));
// #[dom_struct] gets consumed, so this lets us keep around a residue
// Do NOT register a modifier/decorator on this attribute
item2.attrs.push(quote_attr!(cx, #[_dom_struct_marker]));

View file

@ -34,8 +34,6 @@ use syntax::symbol::Symbol;
/// Handles the auto-deriving for `#[derive(JSTraceable)]`
pub mod jstraceable;
pub mod lints;
/// Autogenerates implementations of DomObject on DOM structs
pub mod reflector;
/// Utilities for writing plugins
mod utils;
@ -45,10 +43,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
Symbol::intern("dom_struct"),
MultiModifier(box jstraceable::expand_dom_struct));
reg.register_syntax_extension(
Symbol::intern("_generate_reflector"),
MultiDecorator(box reflector::expand_reflector));
reg.register_late_lint_pass(box lints::unrooted_must_root::UnrootedPass::new());
reg.register_late_lint_pass(box lints::privatize::PrivatizePass);
reg.register_late_lint_pass(box lints::inheritance_integrity::InheritancePass);

View file

@ -1,82 +0,0 @@
/* 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 http://mozilla.org/MPL/2.0/. */
use syntax::ast::{ItemKind, MetaItem};
use syntax::codemap::Span;
use syntax::ext::base::{Annotatable, ExtCtxt};
use utils::match_ty_unwrap;
pub fn expand_reflector(cx: &mut ExtCtxt, span: Span, _: &MetaItem, annotatable: &Annotatable,
push: &mut FnMut(Annotatable)) {
if let &Annotatable::Item(ref item) = annotatable {
if let ItemKind::Struct(ref def, _) = item.node {
if def.fields().is_empty() {
cx.span_err(span, "#[dom_struct] should have a reflector or \
parent dom struct as its first field");
return;
}
let struct_name = item.ident;
// This path has to be hardcoded, unfortunately, since we can't resolve paths at expansion time
match def.fields().iter().find(
|f| match_ty_unwrap(&*f.ty, &["dom", "bindings", "reflector", "Reflector"]).is_some()) {
// If it has a field that is a Reflector, use that
Some(f) => {
let field_name = f.ident;
let impl_item = quote_item!(cx,
impl ::dom::bindings::reflector::DomObject for $struct_name {
fn reflector<'a>(&'a self) -> &'a ::dom::bindings::reflector::Reflector {
&self.$field_name
}
}
);
let impl_item_mut = quote_item!(cx,
impl ::dom::bindings::reflector::MutDomObject for $struct_name {
fn init_reflector(&mut self, obj: *mut ::js::jsapi::JSObject) {
self.$field_name.set_jsobject(obj);
}
}
);
impl_item.map(|it| push(Annotatable::Item(it)));
impl_item_mut.map(|it| push(Annotatable::Item(it)))
},
// Or just call it on the first field (supertype).
None => {
let field_name = def.fields()[0].ident;
let impl_item = quote_item!(cx,
impl ::dom::bindings::reflector::DomObject for $struct_name {
fn reflector<'a>(&'a self) -> &'a ::dom::bindings::reflector::Reflector {
self.$field_name.reflector()
}
}
);
let impl_item_mut = quote_item!(cx,
impl ::dom::bindings::reflector::MutDomObject for $struct_name {
fn init_reflector(&mut self, obj: *mut ::js::jsapi::JSObject) {
self.$field_name.init_reflector(obj);
}
}
);
impl_item.map(|it| push(Annotatable::Item(it)));
impl_item_mut.map(|it| push(Annotatable::Item(it)))
}
};
let impl_item = quote_item!(cx,
impl ::js::conversions::ToJSValConvertible for $struct_name {
#[allow(unsafe_code)]
unsafe fn to_jsval(&self,
cx: *mut ::js::jsapi::JSContext,
rval: ::js::jsapi::MutableHandleValue) {
let object = ::dom::bindings::reflector::DomObject::reflector(self).get_jsobject();
object.to_jsval(cx, rval)
}
}
);
impl_item.map(|it| push(Annotatable::Item(it)));
} else {
cx.span_err(span, "#[dom_struct] seems to have been applied to a non-struct");
}
}
}