| 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 // TODO(srdjan): fix limitations. | 5 // TODO(srdjan): fix limitations. |
| 6 // - shift amount must be a Smi. | 6 // - shift amount must be a Smi. |
| 7 class IntegerImplementation { | 7 class _IntegerImplementation { |
| 8 factory IntegerImplementation._uninstantiable() { | 8 factory _IntegerImplementation._uninstantiable() { |
| 9 throw const UnsupportedOperationException( | 9 throw const UnsupportedOperationException( |
| 10 "IntegerImplementation can only be allocated by the VM"); | 10 "_IntegerImplementation can only be allocated by the VM"); |
| 11 } | 11 } |
| 12 num operator +(num other) { | 12 num operator +(num other) { |
| 13 return other.addFromInteger(this); | 13 return other.addFromInteger(this); |
| 14 } | 14 } |
| 15 num operator -(num other) { | 15 num operator -(num other) { |
| 16 return other.subFromInteger(this); | 16 return other.subFromInteger(this); |
| 17 } | 17 } |
| 18 num operator *(num other) { | 18 num operator *(num other) { |
| 19 return other.mulFromInteger(this); | 19 return other.mulFromInteger(this); |
| 20 } | 20 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 return EQUAL; | 125 return EQUAL; |
| 126 } | 126 } |
| 127 } | 127 } |
| 128 | 128 |
| 129 int round() { return this; } | 129 int round() { return this; } |
| 130 int floor() { return this; } | 130 int floor() { return this; } |
| 131 int ceil() { return this; } | 131 int ceil() { return this; } |
| 132 int truncate() { return this; } | 132 int truncate() { return this; } |
| 133 | 133 |
| 134 int toInt() { return this; } | 134 int toInt() { return this; } |
| 135 double toDouble() { return new Double.fromInteger(this); } | 135 double toDouble() { return new _Double.fromInteger(this); } |
| 136 | 136 |
| 137 int pow(int exponent) { | 137 int pow(int exponent) { |
| 138 double res = this.toDouble().pow(exponent); | 138 double res = this.toDouble().pow(exponent); |
| 139 if (res.isInfinite()) { | 139 if (res.isInfinite()) { |
| 140 // Use Bigint instead. | 140 // Use Bigint instead. |
| 141 throw "IntegerImplementation.pow not implemented for large integers."; | 141 throw "_IntegerImplementation.pow not implemented for large integers."; |
| 142 } | 142 } |
| 143 return res.toInt(); | 143 return res.toInt(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 String toStringAsFixed(int fractionDigits) { | 146 String toStringAsFixed(int fractionDigits) { |
| 147 return this.toDouble().toStringAsFixed(fractionDigits); | 147 return this.toDouble().toStringAsFixed(fractionDigits); |
| 148 } | 148 } |
| 149 String toStringAsExponential(int fractionDigits) { | 149 String toStringAsExponential(int fractionDigits) { |
| 150 return this.toDouble().toStringAsExponential(fractionDigits); | 150 return this.toDouble().toStringAsExponential(fractionDigits); |
| 151 } | 151 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 171 } | 171 } |
| 172 StringBuffer buffer = new StringBuffer(); | 172 StringBuffer buffer = new StringBuffer(); |
| 173 if (isNegative) buffer.add("-"); | 173 if (isNegative) buffer.add("-"); |
| 174 for (int i = temp.length - 1; i >= 0; i--) { | 174 for (int i = temp.length - 1; i >= 0; i--) { |
| 175 buffer.add(table[temp[i]]); | 175 buffer.add(table[temp[i]]); |
| 176 } | 176 } |
| 177 return buffer.toString(); | 177 return buffer.toString(); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 class Smi extends IntegerImplementation implements int { | 181 class _Smi extends _IntegerImplementation implements int { |
| 182 factory Smi._uninstantiable() { | 182 factory _Smi._uninstantiable() { |
| 183 throw const UnsupportedOperationException( | 183 throw const UnsupportedOperationException( |
| 184 "Smi can only be allocated by the VM"); | 184 "_Smi can only be allocated by the VM"); |
| 185 } | 185 } |
| 186 int hashCode() { | 186 int hashCode() { |
| 187 return this; | 187 return this; |
| 188 } | 188 } |
| 189 int operator ~() native "Smi_bitNegate"; | 189 int operator ~() native "Smi_bitNegate"; |
| 190 int shrFromInt(int other) native "Smi_shrFromInt"; | 190 int shrFromInt(int other) native "Smi_shrFromInt"; |
| 191 int shlFromInt(int other) native "Smi_shlFromInt"; | 191 int shlFromInt(int other) native "Smi_shlFromInt"; |
| 192 } | 192 } |
| 193 | 193 |
| 194 // Represents integers that cannot be represented by Smi but fit into 64bits. | 194 // Represents integers that cannot be represented by Smi but fit into 64bits. |
| 195 class Mint extends IntegerImplementation implements int { | 195 class _Mint extends _IntegerImplementation implements int { |
| 196 factory Mint._uninstantiable() { | 196 factory _Mint._uninstantiable() { |
| 197 throw const UnsupportedOperationException( | 197 throw const UnsupportedOperationException( |
| 198 "Mint can only be allocated by the VM"); | 198 "_Mint can only be allocated by the VM"); |
| 199 } | 199 } |
| 200 int hashCode() { | 200 int hashCode() { |
| 201 return this; | 201 return this; |
| 202 } | 202 } |
| 203 int operator ~() native "Mint_bitNegate"; | 203 int operator ~() native "Mint_bitNegate"; |
| 204 | 204 |
| 205 // Shift by mint exceeds range that can be handled by the VM. | 205 // Shift by mint exceeds range that can be handled by the VM. |
| 206 int shrFromInt(int other) { | 206 int shrFromInt(int other) { |
| 207 if (other < 0) { | 207 if (other < 0) { |
| 208 return -1; | 208 return -1; |
| 209 } else { | 209 } else { |
| 210 return 0; | 210 return 0; |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 int shlFromInt(int other) { | 213 int shlFromInt(int other) { |
| 214 throw const OutOfMemoryException(); | 214 throw const OutOfMemoryException(); |
| 215 } | 215 } |
| 216 } | 216 } |
| 217 | 217 |
| 218 // A number that can be represented as Smi or Mint will never be represented as | 218 // A number that can be represented as Smi or Mint will never be represented as |
| 219 // Bigint. | 219 // Bigint. |
| 220 class Bigint extends IntegerImplementation implements int { | 220 class _Bigint extends _IntegerImplementation implements int { |
| 221 factory Bigint._uninstantiable() { | 221 factory _Bigint._uninstantiable() { |
| 222 throw const UnsupportedOperationException( | 222 throw const UnsupportedOperationException( |
| 223 "Bigint can only be allocated by the VM"); | 223 "_Bigint can only be allocated by the VM"); |
| 224 } | 224 } |
| 225 int hashCode() { | 225 int hashCode() { |
| 226 return this; | 226 return this; |
| 227 } | 227 } |
| 228 int operator ~() native "Bigint_bitNegate"; | 228 int operator ~() native "Bigint_bitNegate"; |
| 229 | 229 |
| 230 // Shift by bigint exceeds range that can be handled by the VM. | 230 // Shift by bigint exceeds range that can be handled by the VM. |
| 231 int shrFromInt(int other) { | 231 int shrFromInt(int other) { |
| 232 if (other < 0) { | 232 if (other < 0) { |
| 233 return -1; | 233 return -1; |
| 234 } else { | 234 } else { |
| 235 return 0; | 235 return 0; |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 int shlFromInt(int other) { | 238 int shlFromInt(int other) { |
| 239 throw const OutOfMemoryException(); | 239 throw const OutOfMemoryException(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 int pow(int exponent) { | 242 int pow(int exponent) { |
| 243 throw "Bigint.pow not implemented"; | 243 throw "Bigint.pow not implemented"; |
| 244 } | 244 } |
| 245 } | 245 } |
| OLD | NEW |