| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_CONVERSIONS_H_ | 5 #ifndef V8_CONVERSIONS_H_ |
| 6 #define V8_CONVERSIONS_H_ | 6 #define V8_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
| 11 #include "src/handles.h" | 11 #include "src/handles.h" |
| 12 #include "src/objects.h" | |
| 13 #include "src/utils.h" | 12 #include "src/utils.h" |
| 14 | 13 |
| 15 namespace v8 { | 14 namespace v8 { |
| 16 namespace internal { | 15 namespace internal { |
| 17 | 16 |
| 18 class UnicodeCache; | 17 class UnicodeCache; |
| 19 | 18 |
| 20 // Maximum number of significant digits in decimal representation. | 19 // Maximum number of significant digits in decimal representation. |
| 21 // The longest possible double in decimal representation is | 20 // The longest possible double in decimal representation is |
| 22 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 | 21 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 char* DoubleToPrecisionCString(double value, int f); | 149 char* DoubleToPrecisionCString(double value, int f); |
| 151 char* DoubleToRadixCString(double value, int radix); | 150 char* DoubleToRadixCString(double value, int radix); |
| 152 | 151 |
| 153 | 152 |
| 154 static inline bool IsMinusZero(double value) { | 153 static inline bool IsMinusZero(double value) { |
| 155 static const DoubleRepresentation minus_zero(-0.0); | 154 static const DoubleRepresentation minus_zero(-0.0); |
| 156 return DoubleRepresentation(value) == minus_zero; | 155 return DoubleRepresentation(value) == minus_zero; |
| 157 } | 156 } |
| 158 | 157 |
| 159 | 158 |
| 160 static inline bool IsSmiDouble(double value) { | 159 static inline bool IsSmiDouble(double value); |
| 161 return !IsMinusZero(value) && value >= Smi::kMinValue && | |
| 162 value <= Smi::kMaxValue && value == FastI2D(FastD2I(value)); | |
| 163 } | |
| 164 | 160 |
| 165 | 161 |
| 166 // Integer32 is an integer that can be represented as a signed 32-bit | 162 // Integer32 is an integer that can be represented as a signed 32-bit |
| 167 // integer. It has to be in the range [-2^31, 2^31 - 1]. | 163 // integer. It has to be in the range [-2^31, 2^31 - 1]. |
| 168 // We also have to check for negative 0 as it is not an Integer32. | 164 // We also have to check for negative 0 as it is not an Integer32. |
| 169 static inline bool IsInt32Double(double value) { | 165 static inline bool IsInt32Double(double value); |
| 170 return !IsMinusZero(value) && | |
| 171 value >= kMinInt && | |
| 172 value <= kMaxInt && | |
| 173 value == FastI2D(FastD2I(value)); | |
| 174 } | |
| 175 | 166 |
| 176 | 167 |
| 177 // UInteger32 is an integer that can be represented as an unsigned 32-bit | 168 // UInteger32 is an integer that can be represented as an unsigned 32-bit |
| 178 // integer. It has to be in the range [0, 2^32 - 1]. | 169 // integer. It has to be in the range [0, 2^32 - 1]. |
| 179 // We also have to check for negative 0 as it is not a UInteger32. | 170 // We also have to check for negative 0 as it is not a UInteger32. |
| 180 static inline bool IsUint32Double(double value) { | 171 static inline bool IsUint32Double(double value); |
| 181 return !IsMinusZero(value) && | |
| 182 value >= 0 && | |
| 183 value <= kMaxUInt32 && | |
| 184 value == FastUI2D(FastD2UI(value)); | |
| 185 } | |
| 186 | 172 |
| 187 | 173 |
| 188 // Convert from Number object to C integer. | 174 // Convert from Number object to C integer. |
| 189 inline int32_t NumberToInt32(Object* number) { | 175 inline int32_t NumberToInt32(Object* number); |
| 190 if (number->IsSmi()) return Smi::cast(number)->value(); | 176 inline uint32_t NumberToUint32(Object* number); |
| 191 return DoubleToInt32(number->Number()); | |
| 192 } | |
| 193 | |
| 194 | |
| 195 inline uint32_t NumberToUint32(Object* number) { | |
| 196 if (number->IsSmi()) return Smi::cast(number)->value(); | |
| 197 return DoubleToUint32(number->Number()); | |
| 198 } | |
| 199 | 177 |
| 200 | 178 |
| 201 double StringToDouble(UnicodeCache* unicode_cache, Handle<String> string, | 179 double StringToDouble(UnicodeCache* unicode_cache, Handle<String> string, |
| 202 int flags, double empty_string_val = 0.0); | 180 int flags, double empty_string_val = 0.0); |
| 203 | 181 |
| 204 | 182 |
| 205 inline bool TryNumberToSize(Isolate* isolate, | 183 inline bool TryNumberToSize(Isolate* isolate, Object* number, size_t* result); |
| 206 Object* number, size_t* result) { | 184 |
| 207 SealHandleScope shs(isolate); | |
| 208 if (number->IsSmi()) { | |
| 209 int value = Smi::cast(number)->value(); | |
| 210 DCHECK(static_cast<unsigned>(Smi::kMaxValue) | |
| 211 <= std::numeric_limits<size_t>::max()); | |
| 212 if (value >= 0) { | |
| 213 *result = static_cast<size_t>(value); | |
| 214 return true; | |
| 215 } | |
| 216 return false; | |
| 217 } else { | |
| 218 DCHECK(number->IsHeapNumber()); | |
| 219 double value = HeapNumber::cast(number)->value(); | |
| 220 if (value >= 0 && | |
| 221 value <= std::numeric_limits<size_t>::max()) { | |
| 222 *result = static_cast<size_t>(value); | |
| 223 return true; | |
| 224 } else { | |
| 225 return false; | |
| 226 } | |
| 227 } | |
| 228 } | |
| 229 | 185 |
| 230 // Converts a number into size_t. | 186 // Converts a number into size_t. |
| 231 inline size_t NumberToSize(Isolate* isolate, | 187 inline size_t NumberToSize(Isolate* isolate, Object* number); |
| 232 Object* number) { | |
| 233 size_t result = 0; | |
| 234 bool is_valid = TryNumberToSize(isolate, number, &result); | |
| 235 CHECK(is_valid); | |
| 236 return result; | |
| 237 } | |
| 238 | 188 |
| 239 | 189 |
| 240 // returns DoubleToString(StringToDouble(string)) == string | 190 // returns DoubleToString(StringToDouble(string)) == string |
| 241 bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string); | 191 bool IsSpecialIndex(UnicodeCache* unicode_cache, String* string); |
| 242 } } // namespace v8::internal | 192 |
| 193 } // namespace internal |
| 194 } // namespace v8 |
| 243 | 195 |
| 244 #endif // V8_CONVERSIONS_H_ | 196 #endif // V8_CONVERSIONS_H_ |
| OLD | NEW |