| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /* This library defines runtime operations on objects used by the code | 5 /* This library defines runtime operations on objects used by the code |
| 6 * generator. | 6 * generator. |
| 7 */ | 7 */ |
| 8 dart_library.library('dart_runtime/_operations', null, /* Imports */[ | 8 dart_library.library('dart_runtime/_operations', null, /* Imports */[ |
| 9 ], /* Lazy Imports */[ | 9 ], /* Lazy Imports */[ |
| 10 'dart/async', | 10 'dart/async', |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 isSubtype(type, async.StreamSubscription) && | 185 isSubtype(type, async.StreamSubscription) && |
| 186 isSubtype(actual, async.StreamSubscription)) { | 186 isSubtype(actual, async.StreamSubscription)) { |
| 187 console.warn('Ignoring cast fail from ' + types.typeName(actual) + | 187 console.warn('Ignoring cast fail from ' + types.typeName(actual) + |
| 188 ' to ' + types.typeName(type)); | 188 ' to ' + types.typeName(type)); |
| 189 return true; | 189 return true; |
| 190 } | 190 } |
| 191 return false; | 191 return false; |
| 192 } | 192 } |
| 193 | 193 |
| 194 function strongInstanceOf(obj, type) { | 194 function strongInstanceOf(obj, type) { |
| 195 return types.isSubtype(rtti.realRuntimeType(obj), type); | 195 let actual = rtti.realRuntimeType(obj); |
| 196 return types.isSubtype(actual, type) || actual == types.jsobject; |
| 196 } | 197 } |
| 197 exports.strongInstanceOf = strongInstanceOf; | 198 exports.strongInstanceOf = strongInstanceOf; |
| 198 | 199 |
| 199 function instanceOfOrNull(obj, type) { | 200 function instanceOfOrNull(obj, type) { |
| 200 if ((obj == null) || strongInstanceOf(obj, type)) return true; | 201 if ((obj == null) || strongInstanceOf(obj, type)) return true; |
| 201 return false; | 202 return false; |
| 202 } | 203 } |
| 203 | 204 |
| 204 function instanceOf(obj, type) { | 205 function instanceOf(obj, type) { |
| 205 if (strongInstanceOf(obj, type)) return true; | 206 if (strongInstanceOf(obj, type)) return true; |
| 206 // TODO(vsm): This is perhaps too eager to throw a StrongModeError? | 207 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
| 207 // It will throw on <int>[] is List<String>. | 208 // It will throw on <int>[] is List<String>. |
| 208 // TODO(vsm): We can statically detect many cases where this | 209 // TODO(vsm): We can statically detect many cases where this |
| 209 // check is unnecessary. | 210 // check is unnecessary. |
| 210 if (types.isGroundType(type)) return false; | 211 if (types.isGroundType(type)) return false; |
| 211 let actual = rtti.realRuntimeType(obj); | 212 let actual = rtti.realRuntimeType(obj); |
| 212 dart_utils.throwStrongModeError('Strong mode is check failure: ' + | 213 dart_utils.throwStrongModeError('Strong mode is check failure: ' + |
| 213 types.typeName(actual) + ' does not soundly subtype ' + | 214 types.typeName(actual) + ' does not soundly subtype ' + |
| 214 types.typeName(type)); | 215 types.typeName(type)); |
| 215 } | 216 } |
| 216 exports.instanceOf = instanceOf; | 217 exports.instanceOf = instanceOf; |
| 217 | 218 |
| 218 function cast(obj, type) { | 219 function cast(obj, type) { |
| 220 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
| 219 // TODO(vsm): handle non-nullable types | 221 // TODO(vsm): handle non-nullable types |
| 220 if (instanceOfOrNull(obj, type)) return obj; | 222 if (instanceOfOrNull(obj, type)) return obj; |
| 221 let actual = rtti.realRuntimeType(obj); | 223 let actual = rtti.realRuntimeType(obj); |
| 222 if (_ignoreTypeFailure(actual, type)) { | 224 if (types.isGroundType(type)) errors.throwCastError(actual, type); |
| 223 // TODO(vsm): track why this is happening in our async / await tests. | 225 |
| 224 if (types.isGroundType(type)) { | 226 if (_ignoreTypeFailure(actual, type)) return obj; |
| 225 console.error('Should not ignore cast failure from ' + | 227 |
| 226 types.typeName(actual) + ' to ' + types.typeName(type)); | |
| 227 } | |
| 228 return obj; | |
| 229 } | |
| 230 if (types.isGroundType(type)) { | |
| 231 errors.throwCastError(actual, type); | |
| 232 } | |
| 233 dart_utils.throwStrongModeError('Strong mode cast failure from ' + | 228 dart_utils.throwStrongModeError('Strong mode cast failure from ' + |
| 234 types.typeName(actual) + ' to ' + types.typeName(type)); | 229 types.typeName(actual) + ' to ' + types.typeName(type)); |
| 235 } | 230 } |
| 236 exports.cast = cast; | 231 exports.cast = cast; |
| 237 | 232 |
| 238 function arity(f) { | 233 function arity(f) { |
| 239 // TODO(jmesserly): need to parse optional params. | 234 // TODO(jmesserly): need to parse optional params. |
| 240 // In ES6, length is the number of required arguments. | 235 // In ES6, length is the number of required arguments. |
| 241 return { min: f.length, max: f.length }; | 236 return { min: f.length, max: f.length }; |
| 242 } | 237 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 next() { | 429 next() { |
| 435 let i = this.dartIterator; | 430 let i = this.dartIterator; |
| 436 let done = !i.moveNext(); | 431 let done = !i.moveNext(); |
| 437 return { done: done, value: done ? void 0 : i.current }; | 432 return { done: done, value: done ? void 0 : i.current }; |
| 438 } | 433 } |
| 439 } | 434 } |
| 440 exports.JsIterator = JsIterator; | 435 exports.JsIterator = JsIterator; |
| 441 | 436 |
| 442 | 437 |
| 443 }); | 438 }); |
| OLD | NEW |