OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library kernel.transformations.reify.runtime.interceptors; |
| 6 |
| 7 import 'types.dart' show ReifiedType; |
| 8 |
| 9 /// Helper method that generates a [ReifiedType] corresponding to the literal |
| 10 /// type [type]. |
| 11 /// |
| 12 /// Calls to this method are recognized in the transformation and replaced by |
| 13 /// code that constructs the correct type. |
| 14 ReifiedType reify(Type type) { |
| 15 throw "This method should never be called in translated code"; |
| 16 } |
| 17 |
| 18 /// Marker interface to indicate that we can extract the runtime type using |
| 19 /// a property getter. |
| 20 abstract class HasRuntimeTypeGetter { |
| 21 ReifiedType get $type; |
| 22 } |
| 23 |
| 24 /// Interceptor to safely access the type of an object. |
| 25 /// |
| 26 /// For native objects that do not have the `$type` field on them, the |
| 27 /// interceptor directly returns the type, otherwise the value of the field is |
| 28 /// returned. |
| 29 ReifiedType type(dynamic o) { |
| 30 if (o == null) { |
| 31 return reify(Null); |
| 32 } else if (o is HasRuntimeTypeGetter) { |
| 33 return o.$type; |
| 34 } else if (o is bool) { |
| 35 return reify(bool); |
| 36 } else if (o is String) { |
| 37 return reify(String); |
| 38 } else if (o is int) { |
| 39 return reify(int); |
| 40 } else if (o is double) { |
| 41 return reify(double); |
| 42 } else if (o is Type) { |
| 43 return reify(Type); |
| 44 } else if (o is AbstractClassInstantiationError) { |
| 45 return reify(AbstractClassInstantiationError); |
| 46 } else if (o is NoSuchMethodError) { |
| 47 return reify(NoSuchMethodError); |
| 48 } else if (o is CyclicInitializationError) { |
| 49 return reify(CyclicInitializationError); |
| 50 } else if (o is UnsupportedError) { |
| 51 return reify(UnsupportedError); |
| 52 } else if (o is IntegerDivisionByZeroException) { |
| 53 return reify(IntegerDivisionByZeroException); |
| 54 } else if (o is RangeError) { |
| 55 return reify(RangeError); |
| 56 } else if (o is ArgumentError) { |
| 57 return reify(ArgumentError); |
| 58 } |
| 59 ReifiedType type = _type[o]; |
| 60 if (type != null) { |
| 61 return type; |
| 62 } |
| 63 throw 'Unable to get runtime type of ${o.runtimeType}'; |
| 64 } |
| 65 |
| 66 // This constructor call is not intercepted with [attachType] since the runtime |
| 67 // library is currently never transformed. |
| 68 final Expando _type = new Expando(); |
| 69 |
| 70 dynamic attachType(Object o, ReifiedType t) { |
| 71 _type[o] = t; |
| 72 return o; |
| 73 } |
OLD | NEW |