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