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
3 // found in the LICENSE file.
4
5 #ifndef V8_NUMBERS_CONVERSIONS_H_
6 #define V8_NUMBERS_CONVERSIONS_H_
7
8 #include "src/base/export-template.h"
9 #include "src/base/logging.h"
10 #include "src/base/optional.h"
11 #include "src/base/strings.h"
12 #include "src/base/vector.h"
13 #include "src/common/globals.h"
14
15 namespace v8 {
16 namespace internal {
17
18 class BigInt;
19 template <typename T>
20 class Handle;
21
22 // The limit for the the fractionDigits/precision for toFixed, toPrecision
23 // and toExponential.
24 const int kMaxFractionDigits = 100;
25
26 // The fast double-to-(unsigned-)int conversion routine does not guarantee
27 // rounding towards zero.
28 // If x is NaN, the result is INT_MIN. Otherwise the result is the argument x,
29 // clamped to [INT_MIN, INT_MAX] and then rounded to an integer.
FastD2IChecked(double x)30 inline int FastD2IChecked(double x) {
31 if (!(x >= INT_MIN)) return INT_MIN; // Negation to catch NaNs.
32 if (x > INT_MAX) return INT_MAX;
33 return static_cast<int>(x);
34 }
35
36 // The fast double-to-(unsigned-)int conversion routine does not guarantee
37 // rounding towards zero.
38 // The result is undefined if x is infinite or NaN, or if the rounded
39 // integer value is outside the range of type int.
FastD2I(double x)40 inline int FastD2I(double x) {
41 DCHECK(x <= INT_MAX);
42 DCHECK(x >= INT_MIN);
43 return static_cast<int32_t>(x);
44 }
45
46 inline unsigned int FastD2UI(double x);
47
FastI2D(int x)48 inline double FastI2D(int x) {
49 // There is no rounding involved in converting an integer to a
50 // double, so this code should compile to a few instructions without
51 // any FPU pipeline stalls.
52 return static_cast<double>(x);
53 }
54
FastUI2D(unsigned x)55 inline double FastUI2D(unsigned x) {
56 // There is no rounding involved in converting an unsigned integer to a
57 // double, so this code should compile to a few instructions without
58 // any FPU pipeline stalls.
59 return static_cast<double>(x);
60 }
61
62 // This function should match the exact semantics of ECMA-262 20.2.2.17.
63 inline float DoubleToFloat32(double x);
64
65 // This function should match the exact semantics of ECMA-262 9.4.
66 inline double DoubleToInteger(double x);
67
68 // This function should match the exact semantics of ECMA-262 9.5.
69 inline int32_t DoubleToInt32(double x);
70
71 // This function should match the exact semantics of ECMA-262 9.6.
72 inline uint32_t DoubleToUint32(double x);
73
74 // These functions have similar semantics as the ones above, but are
75 // added for 64-bit integer types.
76 inline int64_t DoubleToInt64(double x);
77 inline uint64_t DoubleToUint64(double x);
78
79 // Enumeration for allowing octals and ignoring junk when converting
80 // strings to numbers.
81 enum ConversionFlags {
82 NO_CONVERSION_FLAGS = 0,
83 ALLOW_HEX = 1,
84 ALLOW_OCTAL = 2,
85 ALLOW_IMPLICIT_OCTAL = 4,
86 ALLOW_BINARY = 8,
87 ALLOW_TRAILING_JUNK = 16
88 };
89
90 // Converts a string into a double value according to ECMA-262 9.3.1
91 double StringToDouble(base::Vector<const uint8_t> str, int flags,
92 double empty_string_val = 0);
93 double StringToDouble(base::Vector<const base::uc16> str, int flags,
94 double empty_string_val = 0);
95 // This version expects a zero-terminated character array.
96 double V8_EXPORT_PRIVATE StringToDouble(const char* str, int flags,
97 double empty_string_val = 0);
98
99 double StringToInt(Isolate* isolate, Handle<String> string, int radix);
100
101 // This follows https://tc39.github.io/proposal-bigint/#sec-string-to-bigint
102 // semantics: "" => 0n.
103 MaybeHandle<BigInt> StringToBigInt(Isolate* isolate, Handle<String> string);
104
105 // This version expects a zero-terminated character array. Radix will
106 // be inferred from string prefix (case-insensitive):
107 // 0x -> hex
108 // 0o -> octal
109 // 0b -> binary
110 template <typename IsolateT>
111 EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
112 MaybeHandle<BigInt> BigIntLiteral(IsolateT* isolate, const char* string);
113
114 const int kDoubleToCStringMinBufferSize = 100;
115
116 // Converts a double to a string value according to ECMA-262 9.8.1.
117 // The buffer should be large enough for any floating point number.
118 // 100 characters is enough.
119 V8_EXPORT_PRIVATE const char* DoubleToCString(double value,
120 base::Vector<char> buffer);
121
122 V8_EXPORT_PRIVATE std::unique_ptr<char[]> BigIntLiteralToDecimal(
123 LocalIsolate* isolate, base::Vector<const uint8_t> literal);
124 // Convert an int to a null-terminated string. The returned string is
125 // located inside the buffer, but not necessarily at the start.
126 V8_EXPORT_PRIVATE const char* IntToCString(int n, base::Vector<char> buffer);
127
128 // Additional number to string conversions for the number type.
129 // The caller is responsible for calling free on the returned pointer.
130 char* DoubleToFixedCString(double value, int f);
131 char* DoubleToExponentialCString(double value, int f);
132 char* DoubleToPrecisionCString(double value, int f);
133 char* DoubleToRadixCString(double value, int radix);
134
IsMinusZero(double value)135 static inline bool IsMinusZero(double value) {
136 return bit_cast<int64_t>(value) == bit_cast<int64_t>(-0.0);
137 }
138
139 // Returns true if value can be converted to a SMI, and returns the resulting
140 // integer value of the SMI in |smi_int_value|.
141 inline bool DoubleToSmiInteger(double value, int* smi_int_value);
142
143 inline bool IsSmiDouble(double value);
144
145 // Integer32 is an integer that can be represented as a signed 32-bit
146 // integer. It has to be in the range [-2^31, 2^31 - 1].
147 // We also have to check for negative 0 as it is not an Integer32.
148 inline bool IsInt32Double(double value);
149
150 // UInteger32 is an integer that can be represented as an unsigned 32-bit
151 // integer. It has to be in the range [0, 2^32 - 1].
152 // We also have to check for negative 0 as it is not a UInteger32.
153 inline bool IsUint32Double(double value);
154
155 // Tries to convert |value| to a uint32, setting the result in |uint32_value|.
156 // If the output does not compare equal to the input, returns false and the
157 // value in |uint32_value| is left unspecified.
158 // Used for conversions such as in ECMA-262 15.4.2.2, which check "ToUint32(len)
159 // is equal to len".
160 inline bool DoubleToUint32IfEqualToSelf(double value, uint32_t* uint32_value);
161
162 // Convert from Number object to C integer.
163 inline uint32_t PositiveNumberToUint32(Object number);
164 inline int32_t NumberToInt32(Object number);
165 inline uint32_t NumberToUint32(Object number);
166 inline int64_t NumberToInt64(Object number);
167 inline uint64_t PositiveNumberToUint64(Object number);
168
169 double StringToDouble(Isolate* isolate, Handle<String> string, int flags,
170 double empty_string_val = 0.0);
171
172 // String to double helper without heap allocation.
173 // Returns base::nullopt if the string is longer than
174 // {max_length_for_conversion}. 23 was chosen because any representable double
175 // can be represented using a string of length 23.
176 V8_EXPORT_PRIVATE base::Optional<double> TryStringToDouble(
177 LocalIsolate* isolate, Handle<String> object,
178 int max_length_for_conversion = 23);
179
180 inline bool TryNumberToSize(Object number, size_t* result);
181
182 // Converts a number into size_t.
183 inline size_t NumberToSize(Object number);
184
185 // returns DoubleToString(StringToDouble(string)) == string
186 V8_EXPORT_PRIVATE bool IsSpecialIndex(String string);
187
188 } // namespace internal
189 } // namespace v8
190
191 #endif // V8_NUMBERS_CONVERSIONS_H_
192