| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 if (this.compareTo(lowerLimit) < 0) return lowerLimit; | 103 if (this.compareTo(lowerLimit) < 0) return lowerLimit; |
| 104 if (this.compareTo(upperLimit) > 0) return upperLimit; | 104 if (this.compareTo(upperLimit) > 0) return upperLimit; |
| 105 return this; | 105 return this; |
| 106 } | 106 } |
| 107 | 107 |
| 108 // The return type is intentionally omitted to avoid type checker warnings | 108 // The return type is intentionally omitted to avoid type checker warnings |
| 109 // from assigning JSNumber to double. | 109 // from assigning JSNumber to double. |
| 110 toDouble() => this; | 110 toDouble() => this; |
| 111 | 111 |
| 112 String toStringAsFixed(int fractionDigits) { | 112 String toStringAsFixed(int fractionDigits) { |
| 113 checkNum(fractionDigits); | 113 checkInt(fractionDigits); |
| 114 // TODO(floitsch): fractionDigits must be an integer. | |
| 115 if (fractionDigits < 0 || fractionDigits > 20) { | 114 if (fractionDigits < 0 || fractionDigits > 20) { |
| 116 throw new RangeError(fractionDigits); | 115 throw new RangeError(fractionDigits); |
| 117 } | 116 } |
| 118 String result = JS('String', r'#.toFixed(#)', this, fractionDigits); | 117 String result = JS('String', r'#.toFixed(#)', this, fractionDigits); |
| 119 if (this == 0 && isNegative) return "-$result"; | 118 if (this == 0 && isNegative) return "-$result"; |
| 120 return result; | 119 return result; |
| 121 } | 120 } |
| 122 | 121 |
| 123 String toStringAsExponential([int fractionDigits]) { | 122 String toStringAsExponential([int fractionDigits]) { |
| 124 String result; | 123 String result; |
| 125 if (fractionDigits != null) { | 124 if (fractionDigits != null) { |
| 126 // TODO(floitsch): fractionDigits must be an integer. | 125 checkInt(fractionDigits); |
| 127 checkNum(fractionDigits); | |
| 128 if (fractionDigits < 0 || fractionDigits > 20) { | 126 if (fractionDigits < 0 || fractionDigits > 20) { |
| 129 throw new RangeError(fractionDigits); | 127 throw new RangeError(fractionDigits); |
| 130 } | 128 } |
| 131 result = JS('String', r'#.toExponential(#)', this, fractionDigits); | 129 result = JS('String', r'#.toExponential(#)', this, fractionDigits); |
| 132 } else { | 130 } else { |
| 133 result = JS('String', r'#.toExponential()', this); | 131 result = JS('String', r'#.toExponential()', this); |
| 134 } | 132 } |
| 135 if (this == 0 && isNegative) return "-$result"; | 133 if (this == 0 && isNegative) return "-$result"; |
| 136 return result; | 134 return result; |
| 137 } | 135 } |
| 138 | 136 |
| 139 String toStringAsPrecision(int precision) { | 137 String toStringAsPrecision(int precision) { |
| 140 // TODO(floitsch): precision must be an integer. | 138 checkInt(precision); |
| 141 checkNum(precision); | |
| 142 if (precision < 1 || precision > 21) { | 139 if (precision < 1 || precision > 21) { |
| 143 throw new RangeError(precision); | 140 throw new RangeError(precision); |
| 144 } | 141 } |
| 145 String result = JS('String', r'#.toPrecision(#)', | 142 String result = JS('String', r'#.toPrecision(#)', |
| 146 this, precision); | 143 this, precision); |
| 147 if (this == 0 && isNegative) return "-$result"; | 144 if (this == 0 && isNegative) return "-$result"; |
| 148 return result; | 145 return result; |
| 149 } | 146 } |
| 150 | 147 |
| 151 String toRadixString(int radix) { | 148 String toRadixString(int radix) { |
| 152 checkNum(radix); | 149 checkInt(radix); |
| 153 if (radix < 2 || radix > 36) throw new RangeError(radix); | 150 if (radix < 2 || radix > 36) throw new RangeError(radix); |
| 154 return JS('String', r'#.toString(#)', this, radix); | 151 String result = JS('String', r'#.toString(#)', this, radix); |
| 152 const int rightParenCode = 0x29; |
| 153 if (result.codeUnitAt(result.length - 1) != rightParenCode) { |
| 154 return result; |
| 155 } |
| 156 return _handleIEtoString(result); |
| 157 } |
| 158 |
| 159 static String _handleIEtoString(String result) { |
| 160 // Result is probably IE's untraditional format for large numbers, |
| 161 // e.g., "8.0000000000008(e+15)" for 0x8000000000000800.toString(16). |
| 162 var match = JS('List|Null', |
| 163 r'/^([\da-z]+)(?:\.([\da-z]+))?\(e\+(\d+)\)$/.exec(#)', |
| 164 result); |
| 165 if (match == null) { |
| 166 // Then we don't know how to handle it at all. |
| 167 throw new UnsupportedError("Unexpected toString result: $result"); |
| 168 } |
| 169 String result = JS('String', '#', match[1]); |
| 170 int exponent = JS("int", "+#", match[3]); |
| 171 if (match[2] != null) { |
| 172 result = JS('String', '# + #', result, match[2]); |
| 173 exponent -= JS('int', '#.length', match[2]); |
| 174 } |
| 175 return result + "0" * exponent; |
| 155 } | 176 } |
| 156 | 177 |
| 157 // Note: if you change this, also change the function [S]. | 178 // Note: if you change this, also change the function [S]. |
| 158 String toString() { | 179 String toString() { |
| 159 if (this == 0 && JS('bool', '(1 / #) < 0', this)) { | 180 if (this == 0 && JS('bool', '(1 / #) < 0', this)) { |
| 160 return '-0.0'; | 181 return '-0.0'; |
| 161 } else { | 182 } else { |
| 162 return JS('String', r'"" + (#)', this); | 183 return JS('String', r'"" + (#)', this); |
| 163 } | 184 } |
| 164 } | 185 } |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 } | 413 } |
| 393 | 414 |
| 394 class JSDouble extends JSNumber implements double { | 415 class JSDouble extends JSNumber implements double { |
| 395 const JSDouble(); | 416 const JSDouble(); |
| 396 Type get runtimeType => double; | 417 Type get runtimeType => double; |
| 397 } | 418 } |
| 398 | 419 |
| 399 class JSPositiveInt extends JSInt {} | 420 class JSPositiveInt extends JSInt {} |
| 400 class JSUInt32 extends JSPositiveInt {} | 421 class JSUInt32 extends JSPositiveInt {} |
| 401 class JSUInt31 extends JSUInt32 {} | 422 class JSUInt31 extends JSUInt32 {} |
| OLD | NEW |