feat(Reflection): extract reflection capabilities into a separate module

This commit is contained in:
vsavkin
2014-11-20 12:07:48 -08:00
parent 044625a098
commit 6e8175a816
46 changed files with 637 additions and 416 deletions

View File

@ -3,20 +3,23 @@ import {Type, FIELD, isBlank, isPresent, BaseException} from 'facade/lang';
import {DOM, Element} from 'facade/dom';
import {Compiler} from './compiler/compiler';
import {ProtoView} from './compiler/view';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {Reflector, reflector} from 'reflection/reflection';
import {ReflectionCapabilities} from 'reflection/reflection_capabilities';
import {Parser} from 'change_detection/parser/parser';
import {Lexer} from 'change_detection/parser/lexer';
import {ChangeDetector} from 'change_detection/change_detector';
import {RecordRange} from 'change_detection/record_range';
import {TemplateLoader} from './compiler/template_loader';
import {Reflector} from './compiler/reflector';
import {DirectiveMetadataReader} from './compiler/directive_metadata_reader';
import {AnnotatedType} from './compiler/annotated_type';
import {ListWrapper} from 'facade/collection';
var _rootInjector: Injector;
// Contains everything that is safe to share between applications.
var _rootBindings = [Compiler, TemplateLoader, Reflector, Parser, Lexer, ClosureMap];
var _rootBindings = [
bind(Reflector).toValue(reflector), Compiler, TemplateLoader, DirectiveMetadataReader, Parser, Lexer
];
export var appViewToken = new Object();
export var appWatchGroupToken = new Object();
@ -27,12 +30,12 @@ export var appDocumentToken = new Object();
// Exported only for tests that need to overwrite default document binding.
export function documentDependentBindings(appComponentType) {
return [
bind(appComponentAnnotatedTypeToken).toFactory((reflector) => {
bind(appComponentAnnotatedTypeToken).toFactory((reader) => {
// TODO(rado): inspect annotation here and warn if there are bindings,
// lightDomServices, and other component annotations that are skipped
// for bootstrapping components.
return reflector.annotatedType(appComponentType);
}, [Reflector]),
return reader.annotatedType(appComponentType);
}, [DirectiveMetadataReader]),
bind(appElementToken).toFactory((appComponentAnnotatedType, appDocument) => {
var selector = appComponentAnnotatedType.annotation.selector;
@ -71,6 +74,8 @@ function _injectorBindings(appComponentType) {
// Multiple calls to this method are allowed. Each application would only share
// _rootInjector, which is not user-configurable by design, thus safe to share.
export function bootstrap(appComponentType: Type, bindings=null) {
reflector.reflectionCapabilities = new ReflectionCapabilities();
// TODO(rado): prepopulate template cache, so applications with only
// index.html and main.js are possible.
if (isBlank(_rootInjector)) _rootInjector = new Injector(_rootBindings);

View File

@ -4,9 +4,8 @@ import {List, ListWrapper} from 'facade/collection';
import {DOM, Element} from 'facade/dom';
import {Parser} from 'change_detection/parser/parser';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {Reflector} from './reflector';
import {DirectiveMetadataReader} from './directive_metadata_reader';
import {ProtoView} from './view';
import {CompilePipeline} from './pipeline/compile_pipeline';
import {CompileElement} from './pipeline/compile_element';
@ -22,14 +21,12 @@ import {Component} from '../annotations/annotations';
*/
export class Compiler {
_templateLoader:TemplateLoader;
_reflector: Reflector;
_reader: DirectiveMetadataReader;
_parser:Parser;
_closureMap:ClosureMap;
constructor(templateLoader:TemplateLoader, reflector: Reflector, parser:Parser, closureMap:ClosureMap) {
constructor(templateLoader:TemplateLoader, reader: DirectiveMetadataReader, parser:Parser) {
this._templateLoader = templateLoader;
this._reflector = reflector;
this._reader = reader;
this._parser = parser;
this._closureMap = closureMap;
}
createSteps(component:AnnotatedType):List<CompileStep> {
@ -37,16 +34,16 @@ export class Compiler {
var directives = annotation.template.directives;
var annotatedDirectives = ListWrapper.create();
for (var i=0; i<directives.length; i++) {
ListWrapper.push(annotatedDirectives, this._reflector.annotatedType(directives[i]));
ListWrapper.push(annotatedDirectives, this._reader.annotatedType(directives[i]));
}
return createDefaultSteps(this._parser, this._closureMap, annotatedDirectives);
return createDefaultSteps(this._parser, annotatedDirectives);
}
compile(component:Type, templateRoot:Element = null):Promise<ProtoView> {
// TODO load all components transitively from the cache first
var cache = null;
return PromiseWrapper.resolve(this.compileWithCache(
cache, this._reflector.annotatedType(component), templateRoot)
cache, this._reader.annotatedType(component), templateRoot)
);
}

View File

@ -1,6 +1,7 @@
import {Type, isPresent, BaseException} from 'facade/lang';
import {Type, isPresent, BaseException, stringify} from 'facade/lang';
import {Directive} from '../annotations/annotations';
import {AnnotatedType} from './annotated_type';
import {reflector} from 'reflection/reflection';
/**
* Interface representing a way of extracting [Directive] annotations from
@ -10,10 +11,10 @@ import {AnnotatedType} from './annotated_type';
* 2) Dart reflective implementation
* 3) Dart transformer generated implementation
*/
export class Reflector {
export class DirectiveMetadataReader {
annotatedType(type:Type):AnnotatedType {
var annotations = type.annotations;
if (annotations) {
var annotations = reflector.annotations(type);
if (isPresent(annotations)) {
for (var i=0; i<annotations.length; i++) {
var annotation = annotations[i];
if (annotation instanceof Directive) {
@ -21,6 +22,6 @@ export class Reflector {
}
}
}
throw new BaseException('No Directive annotation found on ' + type.name);
throw new BaseException(`No Directive annotation found on ${stringify(type)}`);
}
}

View File

@ -1,5 +1,4 @@
import {Parser} from 'change_detection/parser/parser';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {List} from 'facade/collection';
import {PropertyBindingParser} from './property_binding_parser';
@ -16,10 +15,7 @@ import {ElementBinderBuilder} from './element_binder_builder';
* Takes in an HTMLElement and produces the ProtoViews,
* ProtoElementInjectors and ElementBinders in the end.
*/
export function createDefaultSteps(
parser:Parser, closureMap:ClosureMap,
directives: List<AnnotatedType>
) {
export function createDefaultSteps(parser:Parser, directives: List<AnnotatedType>) {
return [
new ViewSplitter(parser),
new TextInterpolationParser(parser),
@ -28,6 +24,6 @@ export function createDefaultSteps(
new ElementBindingMarker(),
new ProtoViewBuilder(),
new ProtoElementInjectorBuilder(),
new ElementBinderBuilder(closureMap)
new ElementBinderBuilder()
];
}

View File

@ -10,7 +10,6 @@ import {Component} from '../../annotations/annotations';
import {CompileStep} from './compile_step';
import {CompileElement} from './compile_element';
import {CompileControl} from './compile_control';
import {Reflector} from '../reflector';
/**
* Parses the directives on a single element. Assumes ViewSplitter has already created

View File

@ -2,8 +2,9 @@ import {int, isPresent, isBlank, Type, BaseException, stringify} from 'facade/la
import {Element} from 'facade/dom';
import {ListWrapper, List, MapWrapper, StringMapWrapper} from 'facade/collection';
import {reflector} from 'reflection/reflection';
import {Parser} from 'change_detection/parser/parser';
import {ClosureMap} from 'change_detection/parser/closure_map';
import {ProtoRecordRange} from 'change_detection/record_range';
import {Component, Directive} from '../../annotations/annotations';
@ -11,7 +12,6 @@ import {AnnotatedType} from '../annotated_type';
import {ProtoView, ElementPropertyMemento, DirectivePropertyMemento} from '../view';
import {ProtoElementInjector} from '../element_injector';
import {ElementBinder} from '../element_binder';
import {Reflector} from '../reflector';
import {CompileStep} from './compile_step';
import {CompileElement} from './compile_element';
@ -43,11 +43,6 @@ import {CompileControl} from './compile_control';
* with the flag `isViewRoot`.
*/
export class ElementBinderBuilder extends CompileStep {
_closureMap:ClosureMap;
constructor(closureMap:ClosureMap) {
this._closureMap = closureMap;
}
process(parent:CompileElement, current:CompileElement, control:CompileControl) {
var elementBinder = null;
if (current.hasBindings) {
@ -125,7 +120,7 @@ export class ElementBinderBuilder extends CompileStep {
directiveIndex++,
expression.ast,
dirProp,
this._closureMap.setter(dirProp)
reflector.setter(dirProp)
);
});
});

View File

@ -1,27 +0,0 @@
library facade.compiler.reflector;
import 'dart:mirrors';
import '../annotations/annotations.dart';
import './annotated_type.dart';
import 'package:facade/lang.dart';
/**
* Interface representing a way of extracting [Directive] annotations from
* [Type]. This interface has three native implementations:
*
* 1) JavaScript native implementation
* 2) Dart reflective implementation
* 3) Dart transformer generated implementation
*/
class Reflector {
AnnotatedType annotatedType(Type type) {
var directiveAnnotations = reflectType(type).metadata
.map( (im) => im.reflectee)
.where( (annotation) => annotation is Directive);
if (directiveAnnotations.isEmpty) {
throw new BaseException('No Directive annotation found on '+stringify(type));
}
return new AnnotatedType(type, directiveAnnotations.first);
}
}

View File

@ -7,7 +7,7 @@ import {AST} from 'change_detection/parser/ast';
import {ProtoElementInjector, ElementInjector, PreBuiltObjects} from './element_injector';
import {ElementBinder} from './element_binder';
import {AnnotatedType} from './annotated_type';
import {SetterFn} from 'change_detection/parser/closure_map';
import {SetterFn} from 'reflection/types';
import {FIELD, IMPLEMENTS, int, isPresent, isBlank} from 'facade/lang';
import {Injector} from 'di/di';
import {NgElement} from 'core/dom/element';