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