OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library _js_helper; | 5 library _js_helper; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 part 'constant_map.dart'; | 9 part 'constant_map.dart'; |
10 part 'native_helper.dart'; | 10 part 'native_helper.dart'; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 : identical(ge$slow(a, b), true); | 54 : identical(ge$slow(a, b), true); |
55 | 55 |
56 ltB(var a, var b) => (a is num && b is num) | 56 ltB(var a, var b) => (a is num && b is num) |
57 ? JS('bool', r'# < #', a, b) | 57 ? JS('bool', r'# < #', a, b) |
58 : identical(lt$slow(a, b), true); | 58 : identical(lt$slow(a, b), true); |
59 | 59 |
60 leB(var a, var b) => (a is num && b is num) | 60 leB(var a, var b) => (a is num && b is num) |
61 ? JS('bool', r'# <= #', a, b) | 61 ? JS('bool', r'# <= #', a, b) |
62 : identical(le$slow(a, b), true); | 62 : identical(le$slow(a, b), true); |
63 | 63 |
64 index(var a, var index) { | |
65 // The type test may cause a NoSuchMethodError to be thrown but | |
66 // that matches the specification of what the indexing operator is | |
67 // supposed to do. | |
68 bool isJsArrayOrString = JS('bool', | |
69 r'typeof # == "string" || #.constructor === Array', | |
70 a, a); | |
71 if (isJsArrayOrString) { | |
72 var key = JS('int', '# >>> 0', index); | |
73 if (identical(key, index) && key < JS('int', r'#.length', a)) { | |
74 return JS('var', r'#[#]', a, key); | |
75 } | |
76 } | |
77 return index$slow(a, index); | |
78 } | |
79 | |
80 indexSet(var a, var index, var value) { | |
81 // The type test may cause a NoSuchMethodError to be thrown but | |
82 // that matches the specification of what the indexing operator is | |
83 // supposed to do. | |
84 bool isMutableJsArray = JS('bool', | |
85 r'#.constructor === Array && !#.immutable$list', | |
86 a, a); | |
87 if (isMutableJsArray) { | |
88 var key = JS('int', '# >>> 0', index); | |
89 if (identical(key, index) && key < JS('int', r'#.length', a)) { | |
90 JS('void', r'#[#] = #', a, key, value); | |
91 return; | |
92 } | |
93 } | |
94 indexSet$slow(a, index, value); | |
95 } | |
96 | |
97 /** | 64 /** |
98 * Returns true if both arguments are numbers. | 65 * Returns true if both arguments are numbers. |
99 * | 66 * |
100 * If only the first argument is a number, an | 67 * If only the first argument is a number, an |
101 * [ArgumentError] with the other argument is thrown. | 68 * [ArgumentError] with the other argument is thrown. |
102 */ | 69 */ |
103 bool checkNumbers(var a, var b) { | 70 bool checkNumbers(var a, var b) { |
104 if (a is num) { | 71 if (a is num) { |
105 if (b is num) { | 72 if (b is num) { |
106 return true; | 73 return true; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 } | 240 } |
274 | 241 |
275 xor(var a, var b) { | 242 xor(var a, var b) { |
276 // TODO(floitsch): inputs must be integers. | 243 // TODO(floitsch): inputs must be integers. |
277 if (checkNumbers(a, b)) { | 244 if (checkNumbers(a, b)) { |
278 return JS('num', r'(# ^ #) >>> 0', a, b); | 245 return JS('num', r'(# ^ #) >>> 0', a, b); |
279 } | 246 } |
280 return UNINTERCEPTED(a ^ b); | 247 return UNINTERCEPTED(a ^ b); |
281 } | 248 } |
282 | 249 |
283 not(var a) { | |
284 if (JS('bool', r'typeof # === "number"', a)) { | |
285 return JS('num', r'(~#) >>> 0', a); | |
286 } | |
287 return UNINTERCEPTED(~a); | |
288 } | |
289 | |
290 neg(var a) { | |
291 if (JS('bool', r'typeof # === "number"', a)) return JS('num', r'-#', a); | |
292 return UNINTERCEPTED(-a); | |
293 } | |
294 | |
295 index$slow(var a, var index) { | |
296 if (a is String || isJsArray(a)) { | |
297 if (index is !int) { | |
298 if (index is !num) throw new ArgumentError(index); | |
299 if (!identical(index.truncate(), index)) throw new ArgumentError(index); | |
300 } | |
301 if (index < 0 || index >= a.length) { | |
302 throw new RangeError.value(index); | |
303 } | |
304 return JS('', r'#[#]', a, index); | |
305 } | |
306 return UNINTERCEPTED(a[index]); | |
307 } | |
308 | |
309 void indexSet$slow(var a, var index, var value) { | |
310 if (isJsArray(a)) { | |
311 if (index is !int) { | |
312 throw new ArgumentError(index); | |
313 } | |
314 if (index < 0 || index >= a.length) { | |
315 throw new RangeError.value(index); | |
316 } | |
317 checkMutable(a, 'indexed set'); | |
318 JS('void', r'#[#] = #', a, index, value); | |
319 return; | |
320 } | |
321 UNINTERCEPTED(a[index] = value); | |
322 } | |
323 | |
324 checkMutable(list, reason) { | 250 checkMutable(list, reason) { |
325 if (JS('bool', r'!!(#.immutable$list)', list)) { | 251 if (JS('bool', r'!!(#.immutable$list)', list)) { |
326 throw new UnsupportedError(reason); | 252 throw new UnsupportedError(reason); |
327 } | 253 } |
328 } | 254 } |
329 | 255 |
330 checkGrowable(list, reason) { | 256 checkGrowable(list, reason) { |
331 if (JS('bool', r'!!(#.fixed$length)', list)) { | 257 if (JS('bool', r'!!(#.fixed$length)', list)) { |
332 throw new UnsupportedError(reason); | 258 throw new UnsupportedError(reason); |
333 } | 259 } |
(...skipping 1407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1741 if (len != t.length) return false; | 1667 if (len != t.length) return false; |
1742 for (int i = 1; i < len; i++) { | 1668 for (int i = 1; i < len; i++) { |
1743 if (!isSubtype(s[i], t[i])) { | 1669 if (!isSubtype(s[i], t[i])) { |
1744 return false; | 1670 return false; |
1745 } | 1671 } |
1746 } | 1672 } |
1747 return true; | 1673 return true; |
1748 } | 1674 } |
1749 | 1675 |
1750 createRuntimeType(String name) => new TypeImpl(name); | 1676 createRuntimeType(String name) => new TypeImpl(name); |
OLD | NEW |