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 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 214 } |
215 return false; | 215 return false; |
216 }); | 216 }); |
217 })()'''); | 217 })()'''); |
218 | 218 |
219 /// Returns true if [obj] is an instance of [type] | 219 /// Returns true if [obj] is an instance of [type] |
220 /// Returns false if [obj] is not an instance of [type] in both spec | 220 /// Returns false if [obj] is not an instance of [type] in both spec |
221 /// and strong mode | 221 /// and strong mode |
222 /// Returns null if [obj] is not an instance of [type] in strong mode | 222 /// Returns null if [obj] is not an instance of [type] in strong mode |
223 /// but might be in spec mode | 223 /// but might be in spec mode |
224 strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { | 224 bool strongInstanceOf(obj, type, ignoreFromWhiteList) => JS('', '''(() => { |
225 let actual = $getReifiedType($obj); | 225 let actual = $getReifiedType($obj); |
226 let result = $isSubtype(actual, $type); | 226 let result = $isSubtype(actual, $type); |
227 if (result || actual == $jsobject || | 227 if (result || actual == $jsobject || |
228 actual == $int && type == $double) return true; | 228 actual == $int && type == $double) return true; |
229 if (result === false) return false; | 229 if (result === false) return false; |
230 if ($ignoreFromWhiteList == void 0) return result; | 230 if ($ignoreFromWhiteList == void 0) return result; |
231 if ($_ignoreTypeFailure(actual, $type)) return true; | 231 if ($_ignoreTypeFailure(actual, $type)) return true; |
232 return result; | 232 return result; |
233 })()'''); | 233 })()'''); |
234 | 234 |
(...skipping 10 matching lines...) Expand all Loading... |
245 instanceOf(obj, type) => JS('', '''(() => { | 245 instanceOf(obj, type) => JS('', '''(() => { |
246 let result = $strongInstanceOf($obj, $type); | 246 let result = $strongInstanceOf($obj, $type); |
247 if (result !== null) return result; | 247 if (result !== null) return result; |
248 let actual = $getReifiedType($obj); | 248 let actual = $getReifiedType($obj); |
249 $throwStrongModeError('Strong mode is check failure: ' + | 249 $throwStrongModeError('Strong mode is check failure: ' + |
250 $typeName(actual) + ' does not soundly subtype ' + | 250 $typeName(actual) + ' does not soundly subtype ' + |
251 $typeName($type)); | 251 $typeName($type)); |
252 })()'''); | 252 })()'''); |
253 | 253 |
254 @JSExportName('as') | 254 @JSExportName('as') |
255 cast(obj, type) => JS('', '''(() => { | 255 cast(obj, type) { |
256 if ($obj == null) return $obj; | 256 if (obj == null) return obj; |
257 | 257 |
258 let result = $strongInstanceOf($obj, $type, true); | 258 bool result = strongInstanceOf(obj, type, true); |
259 if (result) return $obj; | 259 if (JS('bool', '#', result)) return obj; |
| 260 _throwCastError(obj, type, result); |
| 261 } |
260 | 262 |
261 let actual = $getReifiedType($obj); | 263 asNotNull(obj, type) { |
| 264 if (obj == null) throwNullValueError(); |
262 | 265 |
263 if (result === false) $throwCastError(actual, $type); | 266 var result = strongInstanceOf(obj, type, true); |
| 267 if (JS('bool', '#', result)) return obj; |
| 268 _throwCastError(obj, type, result); |
| 269 } |
264 | 270 |
265 $throwStrongModeError('Strong mode cast failure from ' + | 271 bool test(obj) { |
266 $typeName(actual) + ' to ' + $typeName($type)); | 272 if (JS('bool', 'typeof # == "boolean"', obj)) return JS('bool', '#', obj); |
267 })()'''); | 273 throwCastError(getReifiedType(obj), JS('', '#', bool)); |
| 274 } |
268 | 275 |
269 asInt(obj) => JS('', '''(() => { | 276 void _throwCastError(obj, type, bool result) { |
270 if ($obj == null) { | 277 var actual = getReifiedType(obj); |
271 return null; | 278 if (result == false) throwCastError(actual, type); |
| 279 |
| 280 throwStrongModeError('Strong mode cast failure from ' + |
| 281 typeName(actual) + ' to ' + typeName(type)); |
| 282 } |
| 283 |
| 284 asInt(obj) { |
| 285 if (obj == null) return null; |
| 286 |
| 287 if (JS('bool', 'Math.floor(#) != #', obj, obj)) { |
| 288 throwCastError(getReifiedType(obj), int); |
272 } | 289 } |
273 if (Math.floor($obj) != $obj) { | 290 return obj; |
274 // Note: null will also be caught by this check | 291 } |
275 $throwCastError($getReifiedType($obj), $int); | |
276 } | |
277 return $obj; | |
278 })()'''); | |
279 | |
280 arity(f) => JS('', '''(() => { | |
281 // TODO(jmesserly): need to parse optional params. | |
282 // In ES6, length is the number of required arguments. | |
283 return { min: $f.length, max: $f.length }; | |
284 })()'''); | |
285 | 292 |
286 equals(x, y) => JS('', '''(() => { | 293 equals(x, y) => JS('', '''(() => { |
287 if ($x == null || $y == null) return $x == $y; | 294 if ($x == null || $y == null) return $x == $y; |
288 let eq = $x['==']; | 295 let eq = $x['==']; |
289 return eq ? eq.call($x, $y) : $x === $y; | 296 return eq ? eq.call($x, $y) : $x === $y; |
290 })()'''); | 297 })()'''); |
291 | 298 |
292 /// Checks that `x` is not null or undefined. */ | 299 /// Checks that `x` is not null or undefined. */ |
293 notNull(x) { | 300 notNull(x) { |
294 if (x == null) throwNullValueError(); | 301 if (x == null) throwNullValueError(); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 constructor(dartIterator) { | 512 constructor(dartIterator) { |
506 this.dartIterator = dartIterator; | 513 this.dartIterator = dartIterator; |
507 } | 514 } |
508 next() { | 515 next() { |
509 let i = this.dartIterator; | 516 let i = this.dartIterator; |
510 let done = !i.moveNext(); | 517 let done = !i.moveNext(); |
511 return { done: done, value: done ? void 0 : i.current }; | 518 return { done: done, value: done ? void 0 : i.current }; |
512 } | 519 } |
513 } | 520 } |
514 '''); | 521 '''); |
OLD | NEW |