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 |
31 #include "scanner-base.h" | 33 #include "scanner-base.h" |
32 | 34 |
33 namespace v8 { | 35 namespace v8 { |
34 namespace internal { | 36 namespace internal { |
35 | 37 |
| 38 // Maximum number of significant digits in decimal representation. |
| 39 // The longest possible double in decimal representation is |
| 40 // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 |
| 41 // (768 digits). If we parse a number whose first digits are equal to a |
| 42 // mean of 2 adjacent doubles (that could have up to 769 digits) the result |
| 43 // must be rounded to the bigger one unless the tail consists of zeros, so |
| 44 // we don't need to preserve all the digits. |
| 45 const int kMaxSignificantDigits = 772; |
| 46 |
| 47 static const double JUNK_STRING_VALUE = |
| 48 std::numeric_limits<double>::quiet_NaN(); |
| 49 |
| 50 static bool isDigit(int x, int radix) { |
| 51 return (x >= '0' && x <= '9' && x < '0' + radix) |
| 52 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) |
| 53 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); |
| 54 } |
| 55 |
| 56 |
| 57 static double SignedZero(bool negative) { |
| 58 return negative ? -0.0 : 0.0; |
| 59 } |
| 60 |
36 | 61 |
37 // The fast double-to-(unsigned-)int conversion routine does not guarantee | 62 // The fast double-to-(unsigned-)int conversion routine does not guarantee |
38 // rounding towards zero. | 63 // rounding towards zero. |
39 // The result is unspecified if x is infinite or NaN, or if the rounded | 64 // The result is unspecified if x is infinite or NaN, or if the rounded |
40 // integer value is outside the range of type int. | 65 // integer value is outside the range of type int. |
41 static inline int FastD2I(double x) { | 66 static inline int FastD2I(double x) { |
42 // The static_cast convertion from double to int used to be slow, but | 67 // The static_cast convertion from double to int used to be slow, but |
43 // as new benchmarks show, now it is much faster than lrint(). | 68 // as new benchmarks show, now it is much faster than lrint(). |
44 return static_cast<int>(x); | 69 return static_cast<int>(x); |
45 } | 70 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // Enumeration for allowing octals and ignoring junk when converting | 105 // Enumeration for allowing octals and ignoring junk when converting |
81 // strings to numbers. | 106 // strings to numbers. |
82 enum ConversionFlags { | 107 enum ConversionFlags { |
83 NO_FLAGS = 0, | 108 NO_FLAGS = 0, |
84 ALLOW_HEX = 1, | 109 ALLOW_HEX = 1, |
85 ALLOW_OCTALS = 2, | 110 ALLOW_OCTALS = 2, |
86 ALLOW_TRAILING_JUNK = 4 | 111 ALLOW_TRAILING_JUNK = 4 |
87 }; | 112 }; |
88 | 113 |
89 | 114 |
90 // Convert from Number object to C integer. | |
91 static inline int32_t NumberToInt32(Object* number); | |
92 static inline uint32_t NumberToUint32(Object* number); | |
93 | |
94 | |
95 // Converts a string into a double value according to ECMA-262 9.3.1 | 115 // Converts a string into a double value according to ECMA-262 9.3.1 |
96 double StringToDouble(UnicodeCache* unicode_cache, | 116 double StringToDouble(UnicodeCache* unicode_cache, |
97 String* str, | |
98 int flags, | |
99 double empty_string_val = 0); | |
100 double StringToDouble(UnicodeCache* unicode_cache, | |
101 Vector<const char> str, | 117 Vector<const char> str, |
102 int flags, | 118 int flags, |
103 double empty_string_val = 0); | 119 double empty_string_val = 0); |
104 double StringToDouble(UnicodeCache* unicode_cache, | 120 double StringToDouble(UnicodeCache* unicode_cache, |
105 Vector<const uc16> str, | 121 Vector<const uc16> str, |
106 int flags, | 122 int flags, |
107 double empty_string_val = 0); | 123 double empty_string_val = 0); |
108 // This version expects a zero-terminated character array. | 124 // This version expects a zero-terminated character array. |
109 double StringToDouble(UnicodeCache* unicode_cache, | 125 double StringToDouble(UnicodeCache* unicode_cache, |
110 const char* str, | 126 const char* str, |
111 int flags, | 127 int flags, |
112 double empty_string_val = 0); | 128 double empty_string_val = 0); |
113 | 129 |
114 // Converts a string into an integer. | |
115 double StringToInt(UnicodeCache* unicode_cache, String* str, int radix); | |
116 | |
117 // Converts a double to a string value according to ECMA-262 9.8.1. | 130 // Converts a double to a string value according to ECMA-262 9.8.1. |
118 // The buffer should be large enough for any floating point number. | 131 // The buffer should be large enough for any floating point number. |
119 // 100 characters is enough. | 132 // 100 characters is enough. |
120 const char* DoubleToCString(double value, Vector<char> buffer); | 133 const char* DoubleToCString(double value, Vector<char> buffer); |
121 | 134 |
122 // Convert an int to a null-terminated string. The returned string is | 135 // Convert an int to a null-terminated string. The returned string is |
123 // located inside the buffer, but not necessarily at the start. | 136 // located inside the buffer, but not necessarily at the start. |
124 const char* IntToCString(int n, Vector<char> buffer); | 137 const char* IntToCString(int n, Vector<char> buffer); |
125 | 138 |
126 // Additional number to string conversions for the number type. | 139 // Additional number to string conversions for the number type. |
127 // The caller is responsible for calling free on the returned pointer. | 140 // The caller is responsible for calling free on the returned pointer. |
128 char* DoubleToFixedCString(double value, int f); | 141 char* DoubleToFixedCString(double value, int f); |
129 char* DoubleToExponentialCString(double value, int f); | 142 char* DoubleToExponentialCString(double value, int f); |
130 char* DoubleToPrecisionCString(double value, int f); | 143 char* DoubleToPrecisionCString(double value, int f); |
131 char* DoubleToRadixCString(double value, int radix); | 144 char* DoubleToRadixCString(double value, int radix); |
132 | 145 |
133 } } // namespace v8::internal | 146 } } // namespace v8::internal |
134 | 147 |
135 #endif // V8_CONVERSIONS_H_ | 148 #endif // V8_CONVERSIONS_H_ |
OLD | NEW |