| 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 } |
| 21 num operator ~/(num other) { | 21 num operator ~/(num other) { |
| 22 if ((other is int) && (other == 0)) { | 22 if ((other is int) && (other == 0)) { |
| 23 throw const IntegerDivisionByZeroException(); | 23 throw const IntegerDivisionByZeroException(); |
| 24 } | 24 } |
| 25 return other.truncDivFromInteger(this); | 25 return other._truncDivFromInteger(this); |
| 26 } | 26 } |
| 27 num operator /(num other) { | 27 num operator /(num other) { |
| 28 return this.toDouble() / other.toDouble(); | 28 return this.toDouble() / other.toDouble(); |
| 29 } | 29 } |
| 30 num operator %(num other) { | 30 num operator %(num other) { |
| 31 if ((other is int) && (other == 0)) { | 31 if ((other is int) && (other == 0)) { |
| 32 throw const IntegerDivisionByZeroException(); | 32 throw const IntegerDivisionByZeroException(); |
| 33 } | 33 } |
| 34 return other.moduloFromInteger(this); | 34 return other._moduloFromInteger(this); |
| 35 } | 35 } |
| 36 int operator -() { | 36 int operator -() { |
| 37 return 0 - this; | 37 return 0 - this; |
| 38 } | 38 } |
| 39 int operator &(int other) { | 39 int operator &(int other) { |
| 40 return other.bitAndFromInteger(this); | 40 return other._bitAndFromInteger(this); |
| 41 } | 41 } |
| 42 int operator |(int other) { | 42 int operator |(int other) { |
| 43 return other.bitOrFromInteger(this); | 43 return other._bitOrFromInteger(this); |
| 44 } | 44 } |
| 45 int operator ^(int other) { | 45 int operator ^(int other) { |
| 46 return other.bitXorFromInteger(this); | 46 return other._bitXorFromInteger(this); |
| 47 } | 47 } |
| 48 num remainder(num other) { | 48 num remainder(num other) { |
| 49 return other.remainderFromInteger(this); | 49 return other._remainderFromInteger(this); |
| 50 } | 50 } |
| 51 int bitAndFromInteger(int other) native "Integer_bitAndFromInteger"; | 51 int _bitAndFromInteger(int other) native "Integer_bitAndFromInteger"; |
| 52 int bitOrFromInteger(int other) native "Integer_bitOrFromInteger"; | 52 int _bitOrFromInteger(int other) native "Integer_bitOrFromInteger"; |
| 53 int bitXorFromInteger(int other) native "Integer_bitXorFromInteger"; | 53 int _bitXorFromInteger(int other) native "Integer_bitXorFromInteger"; |
| 54 int addFromInteger(int other) native "Integer_addFromInteger"; | 54 int _addFromInteger(int other) native "Integer_addFromInteger"; |
| 55 int subFromInteger(int other) native "Integer_subFromInteger"; | 55 int _subFromInteger(int other) native "Integer_subFromInteger"; |
| 56 int mulFromInteger(int other) native "Integer_mulFromInteger"; | 56 int _mulFromInteger(int other) native "Integer_mulFromInteger"; |
| 57 int truncDivFromInteger(int other) native "Integer_truncDivFromInteger"; | 57 int _truncDivFromInteger(int other) native "Integer_truncDivFromInteger"; |
| 58 int moduloFromInteger(int other) native "Integer_moduloFromInteger"; | 58 int _moduloFromInteger(int other) native "Integer_moduloFromInteger"; |
| 59 int remainderFromInteger(int other) { | 59 int _remainderFromInteger(int other) { |
| 60 return other - (other ~/ this) * this; | 60 return other - (other ~/ this) * this; |
| 61 } | 61 } |
| 62 int operator >>(int other) { | 62 int operator >>(int other) { |
| 63 return other.shrFromInt(this); | 63 return other._shrFromInt(this); |
| 64 } | 64 } |
| 65 int operator <<(int other) { | 65 int operator <<(int other) { |
| 66 return other.shlFromInt(this); | 66 return other._shlFromInt(this); |
| 67 } | 67 } |
| 68 bool operator <(num other) { | 68 bool operator <(num other) { |
| 69 return other > this; | 69 return other > this; |
| 70 } | 70 } |
| 71 bool operator >(num other) { | 71 bool operator >(num other) { |
| 72 return other.greaterThanFromInteger(this); | 72 return other._greaterThanFromInteger(this); |
| 73 } | 73 } |
| 74 bool operator >=(num other) { | 74 bool operator >=(num other) { |
| 75 return (this == other) || (this > other); | 75 return (this == other) || (this > other); |
| 76 } | 76 } |
| 77 bool operator <=(num other) { | 77 bool operator <=(num other) { |
| 78 return (this == other) || (this < other); | 78 return (this == other) || (this < other); |
| 79 } | 79 } |
| 80 bool greaterThanFromInteger(int other) | 80 bool _greaterThanFromInteger(int other) |
| 81 native "Integer_greaterThanFromInteger"; | 81 native "Integer_greaterThanFromInteger"; |
| 82 bool operator ==(other) { | 82 bool operator ==(other) { |
| 83 if (other is num) { | 83 if (other is num) { |
| 84 return other.equalToInteger(this); | 84 return other._equalToInteger(this); |
| 85 } | 85 } |
| 86 return false; | 86 return false; |
| 87 } | 87 } |
| 88 bool equalToInteger(int other) native "Integer_equalToInteger"; | 88 bool _equalToInteger(int other) native "Integer_equalToInteger"; |
| 89 int abs() { | 89 int abs() { |
| 90 return this < 0 ? -this : this; | 90 return this < 0 ? -this : this; |
| 91 } | 91 } |
| 92 bool isEven() { return ((this & 1) === 0); } | 92 bool isEven() { return ((this & 1) === 0); } |
| 93 bool isOdd() { return !isEven(); } | 93 bool isOdd() { return !isEven(); } |
| 94 bool isNaN() { return false; } | 94 bool isNaN() { return false; } |
| 95 bool isNegative() { return this < 0; } | 95 bool isNegative() { return this < 0; } |
| 96 bool isInfinite() { return false; } | 96 bool isInfinite() { return false; } |
| 97 | 97 |
| 98 int compareTo(num other) { | 98 int compareTo(num other) { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 182 |
| 183 class _Smi extends _IntegerImplementation implements int { | 183 class _Smi extends _IntegerImplementation implements int { |
| 184 factory _Smi._uninstantiable() { | 184 factory _Smi._uninstantiable() { |
| 185 throw const UnsupportedOperationException( | 185 throw const UnsupportedOperationException( |
| 186 "_Smi can only be allocated by the VM"); | 186 "_Smi can only be allocated by the VM"); |
| 187 } | 187 } |
| 188 int hashCode() { | 188 int hashCode() { |
| 189 return this; | 189 return this; |
| 190 } | 190 } |
| 191 int operator ~() native "Smi_bitNegate"; | 191 int operator ~() native "Smi_bitNegate"; |
| 192 int shrFromInt(int other) native "Smi_shrFromInt"; | 192 int _shrFromInt(int other) native "Smi_shrFromInt"; |
| 193 int shlFromInt(int other) native "Smi_shlFromInt"; | 193 int _shlFromInt(int other) native "Smi_shlFromInt"; |
| 194 } | 194 } |
| 195 | 195 |
| 196 // Represents integers that cannot be represented by Smi but fit into 64bits. | 196 // Represents integers that cannot be represented by Smi but fit into 64bits. |
| 197 class _Mint extends _IntegerImplementation implements int { | 197 class _Mint extends _IntegerImplementation implements int { |
| 198 factory _Mint._uninstantiable() { | 198 factory _Mint._uninstantiable() { |
| 199 throw const UnsupportedOperationException( | 199 throw const UnsupportedOperationException( |
| 200 "_Mint can only be allocated by the VM"); | 200 "_Mint can only be allocated by the VM"); |
| 201 } | 201 } |
| 202 int hashCode() { | 202 int hashCode() { |
| 203 return this; | 203 return this; |
| 204 } | 204 } |
| 205 int operator ~() native "Mint_bitNegate"; | 205 int operator ~() native "Mint_bitNegate"; |
| 206 | 206 |
| 207 // Shift by mint exceeds range that can be handled by the VM. | 207 // Shift by mint exceeds range that can be handled by the VM. |
| 208 int shrFromInt(int other) { | 208 int _shrFromInt(int other) { |
| 209 if (other < 0) { | 209 if (other < 0) { |
| 210 return -1; | 210 return -1; |
| 211 } else { | 211 } else { |
| 212 return 0; | 212 return 0; |
| 213 } | 213 } |
| 214 } | 214 } |
| 215 int shlFromInt(int other) { | 215 int _shlFromInt(int other) { |
| 216 throw const OutOfMemoryException(); | 216 throw const OutOfMemoryException(); |
| 217 } | 217 } |
| 218 } | 218 } |
| 219 | 219 |
| 220 // A number that can be represented as Smi or Mint will never be represented as | 220 // A number that can be represented as Smi or Mint will never be represented as |
| 221 // Bigint. | 221 // Bigint. |
| 222 class _Bigint extends _IntegerImplementation implements int { | 222 class _Bigint extends _IntegerImplementation implements int { |
| 223 factory _Bigint._uninstantiable() { | 223 factory _Bigint._uninstantiable() { |
| 224 throw const UnsupportedOperationException( | 224 throw const UnsupportedOperationException( |
| 225 "_Bigint can only be allocated by the VM"); | 225 "_Bigint can only be allocated by the VM"); |
| 226 } | 226 } |
| 227 int hashCode() { | 227 int hashCode() { |
| 228 return this; | 228 return this; |
| 229 } | 229 } |
| 230 int operator ~() native "Bigint_bitNegate"; | 230 int operator ~() native "Bigint_bitNegate"; |
| 231 | 231 |
| 232 // Shift by bigint exceeds range that can be handled by the VM. | 232 // Shift by bigint exceeds range that can be handled by the VM. |
| 233 int shrFromInt(int other) { | 233 int _shrFromInt(int other) { |
| 234 if (other < 0) { | 234 if (other < 0) { |
| 235 return -1; | 235 return -1; |
| 236 } else { | 236 } else { |
| 237 return 0; | 237 return 0; |
| 238 } | 238 } |
| 239 } | 239 } |
| 240 int shlFromInt(int other) { | 240 int _shlFromInt(int other) { |
| 241 throw const OutOfMemoryException(); | 241 throw const OutOfMemoryException(); |
| 242 } | 242 } |
| 243 | 243 |
| 244 int pow(int exponent) { | 244 int pow(int exponent) { |
| 245 throw "Bigint.pow not implemented"; | 245 throw "Bigint.pow not implemented"; |
| 246 } | 246 } |
| 247 } | 247 } |
| OLD | NEW |