Chromium Code Reviews| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 isSubtype(type, async.Stream) && isSubtype(actual, async.Stream) || | 184 isSubtype(type, async.Stream) && isSubtype(actual, async.Stream) || |
| 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) { | |
| 195 return types.isSubtype(rtti.realRuntimeType(obj), type); | |
| 196 } | |
| 197 exports.strongInstanceOf = strongInstanceOf; | |
| 198 | |
| 199 function instanceOfOrNull(obj, type) { | |
| 200 if ((obj == null) || strongInstanceOf(obj, type)) return true; | |
| 201 return false; | |
| 202 } | |
| 203 | |
| 194 function instanceOf(obj, type) { | 204 function instanceOf(obj, type) { |
| 195 return types.isSubtype(rtti.realRuntimeType(obj), type); | 205 if (strongInstanceOf(obj, type)) return true; |
| 206 // TODO(vsm): This is perhaps too eager to throw a StrongModeError? | |
| 207 // It will throw on <int>[] is List<String>. | |
| 208 // TODO(vsm): We can statically detect many cases where this | |
| 209 // check is unnecessary. | |
| 210 if (types.isGroundType(type)) return false; | |
| 211 let actual = rtti.realRuntimeType(obj); | |
| 212 dart_utils.throwStrongModeError('Strong mode cast failure from ' + | |
|
Leaf
2015/08/18 23:31:11
Calling this a "cast failure" is a bit misleading,
| |
| 213 types.typeName(actual) + ' to ' + types.typeName(type)); | |
| 196 } | 214 } |
| 197 exports.instanceOf = instanceOf; | 215 exports.instanceOf = instanceOf; |
| 198 | 216 |
| 199 function instanceOfOrNull(obj, type) { | |
| 200 if ((obj == null) || instanceOf(obj, type)) return true; | |
| 201 let actual = rtti.realRuntimeType(obj); | |
| 202 if (_ignoreTypeFailure(actual, type)) return true; | |
| 203 return false; | |
| 204 } | |
| 205 exports.instanceOfOrNull = instanceOfOrNull; | |
| 206 | |
| 207 function cast(obj, type) { | 217 function cast(obj, type) { |
| 208 // TODO(vsm): handle non-nullable types | 218 // TODO(vsm): handle non-nullable types |
| 209 if (obj == null) return obj; | 219 if (instanceOfOrNull(obj, type)) return obj; |
| 210 let actual = rtti.realRuntimeType(obj); | 220 let actual = rtti.realRuntimeType(obj); |
| 211 if (types.isSubtype(actual, type)) return obj; | 221 if (_ignoreTypeFailure(actual, type)) { |
| 212 if (_ignoreTypeFailure(actual, type)) return obj; | 222 // TODO(vsm): track why this is happening in our async / await tests. |
| 213 errors.throwCastError(actual, type); | 223 if (types.isGroundType(type)) { |
| 224 console.error('Should not ignore cast failure from ' + | |
| 225 types.typeName(actual) + ' to ' + types.typeName(type)); | |
| 226 } | |
| 227 return obj; | |
| 228 } | |
| 229 if (types.isGroundType(type)) { | |
| 230 errors.throwCastError(actual, type); | |
| 231 } | |
| 232 dart_utils.throwStrongModeError('Strong mode cast failure from ' + | |
| 233 types.typeName(actual) + ' to ' + types.typeName(type)); | |
| 214 } | 234 } |
| 215 exports.cast = cast; | 235 exports.cast = cast; |
| 216 | 236 |
| 217 function arity(f) { | 237 function arity(f) { |
| 218 // TODO(jmesserly): need to parse optional params. | 238 // TODO(jmesserly): need to parse optional params. |
| 219 // In ES6, length is the number of required arguments. | 239 // In ES6, length is the number of required arguments. |
| 220 return { min: f.length, max: f.length }; | 240 return { min: f.length, max: f.length }; |
| 221 } | 241 } |
| 222 exports.arity = arity; | 242 exports.arity = arity; |
| 223 | 243 |
| 224 function equals(x, y) { | 244 function equals(x, y) { |
| 225 if (x == null || y == null) return x == y; | 245 if (x == null || y == null) return x == y; |
| 226 let eq = x['==']; | 246 let eq = x['==']; |
| 227 return eq ? eq.call(x, y) : x === y; | 247 return eq ? eq.call(x, y) : x === y; |
| 228 } | 248 } |
| 229 exports.equals = equals; | 249 exports.equals = equals; |
| 230 | 250 |
| 231 /** Checks that `x` is not null or undefined. */ | 251 /** Checks that `x` is not null or undefined. */ |
| 232 function notNull(x) { | 252 function notNull(x) { |
| 233 // TODO(leafp): This is probably not the right error to throw. | 253 if (x == null) errors.throwNullValueError(); |
| 234 if (x == null) throwError('expected not-null value'); | |
| 235 return x; | 254 return x; |
| 236 } | 255 } |
| 237 exports.notNull = notNull; | 256 exports.notNull = notNull; |
| 238 | 257 |
| 239 /** | 258 /** |
| 240 * Creates a dart:collection LinkedHashMap. | 259 * Creates a dart:collection LinkedHashMap. |
| 241 * | 260 * |
| 242 * For a map with string keys an object literal can be used, for example | 261 * For a map with string keys an object literal can be used, for example |
| 243 * `map({'hi': 1, 'there': 2})`. | 262 * `map({'hi': 1, 'there': 2})`. |
| 244 * | 263 * |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 next() { | 433 next() { |
| 415 let i = this.dartIterator; | 434 let i = this.dartIterator; |
| 416 let done = !i.moveNext(); | 435 let done = !i.moveNext(); |
| 417 return { done: done, value: done ? void 0 : i.current }; | 436 return { done: done, value: done ? void 0 : i.current }; |
| 418 } | 437 } |
| 419 } | 438 } |
| 420 exports.JsIterator = JsIterator; | 439 exports.JsIterator = JsIterator; |
| 421 | 440 |
| 422 | 441 |
| 423 }); | 442 }); |
| OLD | NEW |