aboutsummaryrefslogtreecommitdiffstats
path: root/components/plugins/reflector.rs
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2014-12-01 10:10:33 -0500
committerManish Goregaokar <manishsmail@gmail.com>2014-12-27 02:43:13 +0530
commit21607f066ca07a68a6fe84acad373de5da45523f (patch)
tree57781640172e2f07deaf0023f1e12727bb9ab55f /components/plugins/reflector.rs
parentdd8360fb10a16f2124addfb276400615fe78a7ee (diff)
downloadservo-21607f066ca07a68a6fe84acad373de5da45523f.tar.gz
servo-21607f066ca07a68a6fe84acad373de5da45523f.zip
Add internal plugin for creating Reflectable implementations
Diffstat (limited to 'components/plugins/reflector.rs')
-rw-r--r--components/plugins/reflector.rs50
1 files changed, 50 insertions, 0 deletions
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");
+ }
+}