mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Add internal plugin for creating Reflectable implementations
This commit is contained in:
parent
dd8360fb10
commit
21607f066c
4 changed files with 88 additions and 1 deletions
|
@ -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("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("privatize"))));
|
||||||
item2.attrs.push(attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(InternedString::new("jstraceable"))));
|
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)
|
P(item2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
//! - `#[dom_struct]` : Implies `#[privatize]`,`#[jstraceable]`, and `#[must_root]`.
|
//! - `#[dom_struct]` : Implies `#[privatize]`,`#[jstraceable]`, and `#[must_root]`.
|
||||||
//! Use this for structs that correspond to a DOM type
|
//! 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_imports)]
|
||||||
#![deny(unused_variables)]
|
#![deny(unused_variables)]
|
||||||
|
@ -33,12 +33,17 @@ use syntax::parse::token::intern;
|
||||||
// Public for documentation to show up
|
// Public for documentation to show up
|
||||||
/// Handles the auto-deriving for `#[jstraceable]`
|
/// Handles the auto-deriving for `#[jstraceable]`
|
||||||
pub mod jstraceable;
|
pub mod jstraceable;
|
||||||
|
/// Autogenerates implementations of Reflectable on DOM structs
|
||||||
|
pub mod reflector;
|
||||||
pub mod lints;
|
pub mod lints;
|
||||||
|
/// Utilities for writing plugins
|
||||||
|
pub mod utils;
|
||||||
|
|
||||||
#[plugin_registrar]
|
#[plugin_registrar]
|
||||||
pub fn plugin_registrar(reg: &mut Registry) {
|
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("dom_struct"), Modifier(box jstraceable::expand_dom_struct));
|
||||||
reg.register_syntax_extension(intern("jstraceable"), Decorator(box jstraceable::expand_jstraceable));
|
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::TransmutePass as LintPassObject);
|
||||||
reg.register_lint_pass(box lints::UnrootedPass as LintPassObject);
|
reg.register_lint_pass(box lints::UnrootedPass as LintPassObject);
|
||||||
reg.register_lint_pass(box lints::PrivatizePass as LintPassObject);
|
reg.register_lint_pass(box lints::PrivatizePass as LintPassObject);
|
||||||
|
|
50
components/plugins/reflector.rs
Normal file
50
components/plugins/reflector.rs
Normal file
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
29
components/plugins/utils.rs
Normal file
29
components/plugins/utils.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue