OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #ifndef V8_CONVERSIONS_H_ | 28 #ifndef V8_CONVERSIONS_H_ |
29 #define V8_CONVERSIONS_H_ | 29 #define V8_CONVERSIONS_H_ |
30 | 30 |
| 31 #include <limits> |
| 32 |
| 33 #include "checks.h" |
| 34 #include "handles.h" |
| 35 #include "objects.h" |
31 #include "utils.h" | 36 #include "utils.h" |
32 | 37 |
33 namespace v8 { | 38 namespace v8 { |
34 namespace internal { | 39 namespace internal { |
35 | 40 |
36 class UnicodeCache; | 41 class UnicodeCache; |
37 | 42 |
38 // Maximum number of significant digits in decimal representation. | 43 // Maximum number of significant digits in decimal representation. |
39 // The longest possible double in decimal representation is | 44 // The longest possible double in decimal representation is |
40 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 | 45 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // located inside the buffer, but not necessarily at the start. | 161 // located inside the buffer, but not necessarily at the start. |
157 const char* IntToCString(int n, Vector<char> buffer); | 162 const char* IntToCString(int n, Vector<char> buffer); |
158 | 163 |
159 // Additional number to string conversions for the number type. | 164 // Additional number to string conversions for the number type. |
160 // The caller is responsible for calling free on the returned pointer. | 165 // The caller is responsible for calling free on the returned pointer. |
161 char* DoubleToFixedCString(double value, int f); | 166 char* DoubleToFixedCString(double value, int f); |
162 char* DoubleToExponentialCString(double value, int f); | 167 char* DoubleToExponentialCString(double value, int f); |
163 char* DoubleToPrecisionCString(double value, int f); | 168 char* DoubleToPrecisionCString(double value, int f); |
164 char* DoubleToRadixCString(double value, int radix); | 169 char* DoubleToRadixCString(double value, int radix); |
165 | 170 |
| 171 |
| 172 static inline bool IsMinusZero(double value) { |
| 173 static const DoubleRepresentation minus_zero(-0.0); |
| 174 return DoubleRepresentation(value) == minus_zero; |
| 175 } |
| 176 |
| 177 |
| 178 // Integer32 is an integer that can be represented as a signed 32-bit |
| 179 // integer. It has to be in the range [-2^31, 2^31 - 1]. |
| 180 // We also have to check for negative 0 as it is not an Integer32. |
| 181 static inline bool IsInt32Double(double value) { |
| 182 return !IsMinusZero(value) && |
| 183 value >= kMinInt && |
| 184 value <= kMaxInt && |
| 185 value == FastI2D(FastD2I(value)); |
| 186 } |
| 187 |
| 188 |
| 189 // Convert from Number object to C integer. |
| 190 inline int32_t NumberToInt32(Object* number) { |
| 191 if (number->IsSmi()) return Smi::cast(number)->value(); |
| 192 return DoubleToInt32(number->Number()); |
| 193 } |
| 194 |
| 195 |
| 196 inline uint32_t NumberToUint32(Object* number) { |
| 197 if (number->IsSmi()) return Smi::cast(number)->value(); |
| 198 return DoubleToUint32(number->Number()); |
| 199 } |
| 200 |
| 201 |
| 202 double StringToDouble(UnicodeCache* unicode_cache, |
| 203 String* string, |
| 204 int flags, |
| 205 double empty_string_val = 0.0); |
| 206 |
| 207 |
| 208 inline bool TryNumberToSize(Isolate* isolate, |
| 209 Object* number, size_t* result) { |
| 210 SealHandleScope shs(isolate); |
| 211 if (number->IsSmi()) { |
| 212 int value = Smi::cast(number)->value(); |
| 213 ASSERT(static_cast<unsigned>(Smi::kMaxValue) |
| 214 <= std::numeric_limits<size_t>::max()); |
| 215 if (value >= 0) { |
| 216 *result = static_cast<size_t>(value); |
| 217 return true; |
| 218 } |
| 219 return false; |
| 220 } else { |
| 221 ASSERT(number->IsHeapNumber()); |
| 222 double value = HeapNumber::cast(number)->value(); |
| 223 if (value >= 0 && |
| 224 value <= std::numeric_limits<size_t>::max()) { |
| 225 *result = static_cast<size_t>(value); |
| 226 return true; |
| 227 } else { |
| 228 return false; |
| 229 } |
| 230 } |
| 231 } |
| 232 |
| 233 // Converts a number into size_t. |
| 234 inline size_t NumberToSize(Isolate* isolate, |
| 235 Object* number) { |
| 236 size_t result = 0; |
| 237 bool is_valid = TryNumberToSize(isolate, number, &result); |
| 238 CHECK(is_valid); |
| 239 return result; |
| 240 } |
| 241 |
166 } } // namespace v8::internal | 242 } } // namespace v8::internal |
167 | 243 |
168 #endif // V8_CONVERSIONS_H_ | 244 #endif // V8_CONVERSIONS_H_ |
OLD | NEW |