| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of polymer; | 5 part of polymer; |
| 6 | 6 |
| 7 /** Annotation used to automatically register polymer elements. */ | 7 /** Annotation used to automatically register polymer elements. */ |
| 8 class CustomTag { | 8 class CustomTag { |
| 9 final String tagName; | 9 final String tagName; |
| 10 const CustomTag(this.tagName); | 10 final Type type; |
| 11 // TODO(13997): Remove the type parameter. |
| 12 const CustomTag(this.tagName, {this.type}); |
| 11 } | 13 } |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * Metadata used to label static or top-level methods that are called | 16 * Metadata used to label static or top-level methods that are called |
| 15 * automatically when loading the library of a custom element. | 17 * automatically when loading the library of a custom element. |
| 16 */ | 18 */ |
| 17 const initMethod = const _InitMethodAnnotation(); | 19 const initMethod = const _InitMethodAnnotation(); |
| 18 | 20 |
| 19 /** | 21 /** |
| 20 * Initializes a polymer application as follows: | 22 * Initializes a polymer application as follows: |
| 21 * * set up up polling for observable changes | 23 * * set up up polling for observable changes |
| 22 * * initialize MDV | 24 * * initialize MDV |
| 23 * * for each library in [libraries], register custom elements labeled with | 25 * * for each library in [libraries], register custom elements labeled with |
| 24 * [CustomTag] and invoke the initialization method on it. | 26 * [CustomTag] and invoke the initialization method on it. |
| 25 * | 27 * |
| 26 * The initialization on each library is either a method named `main` or | 28 * The initialization on each library is either a method named `main` or |
| 27 * a top-level function and annotated with [initMethod]. | 29 * a top-level function and annotated with [initMethod]. |
| 28 * | 30 * |
| 29 * The urls in [libraries] can be absolute or relative to [srcUrl]. | 31 * The urls in [libraries] can be absolute or relative to [srcUrl]. |
| 30 */ | 32 */ |
| 31 void initPolymer(List<String> libraries, [String srcUrl]) { | 33 void initPolymer(List<String> libraries, [String srcUrl]) { |
| 32 runMicrotask(() { | 34 runMicrotask(() { |
| 33 // DOM events don't yet go through microtasks, so we catch those here. | 35 // DOM events don't yet go through microtasks, so we catch those here. |
| 34 new Timer.periodic(new Duration(milliseconds: 125), | 36 new Timer.periodic(new Duration(milliseconds: 125), |
| 35 (_) => performMicrotaskCheckpoint()); | 37 (_) => performMicrotaskCheckpoint()); |
| 36 | 38 |
| 37 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize. | 39 // TODO(jmesserly): mdv should use initMdv instead of mdv.initialize. |
| 38 mdv.initialize(); | 40 mdv.initialize(); |
| 39 registerCustomElement('polymer-element', () => new PolymerDeclaration()); | 41 document.register(PolymerDeclaration._TAG, PolymerDeclaration); |
| 40 | 42 |
| 41 for (var lib in libraries) { | 43 for (var lib in libraries) { |
| 42 _loadLibrary(lib, srcUrl); | 44 _loadLibrary(lib, srcUrl); |
| 43 } | 45 } |
| 44 | 46 |
| 45 // TODO(jmesserly): this should be in the custom_element polyfill, not here. | 47 // TODO(jmesserly): this should be in the custom_element polyfill, not here. |
| 46 // notify the system that we are bootstrapped | 48 // notify the system that we are bootstrapped |
| 47 document.body.dispatchEvent( | 49 document.body.dispatchEvent( |
| 48 new CustomEvent('WebComponentsReady', canBubble: true)); | 50 new CustomEvent('WebComponentsReady', canBubble: true)); |
| 49 }); | 51 }); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // Search top-level functions marked with @initMethod | 85 // Search top-level functions marked with @initMethod |
| 84 for (var f in lib.functions.values) { | 86 for (var f in lib.functions.values) { |
| 85 _maybeInvoke(lib, f); | 87 _maybeInvoke(lib, f); |
| 86 } | 88 } |
| 87 | 89 |
| 88 for (var c in lib.classes.values) { | 90 for (var c in lib.classes.values) { |
| 89 // Search for @CustomTag on classes | 91 // Search for @CustomTag on classes |
| 90 for (var m in c.metadata) { | 92 for (var m in c.metadata) { |
| 91 var meta = m.reflectee; | 93 var meta = m.reflectee; |
| 92 if (meta is CustomTag) { | 94 if (meta is CustomTag) { |
| 93 Polymer._registerClassMirror(meta.tagName, c); | 95 Polymer.register(meta.tagName, _getReflectedTypeWorkaround(c)); |
| 94 } | 96 } |
| 95 } | 97 } |
| 96 | 98 |
| 97 // TODO(sigmund): check also static methods marked with @initMethod. | 99 // TODO(sigmund): check also static methods marked with @initMethod. |
| 98 // This is blocked on two bugs: | 100 // This is blocked on two bugs: |
| 99 // - dartbug.com/12133 (static methods are incorrectly listed as top-level | 101 // - dartbug.com/12133 (static methods are incorrectly listed as top-level |
| 100 // in dart2js, so they end up being called twice) | 102 // in dart2js, so they end up being called twice) |
| 101 // - dartbug.com/12134 (sometimes "method.metadata" throws an exception, | 103 // - dartbug.com/12134 (sometimes "method.metadata" throws an exception, |
| 102 // we could wrap and hide those exceptions, but it's not ideal). | 104 // we could wrap and hide those exceptions, but it's not ideal). |
| 103 } | 105 } |
| 104 } | 106 } |
| 105 | 107 |
| 108 // Horrible hack to work around: http://dartbug.com/12607 |
| 109 Type _getReflectedTypeWorkaround(ClassMirror cls) { |
| 110 // On Dart VM, just return reflectedType. |
| 111 if (1.0 is! int) return cls.reflectedType; |
| 112 |
| 113 var mangledName = reflect(cls).getField(_mangledNameField).reflectee; |
| 114 Type type = _jsHelper.invoke(#createRuntimeType, [mangledName]).reflectee; |
| 115 return type; |
| 116 } |
| 117 |
| 118 final LibraryMirror _jsHelper = |
| 119 currentMirrorSystem().libraries[Uri.parse('dart:_js_helper')]; |
| 120 |
| 121 final Symbol _mangledNameField = () { |
| 122 var jsClassMirrorMirror = reflect(reflectClass(ClassMirror)).type; |
| 123 for (var name in jsClassMirrorMirror.variables.keys) { |
| 124 if (MirrorSystem.getName(name) == '_mangledName') return name; |
| 125 } |
| 126 }(); |
| 127 |
| 106 void _maybeInvoke(ObjectMirror obj, MethodMirror method) { | 128 void _maybeInvoke(ObjectMirror obj, MethodMirror method) { |
| 107 var annotationFound = false; | 129 var annotationFound = false; |
| 108 for (var meta in method.metadata) { | 130 for (var meta in method.metadata) { |
| 109 if (identical(meta.reflectee, initMethod)) { | 131 if (identical(meta.reflectee, initMethod)) { |
| 110 annotationFound = true; | 132 annotationFound = true; |
| 111 break; | 133 break; |
| 112 } | 134 } |
| 113 } | 135 } |
| 114 if (!annotationFound) return; | 136 if (!annotationFound) return; |
| 115 if (!method.isStatic) { | 137 if (!method.isStatic) { |
| 116 print("warning: methods marked with @initMethod should be static," | 138 print("warning: methods marked with @initMethod should be static," |
| 117 " ${method.simpleName} is not."); | 139 " ${method.simpleName} is not."); |
| 118 return; | 140 return; |
| 119 } | 141 } |
| 120 if (!method.parameters.where((p) => !p.isOptional).isEmpty) { | 142 if (!method.parameters.where((p) => !p.isOptional).isEmpty) { |
| 121 print("warning: methods marked with @initMethod should take no " | 143 print("warning: methods marked with @initMethod should take no " |
| 122 "arguments, ${method.simpleName} expects some."); | 144 "arguments, ${method.simpleName} expects some."); |
| 123 return; | 145 return; |
| 124 } | 146 } |
| 125 obj.invoke(method.simpleName, const []); | 147 obj.invoke(method.simpleName, const []); |
| 126 } | 148 } |
| 127 | 149 |
| 128 class _InitMethodAnnotation { | 150 class _InitMethodAnnotation { |
| 129 const _InitMethodAnnotation(); | 151 const _InitMethodAnnotation(); |
| 130 } | 152 } |
| OLD | NEW |