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 /** | 5 /** |
6 * This part contains helpers for supporting runtime type information. | 6 * This part contains helpers for supporting runtime type information. |
7 * | 7 * |
8 * The helper use a mixture of Dart and JavaScript objects. To indicate which is | 8 * The helper use a mixture of Dart and JavaScript objects. To indicate which is |
9 * used where we adopt the scheme of using explicit type annotation for Dart | 9 * used where we adopt the scheme of using explicit type annotation for Dart |
10 * objects and 'var' or omitted return type for JavaScript objects. | 10 * objects and 'var' or omitted return type for JavaScript objects. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 * representation of type 4 or 5, that is, either a JavaScript array or | 89 * representation of type 4 or 5, that is, either a JavaScript array or |
90 * `null`. | 90 * `null`. |
91 */ | 91 */ |
92 // Don't inline. Let the JS engine inline this. The call expression is much | 92 // Don't inline. Let the JS engine inline this. The call expression is much |
93 // more compact that the inlined expansion. | 93 // more compact that the inlined expansion. |
94 // TODO(sra): For most objects it would be better to initialize the type info as | 94 // TODO(sra): For most objects it would be better to initialize the type info as |
95 // a field in the constructor: http://dartbug.com/22676 . | 95 // a field in the constructor: http://dartbug.com/22676 . |
96 @NoInline() | 96 @NoInline() |
97 Object setRuntimeTypeInfo(Object target, var rti) { | 97 Object setRuntimeTypeInfo(Object target, var rti) { |
98 assert(rti == null || isJsArray(rti)); | 98 assert(rti == null || isJsArray(rti)); |
99 JS('var', r'#.$builtinTypeInfo = #', target, rti); | 99 JS('var', r'#.$ti = #', target, rti); |
100 return target; | 100 return target; |
101 } | 101 } |
102 | 102 |
103 /** | 103 /** |
104 * Returns the runtime type information of [target]. The returned value is a | 104 * Returns the runtime type information of [target]. The returned value is a |
105 * list of type representations for the type arguments. | 105 * list of type representations for the type arguments. |
106 */ | 106 */ |
107 getRuntimeTypeInfo(Object target) { | 107 getRuntimeTypeInfo(Object target) { |
108 if (target == null) return null; | 108 if (target == null) return null; |
109 return JS('var', r'#.$builtinTypeInfo', target); | 109 return JS('var', r'#.$ti', target); |
110 } | 110 } |
111 | 111 |
112 /** | 112 /** |
113 * Returns the type arguments of [target] as an instance of [substitutionName]. | 113 * Returns the type arguments of [target] as an instance of [substitutionName]. |
114 */ | 114 */ |
115 getRuntimeTypeArguments(target, substitutionName) { | 115 getRuntimeTypeArguments(target, substitutionName) { |
116 var substitution = getField(target, | 116 var substitution = getField(target, |
117 '${JS_GET_NAME(JsGetName.OPERATOR_AS_PREFIX)}$substitutionName'); | 117 '${JS_GET_NAME(JsGetName.OPERATOR_AS_PREFIX)}$substitutionName'); |
118 return substitute(substitution, getRuntimeTypeInfo(target)); | 118 return substitute(substitution, getRuntimeTypeInfo(target)); |
119 } | 119 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 } | 205 } |
206 | 206 |
207 /** | 207 /** |
208 * Returns a human-readable representation of the type of [object]. | 208 * Returns a human-readable representation of the type of [object]. |
209 * | 209 * |
210 * In minified mode does *not* use unminified identifiers (even when present). | 210 * In minified mode does *not* use unminified identifiers (even when present). |
211 */ | 211 */ |
212 String getRuntimeTypeString(var object) { | 212 String getRuntimeTypeString(var object) { |
213 String className = getClassName(object); | 213 String className = getClassName(object); |
214 if (object == null) return className; | 214 if (object == null) return className; |
215 var rti = JS('var', r'#.$builtinTypeInfo', object); | 215 var rti = JS('var', r'#.$ti', object); |
216 return "$className${joinArguments(rti, 0)}"; | 216 return "$className${joinArguments(rti, 0)}"; |
217 } | 217 } |
218 | 218 |
219 Type getRuntimeType(var object) { | 219 Type getRuntimeType(var object) { |
220 String type = getRuntimeTypeString(object); | 220 String type = getRuntimeTypeString(object); |
221 return new TypeImpl(type); | 221 return new TypeImpl(type); |
222 } | 222 } |
223 | 223 |
224 /** | 224 /** |
225 * Applies the [substitution] on the [arguments]. | 225 * Applies the [substitution] on the [arguments]. |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 * `null` and `undefined` (which we can avoid). | 682 * `null` and `undefined` (which we can avoid). |
683 */ | 683 */ |
684 bool isIdentical(var s, var t) => JS('bool', '# === #', s, t); | 684 bool isIdentical(var s, var t) => JS('bool', '# === #', s, t); |
685 | 685 |
686 /** | 686 /** |
687 * Returns `true` if the JavaScript values [s] and [t] are not identical. We use | 687 * Returns `true` if the JavaScript values [s] and [t] are not identical. We use |
688 * this helper instead of [identical] because `identical` needs to merge | 688 * this helper instead of [identical] because `identical` needs to merge |
689 * `null` and `undefined` (which we can avoid). | 689 * `null` and `undefined` (which we can avoid). |
690 */ | 690 */ |
691 bool isNotIdentical(var s, var t) => JS('bool', '# !== #', s, t); | 691 bool isNotIdentical(var s, var t) => JS('bool', '# !== #', s, t); |
OLD | NEW |