Chromium Code Reviews| 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 part of _interceptors; | 5 part of _interceptors; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The super interceptor class for [JSInt] and [JSDouble]. The compiler | 8 * The super interceptor class for [JSInt] and [JSDouble]. The compiler |
| 9 * recognizes this class as an interceptor, and changes references to | 9 * recognizes this class as an interceptor, and changes references to |
| 10 * [:this:] to actually use the receiver of the method, which is | 10 * [:this:] to actually use the receiver of the method, which is |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 return this; | 125 return this; |
| 126 } | 126 } |
| 127 | 127 |
| 128 // The return type is intentionally omitted to avoid type checker warnings | 128 // The return type is intentionally omitted to avoid type checker warnings |
| 129 // from assigning JSNumber to double. | 129 // from assigning JSNumber to double. |
| 130 toDouble() => this; | 130 toDouble() => this; |
| 131 | 131 |
| 132 String toStringAsFixed(int fractionDigits) { | 132 String toStringAsFixed(int fractionDigits) { |
| 133 checkInt(fractionDigits); | 133 checkInt(fractionDigits); |
| 134 if (fractionDigits < 0 || fractionDigits > 20) { | 134 if (fractionDigits < 0 || fractionDigits > 20) { |
| 135 throw new RangeError(fractionDigits); | 135 throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits"); |
| 136 } | 136 } |
| 137 String result = JS('String', r'#.toFixed(#)', this, fractionDigits); | 137 String result = JS('String', r'#.toFixed(#)', this, fractionDigits); |
| 138 if (this == 0 && isNegative) return "-$result"; | 138 if (this == 0 && isNegative) return "-$result"; |
| 139 return result; | 139 return result; |
| 140 } | 140 } |
| 141 | 141 |
| 142 String toStringAsExponential([int fractionDigits]) { | 142 String toStringAsExponential([int fractionDigits]) { |
| 143 String result; | 143 String result; |
| 144 if (fractionDigits != null) { | 144 if (fractionDigits != null) { |
| 145 checkInt(fractionDigits); | 145 checkInt(fractionDigits); |
| 146 if (fractionDigits < 0 || fractionDigits > 20) { | 146 if (fractionDigits < 0 || fractionDigits > 20) { |
| 147 throw new RangeError(fractionDigits); | 147 throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits"); |
| 148 } | 148 } |
| 149 result = JS('String', r'#.toExponential(#)', this, fractionDigits); | 149 result = JS('String', r'#.toExponential(#)', this, fractionDigits); |
| 150 } else { | 150 } else { |
| 151 result = JS('String', r'#.toExponential()', this); | 151 result = JS('String', r'#.toExponential()', this); |
| 152 } | 152 } |
| 153 if (this == 0 && isNegative) return "-$result"; | 153 if (this == 0 && isNegative) return "-$result"; |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 String toStringAsPrecision(int precision) { | 157 String toStringAsPrecision(int precision) { |
| 158 checkInt(precision); | 158 checkInt(precision); |
| 159 if (precision < 1 || precision > 21) { | 159 if (precision < 1 || precision > 21) { |
| 160 throw new RangeError(precision); | 160 throw new RangeError.range(precision, 1, 21, "precision"); |
| 161 } | 161 } |
| 162 String result = JS('String', r'#.toPrecision(#)', | 162 String result = JS('String', r'#.toPrecision(#)', |
| 163 this, precision); | 163 this, precision); |
| 164 if (this == 0 && isNegative) return "-$result"; | 164 if (this == 0 && isNegative) return "-$result"; |
| 165 return result; | 165 return result; |
| 166 } | 166 } |
| 167 | 167 |
| 168 String toRadixString(int radix) { | 168 String toRadixString(int radix) { |
| 169 checkInt(radix); | 169 checkInt(radix); |
| 170 if (radix < 2 || radix > 36) { | 170 if (radix < 2 || radix > 36) { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 int nonneg = this < 0 ? -this - 1 : this; | 383 int nonneg = this < 0 ? -this - 1 : this; |
| 384 if (nonneg >= 0x100000000) { | 384 if (nonneg >= 0x100000000) { |
| 385 nonneg = nonneg ~/ 0x100000000; | 385 nonneg = nonneg ~/ 0x100000000; |
| 386 return _bitCount(_spread(nonneg)) + 32; | 386 return _bitCount(_spread(nonneg)) + 32; |
| 387 } | 387 } |
| 388 return _bitCount(_spread(nonneg)); | 388 return _bitCount(_spread(nonneg)); |
| 389 } | 389 } |
| 390 | 390 |
| 391 // Returns pow(this, e) % m. | 391 // Returns pow(this, e) % m. |
| 392 int modPow(int e, int m) { | 392 int modPow(int e, int m) { |
| 393 if (e is! int) throw argumentErrorValue(e); | 393 if (e is! int) throw argumentErrorValue(e); |
|
regis
2015/06/24 15:40:43
Why not ArgumentError.value(e, "exponent", "not an
Lasse Reichstein Nielsen
2015/06/25 07:22:21
I think the argumentErrorValue function might be t
sra1
2015/06/25 21:28:48
If you want to pass more arguments to ArgumentErro
Lasse Reichstein Nielsen
2015/06/30 05:50:47
Done.
| |
| 394 if (m is! int) throw argumentErrorValue(m); | 394 if (m is! int) throw argumentErrorValue(m); |
|
regis
2015/06/24 15:40:43
ditto
Lasse Reichstein Nielsen
2015/06/30 05:50:47
Done.
| |
| 395 if (e < 0) throw new RangeError(e); | 395 if (e < 0) throw new RangeError.range(e, 0, null, "exponent"); |
| 396 if (m <= 0) throw new RangeError(m); | 396 if (m <= 0) throw new RangeError.range(m, 1, null, "modulus"); |
| 397 if (e == 0) return 1; | 397 if (e == 0) return 1; |
| 398 int b = this; | 398 int b = this; |
| 399 if (b < 0 || b > m) { | 399 if (b < 0 || b > m) { |
| 400 b %= m; | 400 b %= m; |
| 401 } | 401 } |
| 402 int r = 1; | 402 int r = 1; |
| 403 while (e > 0) { | 403 while (e > 0) { |
| 404 if (e.isOdd) { | 404 if (e.isOdd) { |
| 405 r = (r * b) % m; | 405 r = (r * b) % m; |
| 406 } | 406 } |
| 407 e ~/= 2; | 407 e ~/= 2; |
| 408 b = (b * b) % m; | 408 b = (b * b) % m; |
| 409 } | 409 } |
| 410 return r; | 410 return r; |
| 411 } | 411 } |
| 412 | 412 |
| 413 // If inv is false, returns gcd(x, y). | 413 // If inv is false, returns gcd(x, y). |
| 414 // If inv is true and gcd(x, y) = 1, returns d, so that c*x + d*y = 1. | 414 // If inv is true and gcd(x, y) = 1, returns d, so that c*x + d*y = 1. |
| 415 // If inv is true and gcd(x, y) != 1, throws RangeError("Not coprime"). | 415 // If inv is true and gcd(x, y) != 1, throws UnsupportedError("Not coprime"). |
| 416 static int _binaryGcd(int x, int y, bool inv) { | 416 static int _binaryGcd(int x, int y, bool inv) { |
| 417 int s = 1; | 417 int s = 1; |
| 418 if (!inv) { | 418 if (!inv) { |
| 419 while (x.isEven && y.isEven) { | 419 while (x.isEven && y.isEven) { |
| 420 x ~/= 2; | 420 x ~/= 2; |
| 421 y ~/= 2; | 421 y ~/= 2; |
| 422 s *= 2; | 422 s *= 2; |
| 423 } | 423 } |
| 424 if (y.isOdd) { | 424 if (y.isOdd) { |
| 425 var t = x; | 425 var t = x; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 u -= v; | 465 u -= v; |
| 466 if (ac) a -= c; | 466 if (ac) a -= c; |
| 467 b -= d; | 467 b -= d; |
| 468 } else { | 468 } else { |
| 469 v -= u; | 469 v -= u; |
| 470 if (ac) c -= a; | 470 if (ac) c -= a; |
| 471 d -= b; | 471 d -= b; |
| 472 } | 472 } |
| 473 } while (u != 0); | 473 } while (u != 0); |
| 474 if (!inv) return s*v; | 474 if (!inv) return s*v; |
| 475 if (v != 1) throw new RangeError("Not coprime"); | 475 if (v != 1) throw new UnsupportedError("Not coprime"); |
| 476 if (d < 0) { | 476 if (d < 0) { |
| 477 d += x; | 477 d += x; |
| 478 if (d < 0) d += x; | 478 if (d < 0) d += x; |
| 479 } else if (d > x) { | 479 } else if (d > x) { |
| 480 d -= x; | 480 d -= x; |
| 481 if (d > x) d -= x; | 481 if (d > x) d -= x; |
| 482 } | 482 } |
| 483 return d; | 483 return d; |
| 484 } | 484 } |
| 485 | 485 |
| 486 // Returns 1/this % m, with m > 0. | 486 // Returns 1/this % m, with m > 0. |
| 487 int modInverse(int m) { | 487 int modInverse(int m) { |
| 488 if (m is! int) throw new ArgumentError(m); | 488 if (m is! int) { |
| 489 if (m <= 0) throw new RangeError(m); | 489 throw new ArgumentError.value(m, "modulus", "not an integer"); |
| 490 } | |
| 491 if (m <= 0) throw new RangeError.range(m, 1, null, "modulus"); | |
| 490 if (m == 1) return 0; | 492 if (m == 1) return 0; |
| 491 int t = this; | 493 int t = this; |
| 492 if ((t < 0) || (t >= m)) t %= m; | 494 if ((t < 0) || (t >= m)) t %= m; |
| 493 if (t == 1) return 1; | 495 if (t == 1) return 1; |
| 494 if ((t == 0) || (t.isEven && m.isEven)) throw new RangeError("Not coprime"); | 496 if ((t == 0) || (t.isEven && m.isEven)) { |
| 497 throw new UnsupportedError("Not coprime"); | |
| 498 } | |
| 495 return _binaryGcd(m, t, true); | 499 return _binaryGcd(m, t, true); |
| 496 } | 500 } |
| 497 | 501 |
| 498 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0. | 502 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0. |
| 499 int gcd(int other) { | 503 int gcd(int other) { |
| 500 if (other is! int) throw new ArgumentError(other); | 504 if (m is! int) { |
| 501 if ((this == 0) || (other == 0)) throw new RangeError(0); | 505 throw new ArgumentError.value(m, "other", "not an integer"); |
| 506 } | |
| 507 if (this == 0) { | |
| 508 throw new ArgumentError.value(this, "first operand", "must not be zero"); | |
| 509 } | |
| 510 if (other == 0) { | |
| 511 throw new ArgumentError.value(this, "second operand", "must not be zero"); | |
| 512 } | |
| 502 int x = this.abs(); | 513 int x = this.abs(); |
| 503 int y = other.abs(); | 514 int y = other.abs(); |
| 504 if ((x == 1) || (y == 1)) return 1; | 515 if ((x == 1) || (y == 1)) return 1; |
| 505 return _binaryGcd(x, y, false); | 516 return _binaryGcd(x, y, false); |
| 506 } | 517 } |
| 507 | 518 |
| 508 // Assumes i is <= 32-bit and unsigned. | 519 // Assumes i is <= 32-bit and unsigned. |
| 509 static int _bitCount(int i) { | 520 static int _bitCount(int i) { |
| 510 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". | 521 // See "Hacker's Delight", section 5-1, "Counting 1-Bits". |
| 511 | 522 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 } | 563 } |
| 553 | 564 |
| 554 class JSDouble extends JSNumber implements double { | 565 class JSDouble extends JSNumber implements double { |
| 555 const JSDouble(); | 566 const JSDouble(); |
| 556 Type get runtimeType => double; | 567 Type get runtimeType => double; |
| 557 } | 568 } |
| 558 | 569 |
| 559 class JSPositiveInt extends JSInt {} | 570 class JSPositiveInt extends JSInt {} |
| 560 class JSUInt32 extends JSPositiveInt {} | 571 class JSUInt32 extends JSPositiveInt {} |
| 561 class JSUInt31 extends JSUInt32 {} | 572 class JSUInt31 extends JSUInt32 {} |
| OLD | NEW |