aboutsummaryrefslogtreecommitdiffstats
path: root/components/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'components/plugins')
-rw-r--r--components/plugins/jstraceable.rs3
-rw-r--r--components/plugins/lib.rs7
-rw-r--r--components/plugins/reflector.rs50
-rw-r--r--components/plugins/utils.rs29
4 files changed, 88 insertions, 1 deletions
diff --git a/components/plugins/jstraceable.rs b/components/plugins/jstraceable.rs
index 158ec60f37f..a093ae9128a 100644
--- a/components/plugins/jstraceable.rs
+++ b/components/plugins/jstraceable.rs
@@ -17,6 +17,9 @@ pub fn expand_dom_struct(_: &mut ExtCtxt, _: Span, _: &MetaItem, item: P<Item>)
item2.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("must_root"))));
item2.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("privatize"))));
item2.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("jstraceable"))));
+
+ // The following attribute is only for internal usage
+ item2.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("_generate_reflector"))));
P(item2)
}
diff --git a/components/plugins/lib.rs b/components/plugins/lib.rs
index aa4ba7fd865..77587b3cda9 100644
--- a/components/plugins/lib.rs
+++ b/components/plugins/lib.rs
@@ -12,7 +12,7 @@
//! - `#[dom_struct]` : Implies `#[privatize]`,`#[jstraceable]`, and `#[must_root]`.
//! Use this for structs that correspond to a DOM type
-#![feature(macro_rules, plugin_registrar, quote, phase)]
+#![feature(macro_rules, plugin_registrar, quote, phase, if_let)]
#![deny(unused_imports)]
#![deny(unused_variables)]
@@ -33,12 +33,17 @@ use syntax::parse::token::intern;
// Public for documentation to show up
/// Handles the auto-deriving for `#[jstraceable]`
pub mod jstraceable;
+/// Autogenerates implementations of Reflectable on DOM structs
+pub mod reflector;
pub mod lints;
+/// Utilities for writing plugins
+pub mod utils;
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(intern("dom_struct"), Modifier(box jstraceable::expand_dom_struct));
reg.register_syntax_extension(intern("jstraceable"), Decorator(box jstraceable::expand_jstraceable));
+ reg.register_syntax_extension(intern("_generate_reflector"), Decorator(box reflector::expand_reflector));
reg.register_lint_pass(box lints::TransmutePass as LintPassObject);
reg.register_lint_pass(box lints::UnrootedPass as LintPassObject);
reg.register_lint_pass(box lints::PrivatizePass as LintPassObject);
diff --git a/components/plugins/reflector.rs b/components/plugins/reflector.rs
new file mode 100644
index 00000000000..901846791c4
--- /dev/null
+++ b/components/plugins/reflector.rs
@@ -0,0 +1,50 @@
+/* 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::ext::base::ExtCtxt;
+use syntax::codemap::Span;
+use syntax::ptr::P;
+use syntax::ast::{Item, MetaItem};
+use syntax::ast;
+use utils::match_ty_unwrap;
+
+
+pub fn expand_reflector(cx: &mut ExtCtxt, span: Span, _: &MetaItem, item: &Item, push: |P<Item>|) {
+
+ if let ast::ItemStruct(ref def, _) = item.node {
+ let struct_name = item.ident;
+ match def.fields.iter().find(|f| match_ty_unwrap(&*f.node.ty, &["dom", "bindings", "utils", "Reflector"]).is_some()) {
+ // If it has a field that is a Reflector, use that
+ Some(f) => {
+ let field_name = f.node.ident();
+ let impl_item = quote_item!(cx,
+ impl ::dom::bindings::utils::Reflectable for $struct_name {
+ fn reflector<'a>(&'a self) -> &'a ::dom::bindings::utils::Reflector {
+ &self.$field_name
+ }
+ }
+ );
+ impl_item.map(|it| push(it))
+ },
+ // Or just call it on the first field (supertype).
+ // TODO: Write a lint to ensure that this first field is indeed a #[dom_struct],
+ // and the only such field in the struct definition (including reflectors)
+ // Unfortunately we can't do it here itself because a def_map (from middle) is not available
+ // at expansion time
+ None => {
+ let field_name = def.fields[0].node.ident();
+ let impl_item = quote_item!(cx,
+ impl ::dom::bindings::utils::Reflectable for $struct_name {
+ fn reflector<'a>(&'a self) -> &'a ::dom::bindings::utils::Reflector {
+ self.$field_name.reflector()
+ }
+ }
+ );
+ impl_item.map(|it| push(it))
+ }
+ };
+ } else {
+ cx.span_bug(span, "#[dom_struct] seems to have been applied to a non-struct");
+ }
+}
diff --git a/components/plugins/utils.rs b/components/plugins/utils.rs
new file mode 100644
index 00000000000..434a5e08582
--- /dev/null
+++ b/components/plugins/utils.rs
@@ -0,0 +1,29 @@
+/* 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::ptr::P;
+use syntax::ast::{TyPath, Path, AngleBracketedParameters, PathSegment, Ty};
+
+/// Matches a type with a provided string, and returns its type parameters if successful
+pub fn match_ty_unwrap<'a>(ty: &'a Ty, segments: &[&str]) -> Option<&'a [P<Ty>]> {
+ match ty.node {
+ TyPath(Path {segments: ref seg, ..}, _, _) => {
+ // So ast::Path isn't the full path, just the tokens that were provided.
+ // I could muck around with the maps and find the full path
+ // however the more efficient way is to simply reverse the iterators and zip them
+ // which will compare them in reverse until one of them runs out of segments
+ if seg.iter().rev().zip(segments.iter().rev()).all(|(a,b)| a.identifier.as_str() == *b) {
+ match seg.as_slice().last() {
+ Some(&PathSegment {parameters: AngleBracketedParameters(ref a), ..}) => {
+ Some(a.types.as_slice())
+ }
+ _ => None
+ }
+ } else {
+ None
+ }
+ },
+ _ => None
+ }
+}