V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
conversions.h
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_CONVERSIONS_H_
6 #define V8_CONVERSIONS_H_
7 
8 #include <limits>
9 
10 #include "src/base/logging.h"
11 #include "src/utils.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.
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.
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 
46 
47 inline double FastI2D(int x) {
48  // There is no rounding involved in converting an integer to a
49  // double, so this code should compile to a few instructions without
50  // any FPU pipeline stalls.
51  return static_cast<double>(x);
52 }
53 
54 
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 
63 // This function should match the exact semantics of ECMA-262 20.2.2.17.
64 inline float DoubleToFloat32(double x);
65 
66 
67 // This function should match the exact semantics of ECMA-262 9.4.
68 inline double DoubleToInteger(double x);
69 
70 
71 // This function should match the exact semantics of ECMA-262 9.5.
72 inline int32_t DoubleToInt32(double x);
73 
74 
75 // This function should match the exact semantics of ECMA-262 9.6.
76 inline uint32_t DoubleToUint32(double x);
77 
78 
79 // Enumeration for allowing octals and ignoring junk when converting
80 // strings to numbers.
81 enum ConversionFlags {
82  NO_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 
91 // Converts a string into a double value according to ECMA-262 9.3.1
92 double StringToDouble(Vector<const uint8_t> str, int flags,
93  double empty_string_val = 0);
94 double StringToDouble(Vector<const uc16> str, int flags,
95  double empty_string_val = 0);
96 // This version expects a zero-terminated character array.
97 double StringToDouble(const char* str, int flags, 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 V8_EXPORT_PRIVATE MaybeHandle<BigInt> BigIntLiteral(Isolate* isolate,
111  const char* string);
112 
113 const int kDoubleToCStringMinBufferSize = 100;
114 
115 // Converts a double to a string value according to ECMA-262 9.8.1.
116 // The buffer should be large enough for any floating point number.
117 // 100 characters is enough.
118 const char* DoubleToCString(double value, Vector<char> buffer);
119 
120 // Convert an int to a null-terminated string. The returned string is
121 // located inside the buffer, but not necessarily at the start.
122 const char* IntToCString(int n, Vector<char> buffer);
123 
124 // Additional number to string conversions for the number type.
125 // The caller is responsible for calling free on the returned pointer.
126 char* DoubleToFixedCString(double value, int f);
127 char* DoubleToExponentialCString(double value, int f);
128 char* DoubleToPrecisionCString(double value, int f);
129 char* DoubleToRadixCString(double value, int radix);
130 
131 static inline bool IsMinusZero(double value) {
132  return bit_cast<int64_t>(value) == bit_cast<int64_t>(-0.0);
133 }
134 
135 // Returns true if value can be converted to a SMI, and returns the resulting
136 // integer value of the SMI in |smi_int_value|.
137 inline bool DoubleToSmiInteger(double value, int* smi_int_value);
138 
139 inline bool IsSmiDouble(double value);
140 
141 // Integer32 is an integer that can be represented as a signed 32-bit
142 // integer. It has to be in the range [-2^31, 2^31 - 1].
143 // We also have to check for negative 0 as it is not an Integer32.
144 inline bool IsInt32Double(double value);
145 
146 // UInteger32 is an integer that can be represented as an unsigned 32-bit
147 // integer. It has to be in the range [0, 2^32 - 1].
148 // We also have to check for negative 0 as it is not a UInteger32.
149 inline bool IsUint32Double(double value);
150 
151 // Tries to convert |value| to a uint32, setting the result in |uint32_value|.
152 // If the output does not compare equal to the input, returns false and the
153 // value in |uint32_value| is left unspecified.
154 // Used for conversions such as in ECMA-262 15.4.2.2, which check "ToUint32(len)
155 // is equal to len".
156 inline bool DoubleToUint32IfEqualToSelf(double value, uint32_t* uint32_value);
157 
158 // Convert from Number object to C integer.
159 inline uint32_t PositiveNumberToUint32(Object* number);
160 inline int32_t NumberToInt32(Object* number);
161 inline uint32_t NumberToUint32(Object* number);
162 inline int64_t NumberToInt64(Object* number);
163 inline uint64_t PositiveNumberToUint64(Object* number);
164 
165 double StringToDouble(Isolate* isolate, Handle<String> string, int flags,
166  double empty_string_val = 0.0);
167 
168 inline bool TryNumberToSize(Object* number, size_t* result);
169 
170 // Converts a number into size_t.
171 inline size_t NumberToSize(Object* number);
172 
173 // returns DoubleToString(StringToDouble(string)) == string
174 bool IsSpecialIndex(String string);
175 
176 } // namespace internal
177 } // namespace v8
178 
179 #endif // V8_CONVERSIONS_H_
Definition: libplatform.h:13