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 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 $isSubtype($type, $StreamSubscription) && | 188 $isSubtype($type, $StreamSubscription) && |
189 $isSubtype($actual, $StreamSubscription)) { | 189 $isSubtype($actual, $StreamSubscription)) { |
190 console.warn('Ignoring cast fail from ' + $typeName($actual) + | 190 console.warn('Ignoring cast fail from ' + $typeName($actual) + |
191 ' to ' + $typeName($type)); | 191 ' to ' + $typeName($type)); |
192 return true; | 192 return true; |
193 } | 193 } |
194 return false; | 194 return false; |
195 })()'''); | 195 })()'''); |
196 | 196 |
197 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { | 197 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { |
198 let actual = $realRuntimeType($obj); | 198 let actual = $getReifiedType($obj); |
199 if ($isSubtype(actual, $type) || actual == $jsobject || | 199 if ($isSubtype(actual, $type) || actual == $jsobject || |
200 actual == $int && type == $double) return true; | 200 actual == $int && type == $double) return true; |
201 if ($ignoreFromWhiteList == void 0) return false; | 201 if ($ignoreFromWhiteList == void 0) return false; |
202 if ($isGroundType($type)) return false; | 202 if ($isGroundType($type)) return false; |
203 if ($_ignoreTypeFailure(actual, $type)) return true; | 203 if ($_ignoreTypeFailure(actual, $type)) return true; |
204 return false; | 204 return false; |
205 })()'''); | 205 })()'''); |
206 | 206 |
207 instanceOfOrNull(obj, type) => JS('', '''(() => { | 207 instanceOfOrNull(obj, type) => JS('', '''(() => { |
208 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true; | 208 if (($obj == null) || $strongInstanceOf($obj, $type, true)) return true; |
209 return false; | 209 return false; |
210 })()'''); | 210 })()'''); |
211 | 211 |
212 @JSExportName('is') | 212 @JSExportName('is') |
213 instanceOf(obj, type) => JS('', '''(() => { | 213 instanceOf(obj, type) => JS('', '''(() => { |
214 if ($strongInstanceOf($obj, $type)) return true; | 214 if ($strongInstanceOf($obj, $type)) return true; |
215 // TODO(#296): This is perhaps too eager to throw a StrongModeError? | 215 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
216 // It will throw on <int>[] is List<String>. | 216 // It will throw on <int>[] is List<String>. |
217 // TODO(vsm): We can statically detect many cases where this | 217 // TODO(vsm): We can statically detect many cases where this |
218 // check is unnecessary. | 218 // check is unnecessary. |
219 if ($isGroundType($type)) return false; | 219 if ($isGroundType($type)) return false; |
220 let actual = $realRuntimeType($obj); | 220 let actual = $getReifiedType($obj); |
221 $throwStrongModeError('Strong mode is check failure: ' + | 221 $throwStrongModeError('Strong mode is check failure: ' + |
222 $typeName(actual) + ' does not soundly subtype ' + | 222 $typeName(actual) + ' does not soundly subtype ' + |
223 $typeName($type)); | 223 $typeName($type)); |
224 })()'''); | 224 })()'''); |
225 | 225 |
226 @JSExportName('as') | 226 @JSExportName('as') |
227 cast(obj, type) => JS('', '''(() => { | 227 cast(obj, type) => JS('', '''(() => { |
228 // TODO(#296): This is perhaps too eager to throw a StrongModeError? | 228 // TODO(#296): This is perhaps too eager to throw a StrongModeError? |
229 // TODO(vsm): handle non-nullable types | 229 // TODO(vsm): handle non-nullable types |
230 if ($instanceOfOrNull($obj, $type)) return $obj; | 230 if ($instanceOfOrNull($obj, $type)) return $obj; |
231 let actual = $realRuntimeType($obj); | 231 let actual = $getReifiedType($obj); |
232 if ($isGroundType($type)) $throwCastError(actual, $type); | 232 if ($isGroundType($type)) $throwCastError(actual, $type); |
233 | 233 |
234 if ($_ignoreTypeFailure(actual, $type)) return $obj; | 234 if ($_ignoreTypeFailure(actual, $type)) return $obj; |
235 | 235 |
236 $throwStrongModeError('Strong mode cast failure from ' + | 236 $throwStrongModeError('Strong mode cast failure from ' + |
237 $typeName(actual) + ' to ' + $typeName($type)); | 237 $typeName(actual) + ' to ' + $typeName($type)); |
238 })()'''); | 238 })()'''); |
239 | 239 |
240 asInt(obj) => JS('', '''(() => { | 240 asInt(obj) => JS('', '''(() => { |
241 if ($obj == null) { | 241 if ($obj == null) { |
242 return null; | 242 return null; |
243 } | 243 } |
244 if (Math.floor($obj) != $obj) { | 244 if (Math.floor($obj) != $obj) { |
245 // Note: null will also be caught by this check | 245 // Note: null will also be caught by this check |
246 $throwCastError($realRuntimeType($obj), $int); | 246 $throwCastError($getReifiedType($obj), $int); |
247 } | 247 } |
248 return $obj; | 248 return $obj; |
249 })()'''); | 249 })()'''); |
250 | 250 |
251 arity(f) => JS('', '''(() => { | 251 arity(f) => JS('', '''(() => { |
252 // TODO(jmesserly): need to parse optional params. | 252 // TODO(jmesserly): need to parse optional params. |
253 // In ES6, length is the number of required arguments. | 253 // In ES6, length is the number of required arguments. |
254 return { min: $f.length, max: $f.length }; | 254 return { min: $f.length, max: $f.length }; |
255 })()'''); | 255 })()'''); |
256 | 256 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 | 383 |
384 /// | 384 /// |
385 /// Canonicalize a constant object. | 385 /// Canonicalize a constant object. |
386 /// | 386 /// |
387 /// Preconditions: | 387 /// Preconditions: |
388 /// - `obj` is an objects or array, not a primitive. | 388 /// - `obj` is an objects or array, not a primitive. |
389 /// - nested values of the object are themselves already canonicalized. | 389 /// - nested values of the object are themselves already canonicalized. |
390 /// | 390 /// |
391 @JSExportName('const') | 391 @JSExportName('const') |
392 const_(obj) => JS('', '''(() => { | 392 const_(obj) => JS('', '''(() => { |
393 let objectKey = [$realRuntimeType($obj)]; | 393 let objectKey = [$getReifiedType($obj)]; |
394 // TODO(jmesserly): there's no guarantee in JS that names/symbols are | 394 // TODO(jmesserly): there's no guarantee in JS that names/symbols are |
395 // returned in the same order. | 395 // returned in the same order. |
396 // | 396 // |
397 // We could probably get the same order if we're judicious about | 397 // We could probably get the same order if we're judicious about |
398 // initializing fields in a consistent order across all const constructors. | 398 // initializing fields in a consistent order across all const constructors. |
399 // Alternatively we need a way to sort them to make consistent. | 399 // Alternatively we need a way to sort them to make consistent. |
400 // | 400 // |
401 // Right now we use the (name,value) pairs in sequence, which prevents | 401 // Right now we use the (name,value) pairs in sequence, which prevents |
402 // an object with incorrect field values being returned, but won't | 402 // an object with incorrect field values being returned, but won't |
403 // canonicalize correctly if key order is different. | 403 // canonicalize correctly if key order is different. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 constructor(dartIterator) { | 455 constructor(dartIterator) { |
456 this.dartIterator = dartIterator; | 456 this.dartIterator = dartIterator; |
457 } | 457 } |
458 next() { | 458 next() { |
459 let i = this.dartIterator; | 459 let i = this.dartIterator; |
460 let done = !i.moveNext(); | 460 let done = !i.moveNext(); |
461 return { done: done, value: done ? void 0 : i.current }; | 461 return { done: done, value: done ? void 0 : i.current }; |
462 } | 462 } |
463 } | 463 } |
464 '''); | 464 '''); |
OLD | NEW |