| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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() { |
| 9 throw const UnsupportedOperationException( |
| 10 "IntegerImplementation can only be allocated by the VM"); |
| 11 } |
| 8 num operator +(num other) { | 12 num operator +(num other) { |
| 9 return other.addFromInteger(this); | 13 return other.addFromInteger(this); |
| 10 } | 14 } |
| 11 num operator -(num other) { | 15 num operator -(num other) { |
| 12 return other.subFromInteger(this); | 16 return other.subFromInteger(this); |
| 13 } | 17 } |
| 14 num operator *(num other) { | 18 num operator *(num other) { |
| 15 return other.mulFromInteger(this); | 19 return other.mulFromInteger(this); |
| 16 } | 20 } |
| 17 num operator ~/(num other) { | 21 num operator ~/(num other) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 StringBuffer buffer = new StringBuffer(); | 153 StringBuffer buffer = new StringBuffer(); |
| 150 if (isNegative) buffer.add("-"); | 154 if (isNegative) buffer.add("-"); |
| 151 for (int i = temp.length - 1; i >= 0; i--) { | 155 for (int i = temp.length - 1; i >= 0; i--) { |
| 152 buffer.add(table[temp[i]]); | 156 buffer.add(table[temp[i]]); |
| 153 } | 157 } |
| 154 return buffer.toString(); | 158 return buffer.toString(); |
| 155 } | 159 } |
| 156 } | 160 } |
| 157 | 161 |
| 158 class Smi extends IntegerImplementation implements int { | 162 class Smi extends IntegerImplementation implements int { |
| 163 factory Smi._uninstantiable() { |
| 164 throw const UnsupportedOperationException( |
| 165 "Smi can only be allocated by the VM"); |
| 166 } |
| 159 int hashCode() { | 167 int hashCode() { |
| 160 return this; | 168 return this; |
| 161 } | 169 } |
| 162 int operator ~() native "Smi_bitNegate"; | 170 int operator ~() native "Smi_bitNegate"; |
| 163 int sarFromInt(int other) native "Smi_sarFromInt"; | 171 int sarFromInt(int other) native "Smi_sarFromInt"; |
| 164 int shlFromInt(int other) native "Smi_shlFromInt"; | 172 int shlFromInt(int other) native "Smi_shlFromInt"; |
| 165 } | 173 } |
| 166 | 174 |
| 167 // Represents integers that cannot be represented by Smi but fit into 64bits. | 175 // Represents integers that cannot be represented by Smi but fit into 64bits. |
| 168 class Mint extends IntegerImplementation implements int { | 176 class Mint extends IntegerImplementation implements int { |
| 177 factory Mint._uninstantiable() { |
| 178 throw const UnsupportedOperationException( |
| 179 "Mint can only be allocated by the VM"); |
| 180 } |
| 169 int hashCode() { | 181 int hashCode() { |
| 170 return this; | 182 return this; |
| 171 } | 183 } |
| 172 int operator ~() native "Mint_bitNegate"; | 184 int operator ~() native "Mint_bitNegate"; |
| 173 } | 185 } |
| 174 | 186 |
| 175 // A number that can be represented as Smi or Mint will never be represented as | 187 // A number that can be represented as Smi or Mint will never be represented as |
| 176 // Bigint. | 188 // Bigint. |
| 177 class Bigint extends IntegerImplementation implements int { | 189 class Bigint extends IntegerImplementation implements int { |
| 190 factory Bigint._uninstantiable() { |
| 191 throw const UnsupportedOperationException( |
| 192 "Bigint can only be allocated by the VM"); |
| 193 } |
| 178 int hashCode() { | 194 int hashCode() { |
| 179 return this; | 195 return this; |
| 180 } | 196 } |
| 181 int operator ~() native "Bigint_bitNegate"; | 197 int operator ~() native "Bigint_bitNegate"; |
| 182 | 198 |
| 183 // Shift by bigint exceeds range that can be handled by the VM. | 199 // Shift by bigint exceeds range that can be handled by the VM. |
| 184 int sarFromInt(int other) { | 200 int sarFromInt(int other) { |
| 185 if (other < 0) { | 201 if (other < 0) { |
| 186 return -1; | 202 return -1; |
| 187 } else { | 203 } else { |
| 188 return 0; | 204 return 0; |
| 189 } | 205 } |
| 190 } | 206 } |
| 191 int shlFromInt(int other) { | 207 int shlFromInt(int other) { |
| 192 throw const OutOfMemoryException(); | 208 throw const OutOfMemoryException(); |
| 193 } | 209 } |
| 194 } | 210 } |
| OLD | NEW |