| 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 part of dart._runtime; | 7 part of dart._runtime; |
| 8 | 8 |
| 9 _canonicalFieldName(obj, name, args, displayName) => JS('', '''(() => { | 9 _canonicalFieldName(obj, name, args, displayName) => JS('', '''(() => { |
| 10 $name = $canonicalMember($obj, $name); | 10 $name = $canonicalMember($obj, $name); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 $isSubtype($type, $StreamSubscription) && | 160 $isSubtype($type, $StreamSubscription) && |
| 161 $isSubtype($actual, $StreamSubscription)) { | 161 $isSubtype($actual, $StreamSubscription)) { |
| 162 console.warn('Ignoring cast fail from ' + $typeName($actual) + | 162 console.warn('Ignoring cast fail from ' + $typeName($actual) + |
| 163 ' to ' + $typeName($type)); | 163 ' to ' + $typeName($type)); |
| 164 return true; | 164 return true; |
| 165 } | 165 } |
| 166 return false; | 166 return false; |
| 167 })()'''); | 167 })()'''); |
| 168 | 168 |
| 169 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { | 169 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { |
| 170 let actual = $realRuntimeType($obj); | 170 let actual = $getReifiedType($obj); |
| 171 if ($isSubtype(actual, $type) || actual == $jsobject || | 171 if ($isSubtype(actual, $type) || actual == $jsobject || |
| 172 actual == $int && type == $double) return true; | 172 actual == $int && type == $double) return true; |
| 173 if ($ignoreFromWhiteList == void 0) return false; | 173 if ($ignoreFromWhiteList == void 0) return false; |
| 174 if ($isGroundType($type)) return false; | 174 if ($isGroundType($type)) return false; |
| 175 if ($_ignoreTypeFailure(actual, $type)) return true; | 175 if ($_ignoreTypeFailure(actual, $type)) return true; |
| 176 return false; | 176 return false; |
| 177 })()'''); | 177 })()'''); |
| 178 | 178 |
| 179 instanceOfOrNull(obj, type) => JS('', '''(() => { | 179 instanceOfOrNull(obj, type) => JS('', '''(() => { |
| 180 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true; | 180 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true; |
| 181 return false; | 181 return false; |
| 182 })()'''); | 182 })()'''); |
| 183 | 183 |
| 184 @JSExportName('is') | 184 @JSExportName('is') |
| 185 instanceOf(obj, type) => JS('', '''(() => { | 185 instanceOf(obj, type) => JS('', '''(() => { |
| 186 if ($strongInstanceOf($obj, $type)) return true; | 186 if ($strongInstanceOf($obj, $type)) return true; |
| 187 // TODO(#296): This is perhaps too eager to throw a StrongModeError? | 187 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
| 188 // It will throw on <int>[] is List<String>. | 188 // It will throw on <int>[] is List<String>. |
| 189 // TODO(vsm): We can statically detect many cases where this | 189 // TODO(vsm): We can statically detect many cases where this |
| 190 // check is unnecessary. | 190 // check is unnecessary. |
| 191 if ($isGroundType($type)) return false; | 191 if ($isGroundType($type)) return false; |
| 192 let actual = $realRuntimeType($obj); | 192 let actual = $getReifiedType($obj); |
| 193 $throwStrongModeError('Strong mode is check failure: ' + | 193 $throwStrongModeError('Strong mode is check failure: ' + |
| 194 $typeName(actual) + ' does not soundly subtype ' + | 194 $typeName(actual) + ' does not soundly subtype ' + |
| 195 $typeName($type)); | 195 $typeName($type)); |
| 196 })()'''); | 196 })()'''); |
| 197 | 197 |
| 198 @JSExportName('as') | 198 @JSExportName('as') |
| 199 cast(obj, type) => JS('', '''(() => { | 199 cast(obj, type) => JS('', '''(() => { |
| 200 // TODO(#296): This is perhaps too eager to throw a StrongModeError? | 200 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
| 201 // TODO(vsm): handle non-nullable types | 201 // TODO(vsm): handle non-nullable types |
| 202 if ($instanceOfOrNull($obj, $type)) return $obj; | 202 if ($instanceOfOrNull($obj, $type)) return $obj; |
| 203 let actual = $realRuntimeType($obj); | 203 let actual = $getReifiedType($obj); |
| 204 if ($isGroundType($type)) $throwCastError(actual, $type); | 204 if ($isGroundType($type)) $throwCastError(actual, $type); |
| 205 | 205 |
| 206 if ($_ignoreTypeFailure(actual, $type)) return $obj; | 206 if ($_ignoreTypeFailure(actual, $type)) return $obj; |
| 207 | 207 |
| 208 $throwStrongModeError('Strong mode cast failure from ' + | 208 $throwStrongModeError('Strong mode cast failure from ' + |
| 209 $typeName(actual) + ' to ' + $typeName($type)); | 209 $typeName(actual) + ' to ' + $typeName($type)); |
| 210 })()'''); | 210 })()'''); |
| 211 | 211 |
| 212 asInt(obj) => JS('', '''(() => { | 212 asInt(obj) => JS('', '''(() => { |
| 213 if ($obj == null) { | 213 if ($obj == null) { |
| 214 return null; | 214 return null; |
| 215 } | 215 } |
| 216 if (Math.floor($obj) != $obj) { | 216 if (Math.floor($obj) != $obj) { |
| 217 // Note: null will also be caught by this check | 217 // Note: null will also be caught by this check |
| 218 $throwCastError($realRuntimeType($obj), $int); | 218 $throwCastError($getReifiedType($obj), $int); |
| 219 } | 219 } |
| 220 return $obj; | 220 return $obj; |
| 221 })()'''); | 221 })()'''); |
| 222 | 222 |
| 223 arity(f) => JS('', '''(() => { | 223 arity(f) => JS('', '''(() => { |
| 224 // TODO(jmesserly): need to parse optional params. | 224 // TODO(jmesserly): need to parse optional params. |
| 225 // In ES6, length is the number of required arguments. | 225 // In ES6, length is the number of required arguments. |
| 226 return { min: $f.length, max: $f.length }; | 226 return { min: $f.length, max: $f.length }; |
| 227 })()'''); | 227 })()'''); |
| 228 | 228 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 355 |
| 356 /// | 356 /// |
| 357 /// Canonicalize a constant object. | 357 /// Canonicalize a constant object. |
| 358 /// | 358 /// |
| 359 /// Preconditions: | 359 /// Preconditions: |
| 360 /// - `obj` is an objects or array, not a primitive. | 360 /// - `obj` is an objects or array, not a primitive. |
| 361 /// - nested values of the object are themselves already canonicalized. | 361 /// - nested values of the object are themselves already canonicalized. |
| 362 /// | 362 /// |
| 363 @JSExportName('const') | 363 @JSExportName('const') |
| 364 const_(obj) => JS('', '''(() => { | 364 const_(obj) => JS('', '''(() => { |
| 365 let objectKey = [$realRuntimeType($obj)]; | 365 let objectKey = [$getReifiedType($obj)]; |
| 366 // TODO(jmesserly): there's no guarantee in JS that names/symbols are | 366 // TODO(jmesserly): there's no guarantee in JS that names/symbols are |
| 367 // returned in the same order. | 367 // returned in the same order. |
| 368 // | 368 // |
| 369 // We could probably get the same order if we're judicious about | 369 // We could probably get the same order if we're judicious about |
| 370 // initializing fields in a consistent order across all const constructors. | 370 // initializing fields in a consistent order across all const constructors. |
| 371 // Alternatively we need a way to sort them to make consistent. | 371 // Alternatively we need a way to sort them to make consistent. |
| 372 // | 372 // |
| 373 // Right now we use the (name,value) pairs in sequence, which prevents | 373 // Right now we use the (name,value) pairs in sequence, which prevents |
| 374 // an object with incorrect field values being returned, but won't | 374 // an object with incorrect field values being returned, but won't |
| 375 // canonicalize correctly if key order is different. | 375 // canonicalize correctly if key order is different. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 constructor(dartIterator) { | 427 constructor(dartIterator) { |
| 428 this.dartIterator = dartIterator; | 428 this.dartIterator = dartIterator; |
| 429 } | 429 } |
| 430 next() { | 430 next() { |
| 431 let i = this.dartIterator; | 431 let i = this.dartIterator; |
| 432 let done = !i.moveNext(); | 432 let done = !i.moveNext(); |
| 433 return { done: done, value: done ? void 0 : i.current }; | 433 return { done: done, value: done ? void 0 : i.current }; |
| 434 } | 434 } |
| 435 } | 435 } |
| 436 '''); | 436 '''); |
| OLD | NEW |