OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Static implementation of smoke services using code-generated data. | 5 /// Static implementation of smoke services using code-generated data. |
6 library smoke.static; | 6 library smoke.static; |
7 | 7 |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'package:logging/logging.dart'; | 10 import 'package:logging/logging.dart'; |
11 import 'package:smoke/smoke.dart'; | 11 import 'package:smoke/smoke.dart'; |
12 | 12 |
13 import 'src/common.dart'; | 13 import 'src/common.dart'; |
14 | 14 |
15 typedef T Getter<T>(object); | 15 typedef T Getter<T>(object); |
16 typedef void Setter<T>(object, value); | 16 typedef void Setter<T>(object, value); |
17 | 17 |
18 StaticConfiguration _configuration; | |
19 | |
20 class StaticConfiguration { | 18 class StaticConfiguration { |
21 /// Maps symbol to a function that reads that symbol of an object. For | 19 /// Maps symbol to a function that reads that symbol of an object. For |
22 /// instance, `#i: (o) => o.i`. | 20 /// instance, `#i: (o) => o.i`. |
23 final Map<Symbol, Getter> getters; | 21 final Map<Symbol, Getter> getters; |
24 | 22 |
25 /// Maps symbol to a function that updates that symbol of an object. For | 23 /// Maps symbol to a function that updates that symbol of an object. For |
26 /// instance, `#i: (o, v) { o.i = v; }`. | 24 /// instance, `#i: (o, v) { o.i = v; }`. |
27 final Map<Symbol, Setter> setters; | 25 final Map<Symbol, Setter> setters; |
28 | 26 |
29 /// Maps a type to it's super class. For example, String: Object. | 27 /// Maps a type to it's super class. For example, String: Object. |
30 final Map<Type, Type> parents; | 28 final Map<Type, Type> parents; |
31 | 29 |
32 /// For each type, a map of declarations per symbol (property or method). | 30 /// For each type, a map of declarations per symbol (property or method). |
33 final Map<Type, Map<Symbol, Declaration>> declarations; | 31 final Map<Type, Map<Symbol, Declaration>> declarations; |
34 | 32 |
35 /// A map from symbol to strings. | 33 /// A map from symbol to strings. |
36 final Map<Symbol, String> names; | 34 final Map<Symbol, String> names; |
37 | 35 |
38 /// A map from strings to symbols (the reverse of [names]). | 36 /// Whether to check for missing declarations, otherwise, return default |
39 final Map<String, Symbol> symbols; | 37 /// values (for example a missing parent class can be treated as Object) |
| 38 final bool checkedMode; |
| 39 |
40 StaticConfiguration({ | 40 StaticConfiguration({ |
41 this.getters: const {}, this.setters: const {}, this.parents: const {}, | 41 this.getters: const {}, this.setters: const {}, this.parents: const {}, |
42 this.declarations: const {}, this.names: const {}}) | 42 this.declarations: const {}, this.names: const {}, |
43 : this.symbols = {} { | 43 this.checkedMode: true}); |
44 names.forEach((k, v) { symbols[v] = k; }); | |
45 } | |
46 } | 44 } |
47 | 45 |
48 /// Set up the smoke package to use a static implementation based on the given | 46 /// Set up the smoke package to use a static implementation based on the given |
49 /// [configuration]. | 47 /// [configuration]. |
50 useGeneratedCode(StaticConfiguration configuration) { | 48 useGeneratedCode(StaticConfiguration configuration) { |
51 _configuration = configuration; | 49 configure(new GeneratedObjectAccessorService(configuration), |
52 configure(new _GeneratedObjectAccessorService(), | 50 new GeneratedTypeInspectorService(configuration), |
53 new _GeneratedTypeInspectorService(), | 51 new GeneratedSymbolConverterService(configuration)); |
54 new _GeneratedSymbolConverterService()); | |
55 } | 52 } |
56 | 53 |
57 /// Implements [ObjectAccessorService] using a static configuration. | 54 /// Implements [ObjectAccessorService] using a static configuration. |
58 class _GeneratedObjectAccessorService implements ObjectAccessorService { | 55 class GeneratedObjectAccessorService implements ObjectAccessorService { |
| 56 final Map<Symbol, Getter> _getters; |
| 57 final Map<Symbol, Setter> _setters; |
| 58 |
| 59 GeneratedObjectAccessorService(StaticConfiguration configuration) |
| 60 : _getters = configuration.getters, |
| 61 _setters = configuration.setters; |
| 62 |
59 read(Object object, Symbol name) { | 63 read(Object object, Symbol name) { |
60 var getter = _configuration.getters[name]; | 64 var getter = _getters[name]; |
61 if (getter == null) { | 65 if (getter == null) { |
62 throw new MissingCodeException('getter "$name" in $object'); | 66 throw new MissingCodeException('getter "$name" in $object'); |
63 } | 67 } |
64 return getter(object); | 68 return getter(object); |
65 } | 69 } |
66 void write(Object object, Symbol name, value) { | 70 void write(Object object, Symbol name, value) { |
67 var setter = _configuration.setters[name]; | 71 var setter = _setters[name]; |
68 if (setter == null) { | 72 if (setter == null) { |
69 throw new MissingCodeException('setter "$name" in $object'); | 73 throw new MissingCodeException('setter "$name" in $object'); |
70 } | 74 } |
71 setter(object, value); | 75 setter(object, value); |
72 } | 76 } |
73 | 77 |
74 invoke(object, Symbol name, List args, {Map namedArgs, bool adjust: false}) { | 78 invoke(object, Symbol name, List args, {Map namedArgs, bool adjust: false}) { |
75 var method; | 79 var method; |
76 if (object is Type) { | 80 if (object is Type) { |
77 } else { | 81 } else { |
78 var getter = _configuration.getters[name]; | 82 var getter = _getters[name]; |
79 method = getter == null ? null : getter(object); | 83 method = getter == null ? null : getter(object); |
80 } | 84 } |
81 if (method == null) { | 85 if (method == null) { |
82 throw new MissingCodeException('method "$name" in $object'); | 86 throw new MissingCodeException('method "$name" in $object'); |
83 } | 87 } |
84 var tentativeError; | 88 var tentativeError; |
85 if (adjust) { | 89 if (adjust) { |
86 var min = minArgs(method); | 90 var min = minArgs(method); |
87 if (min > SUPPORTED_ARGS) { | 91 if (min > SUPPORTED_ARGS) { |
88 tentativeError = 'we tried to adjust the arguments for calling "$name"' | 92 tentativeError = 'we tried to adjust the arguments for calling "$name"' |
(...skipping 17 matching lines...) Expand all Loading... |
106 // TODO(sigmund): consider whether this should just be in a logger or if | 110 // TODO(sigmund): consider whether this should just be in a logger or if |
107 // we should wrap `e` as a new exception (what's the best way to let users | 111 // we should wrap `e` as a new exception (what's the best way to let users |
108 // know about this tentativeError?) | 112 // know about this tentativeError?) |
109 if (tentativeError != null) print(tentativeError); | 113 if (tentativeError != null) print(tentativeError); |
110 rethrow; | 114 rethrow; |
111 } | 115 } |
112 } | 116 } |
113 } | 117 } |
114 | 118 |
115 /// Implements [TypeInspectorService] using a static configuration. | 119 /// Implements [TypeInspectorService] using a static configuration. |
116 class _GeneratedTypeInspectorService implements TypeInspectorService { | 120 class GeneratedTypeInspectorService implements TypeInspectorService { |
| 121 final Map<Type, Type> _parents; |
| 122 final Map<Type, Map<Symbol, Declaration>> _declarations; |
| 123 final bool _checkedMode; |
| 124 |
| 125 GeneratedTypeInspectorService(StaticConfiguration configuration) |
| 126 : _parents = configuration.parents, |
| 127 _declarations = configuration.declarations, |
| 128 _checkedMode = configuration.checkedMode; |
117 bool isSubclassOf(Type type, Type supertype) { | 129 bool isSubclassOf(Type type, Type supertype) { |
118 if (type == supertype || supertype == Object) return true; | 130 if (type == supertype || supertype == Object) return true; |
119 while (type != Object) { | 131 while (type != Object) { |
120 var parentType = _configuration.parents[type]; | 132 var parentType = _parents[type]; |
121 if (parentType == supertype) return true; | 133 if (parentType == supertype) return true; |
122 if (parentType == null) { | 134 if (parentType == null) { |
| 135 if (!_checkedMode) return false; |
123 throw new MissingCodeException('superclass of "$type" ($parentType)'); | 136 throw new MissingCodeException('superclass of "$type" ($parentType)'); |
124 } | 137 } |
125 type = parentType; | 138 type = parentType; |
126 } | 139 } |
127 return false; | 140 return false; |
128 } | 141 } |
129 | 142 |
130 bool hasGetter(Type type, Symbol name) { | 143 bool hasGetter(Type type, Symbol name) { |
131 var decl = _findDeclaration(type, name); | 144 var decl = _findDeclaration(type, name); |
132 // No need to check decl.isProperty because methods are also automatically | 145 // No need to check decl.isProperty because methods are also automatically |
133 // considered getters (auto-closures). | 146 // considered getters (auto-closures). |
134 return decl != null && !decl.isStatic; | 147 return decl != null && !decl.isStatic; |
135 } | 148 } |
136 | 149 |
137 bool hasSetter(Type type, Symbol name) { | 150 bool hasSetter(Type type, Symbol name) { |
138 var decl = _findDeclaration(type, name); | 151 var decl = _findDeclaration(type, name); |
139 return decl != null && !decl.isMethod && !decl.isFinal && !decl.isStatic; | 152 return decl != null && !decl.isMethod && !decl.isFinal && !decl.isStatic; |
140 } | 153 } |
141 | 154 |
142 bool hasInstanceMethod(Type type, Symbol name) { | 155 bool hasInstanceMethod(Type type, Symbol name) { |
143 var decl = _findDeclaration(type, name); | 156 var decl = _findDeclaration(type, name); |
144 return decl != null && decl.isMethod && !decl.isStatic; | 157 return decl != null && decl.isMethod && !decl.isStatic; |
145 } | 158 } |
146 | 159 |
147 bool hasStaticMethod(Type type, Symbol name) { | 160 bool hasStaticMethod(Type type, Symbol name) { |
148 final map = _configuration.declarations[type]; | 161 final map = _declarations[type]; |
149 if (map == null) { | 162 if (map == null) { |
| 163 if (!_checkedMode) return false; |
150 throw new MissingCodeException('declarations for $type'); | 164 throw new MissingCodeException('declarations for $type'); |
151 } | 165 } |
152 final decl = map[name]; | 166 final decl = map[name]; |
153 return decl != null && decl.isMethod && decl.isStatic; | 167 return decl != null && decl.isMethod && decl.isStatic; |
154 } | 168 } |
155 | 169 |
156 Declaration getDeclaration(Type type, Symbol name) { | 170 Declaration getDeclaration(Type type, Symbol name) { |
157 var decl = _findDeclaration(type, name); | 171 var decl = _findDeclaration(type, name); |
158 if (decl == null) { | 172 if (decl == null) { |
| 173 if (!_checkedMode) return null; |
159 throw new MissingCodeException('declaration for $type.$name'); | 174 throw new MissingCodeException('declaration for $type.$name'); |
160 } | 175 } |
161 return decl; | 176 return decl; |
162 } | 177 } |
163 | 178 |
164 List<Declaration> query(Type type, QueryOptions options) { | 179 List<Declaration> query(Type type, QueryOptions options) { |
165 var result; | 180 var result = []; |
166 if (options.includeInherited) { | 181 if (options.includeInherited) { |
167 var superclass = _configuration.parents[type]; | 182 var superclass = _parents[type]; |
168 if (superclass == null) { | 183 if (superclass == null) { |
169 throw new MissingCodeException('superclass of "$type"'); | 184 if (_checkedMode) { |
| 185 throw new MissingCodeException('superclass of "$type"'); |
| 186 } |
| 187 } else if (superclass != options.includeUpTo) { |
| 188 result = query(superclass, options); |
170 } | 189 } |
171 result = (superclass == options.includeUpTo) ? [] | |
172 : query(superclass, options); | |
173 } else { | |
174 result = []; | |
175 } | 190 } |
176 var map = _configuration.declarations[type]; | 191 var map = _declarations[type]; |
177 if (map == null) { | 192 if (map == null) { |
| 193 if (!_checkedMode) return result; |
178 throw new MissingCodeException('declarations for $type'); | 194 throw new MissingCodeException('declarations for $type'); |
179 } | 195 } |
180 for (var decl in map.values) { | 196 for (var decl in map.values) { |
181 if (!options.includeFields && decl.isField) continue; | 197 if (!options.includeFields && decl.isField) continue; |
182 if (!options.includeProperties && decl.isProperty) continue; | 198 if (!options.includeProperties && decl.isProperty) continue; |
183 if (options.excludeFinal && decl.isFinal) continue; | 199 if (options.excludeFinal && decl.isFinal) continue; |
184 if (!options.includeMethods && decl.isMethod) continue; | 200 if (!options.includeMethods && decl.isMethod) continue; |
| 201 if (options.matches != null && !options.matches(decl.name)) continue; |
185 if (options.withAnnotations != null && | 202 if (options.withAnnotations != null && |
186 !matchesAnnotation(decl.annotations, options.withAnnotations)) { | 203 !matchesAnnotation(decl.annotations, options.withAnnotations)) { |
187 continue; | 204 continue; |
188 } | 205 } |
189 result.add(decl); | 206 result.add(decl); |
190 } | 207 } |
191 return result; | 208 return result; |
192 } | 209 } |
| 210 |
| 211 Declaration _findDeclaration(Type type, Symbol name) { |
| 212 while (type != Object) { |
| 213 final declarations = _declarations[type]; |
| 214 if (declarations != null) { |
| 215 final declaration = declarations[name]; |
| 216 if (declaration != null) return declaration; |
| 217 } |
| 218 var parentType = _parents[type]; |
| 219 if (parentType == null) { |
| 220 if (!_checkedMode) return null; |
| 221 throw new MissingCodeException('superclass of "$type"'); |
| 222 } |
| 223 type = parentType; |
| 224 } |
| 225 return null; |
| 226 } |
193 } | 227 } |
194 | 228 |
195 /// Implements [SymbolConverterService] using a static configuration. | 229 /// Implements [SymbolConverterService] using a static configuration. |
196 class _GeneratedSymbolConverterService implements SymbolConverterService { | 230 class GeneratedSymbolConverterService implements SymbolConverterService { |
197 String symbolToName(Symbol symbol) => _configuration.names[symbol]; | 231 Map<Symbol, String> _names; |
198 Symbol nameToSymbol(String name) => _configuration.symbols[name]; | 232 |
| 233 /// A map from strings to symbols (the reverse of [names]). |
| 234 final Map<String, Symbol> _symbols; |
| 235 |
| 236 GeneratedSymbolConverterService(StaticConfiguration configuration) |
| 237 : _names = configuration.names, |
| 238 _symbols = {} { |
| 239 _names.forEach((k, v) { _symbols[v] = k; }); |
| 240 } |
| 241 |
| 242 String symbolToName(Symbol symbol) => _names[symbol]; |
| 243 Symbol nameToSymbol(String name) => _symbols[name]; |
199 } | 244 } |
200 | 245 |
201 | 246 |
202 /// Exception thrown when trynig to access something that should be there, but | 247 /// Exception thrown when trynig to access something that should be there, but |
203 /// the code generator didn't include it. | 248 /// the code generator didn't include it. |
204 class MissingCodeException implements Exception { | 249 class MissingCodeException implements Exception { |
205 final String description; | 250 final String description; |
206 MissingCodeException(this.description); | 251 MissingCodeException(this.description); |
207 | 252 |
208 String toString() => 'Missing $description. ' | 253 String toString() => 'Missing $description. ' |
209 'Code generation for the smoke package seems incomplete.'; | 254 'Code generation for the smoke package seems incomplete.'; |
210 } | 255 } |
211 | |
212 Declaration _findDeclaration(Type type, Symbol name) { | |
213 while (type != Object) { | |
214 final declarations = _configuration.declarations[type]; | |
215 if (declarations != null) { | |
216 final declaration = declarations[name]; | |
217 if (declaration != null) return declaration; | |
218 } | |
219 var parentType = _configuration.parents[type]; | |
220 if (parentType == null) { | |
221 throw new MissingCodeException('superclass of "$type"'); | |
222 } | |
223 type = parentType; | |
224 } | |
225 return null; | |
226 } | |
OLD | NEW |