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 /// Implementation of the smoke services using mirrors. | 5 /// Implementation of the smoke services using mirrors. |
6 library smoke.mirrors; | 6 library smoke.mirrors; |
7 | 7 |
8 import 'dart:mirrors'; | 8 import 'dart:mirrors'; |
9 import 'package:smoke/smoke.dart'; | 9 import 'package:smoke/smoke.dart'; |
10 import 'package:logging/logging.dart'; | 10 import 'package:logging/logging.dart'; |
11 import 'src/common.dart'; | 11 import 'src/common.dart'; |
12 | 12 |
13 /// Set up the smoke package to use a mirror-based implementation. To tune what | 13 /// Set up the smoke package to use a mirror-based implementation. To tune what |
14 /// is preserved by `dart:mirrors`, use a @MirrorsUsed annotation and include | 14 /// is preserved by `dart:mirrors`, use a @MirrorsUsed annotation and include |
15 /// 'smoke.mirrors' in your override arguments. | 15 /// 'smoke.mirrors' in your override arguments. |
16 useMirrors() { | 16 useMirrors() { |
17 configure(new ReflectiveObjectAccessorService(), | 17 configure(new ReflectiveObjectAccessorService(), |
18 new ReflectiveTypeInspectorService(), | 18 new ReflectiveTypeInspectorService(), |
19 new ReflectiveSymbolConverterService()); | 19 new ReflectiveSymbolConverterService()); |
20 } | 20 } |
21 | 21 |
22 var _logger = new Logger('smoke.mirrors'); | 22 var _logger = new Logger('smoke.mirrors'); |
23 | 23 |
24 | 24 |
25 /// Implements [ObjectAccessorService] using mirrors. | 25 /// Implements [ObjectAccessorService] using mirrors. |
26 class ReflectiveObjectAccessorService implements ObjectAccessorService { | 26 class ReflectiveObjectAccessorService implements ObjectAccessorService { |
27 read(Object object, Symbol name) { | 27 read(Object object, Symbol name) => reflect(object).getField(name).reflectee; |
28 var decl = getDeclaration(object.runtimeType, name); | |
29 if (decl != null && decl.isMethod) { | |
30 // TODO(sigmund,jmesserly): remove this once dartbug.com/13002 is fixed. | |
31 return new _MethodClosure(object, name); | |
32 } else { | |
33 return reflect(object).getField(name).reflectee; | |
34 } | |
35 } | |
36 | 28 |
37 void write(Object object, Symbol name, value) { | 29 void write(Object object, Symbol name, value) { |
38 reflect(object).setField(name, value); | 30 reflect(object).setField(name, value); |
39 } | 31 } |
40 | 32 |
41 invoke(receiver, Symbol methodName, List args, | 33 invoke(receiver, Symbol methodName, List args, |
42 {Map namedArgs, bool adjust: false}) { | 34 {Map namedArgs, bool adjust: false}) { |
43 var receiverMirror; | 35 var receiverMirror; |
44 var method; | 36 var method; |
45 if (receiver is Type) { | 37 if (receiver is Type) { |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 String toString() => (new StringBuffer() | 311 String toString() => (new StringBuffer() |
320 ..write('(mirror-based-declaration ') | 312 ..write('(mirror-based-declaration ') |
321 ..write(name) | 313 ..write(name) |
322 ..write(isField ? ' (field) ' | 314 ..write(isField ? ' (field) ' |
323 : (isProperty ? ' (property) ' : ' (method) ')) | 315 : (isProperty ? ' (property) ' : ' (method) ')) |
324 ..write(isFinal ? 'final ' : '') | 316 ..write(isFinal ? 'final ' : '') |
325 ..write(isStatic ? 'static ' : '') | 317 ..write(isStatic ? 'static ' : '') |
326 ..write(annotations) | 318 ..write(annotations) |
327 ..write(')')).toString(); | 319 ..write(')')).toString(); |
328 } | 320 } |
329 | |
330 class _MethodClosure extends Function { | |
331 final receiver; | |
332 final Symbol methodName; | |
333 | |
334 _MethodClosure(this.receiver, this.methodName); | |
335 | |
336 // Technically we could just use noSuchMethod to implement [call], but we | |
337 // don't for 3 reasons: | |
338 // * noSuchMethod makes the code a lot bigger. | |
339 // * even with noSuchMethod, an analyzer bug requires to declare [call] (see | |
340 // dartbug.com/16078). | |
341 // * having [call] also allows `is` checks to work for functions of 0 | |
342 // through 3 arguments. We depend on this in | |
343 // `polymer_expressions/lib/eval.dart` to check whether a function is a | |
344 // filter (takes a single argument). (Note that it's possible to include | |
345 // both [call] and [noSuchMethod], which would make instance-of checks | |
346 // work with the signature of [call], but will allow invoking the function | |
347 // using [noSuchMethod]. | |
348 call([a, b, c]) => invoke(receiver, methodName, [a, b, c], adjust: true); | |
349 } | |
OLD | NEW |