00001 // Copyright 2006-2008 the V8 project authors. All rights reserved. 00002 // Redistribution and use in source and binary forms, with or without 00003 // modification, are permitted provided that the following conditions are 00004 // met: 00005 // 00006 // * Redistributions of source code must retain the above copyright 00007 // notice, this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above 00009 // copyright notice, this list of conditions and the following 00010 // disclaimer in the documentation and/or other materials provided 00011 // with the distribution. 00012 // * Neither the name of Google Inc. nor the names of its 00013 // contributors may be used to endorse or promote products derived 00014 // from this software without specific prior written permission. 00015 // 00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 #ifndef V8_CONVERSIONS_H_ 00029 #define V8_CONVERSIONS_H_ 00030 00031 namespace v8 { namespace internal { 00032 00033 // The fast double-to-int conversion routine does not guarantee 00034 // rounding towards zero. 00035 // The result is unspecified if x is infinite or NaN, or if the rounded 00036 // integer value is outside the range of type int. 00037 static inline int FastD2I(double x); 00038 00039 00040 static inline double FastI2D(int x) { 00041 // There is no rounding involved in converting an integer to a 00042 // double, so this code should compile to a few instructions without 00043 // any FPU pipeline stalls. 00044 return static_cast<double>(x); 00045 } 00046 00047 00048 static inline double FastUI2D(unsigned x) { 00049 // There is no rounding involved in converting an unsigned integer to a 00050 // double, so this code should compile to a few instructions without 00051 // any FPU pipeline stalls. 00052 return static_cast<double>(x); 00053 } 00054 00055 00056 // This function should match the exact semantics of ECMA-262 9.4. 00057 static inline double DoubleToInteger(double x); 00058 00059 00060 // This function should match the exact semantics of ECMA-262 9.5. 00061 static inline int32_t DoubleToInt32(double x); 00062 00063 00064 // This function should match the exact semantics of ECMA-262 9.6. 00065 static inline uint32_t DoubleToUint32(double x) { 00066 return static_cast<uint32_t>(DoubleToInt32(x)); 00067 } 00068 00069 00070 // Returns the value (0 .. 15) of a hexadecimal character c. 00071 // If c is not a legal hexadecimal character, returns a value < 0. 00072 int HexValue(uc32 c); 00073 00074 00075 // Enumeration for allowing octals and ignoring junk when converting 00076 // strings to numbers. 00077 enum ConversionFlags { 00078 NO_FLAGS = 0, 00079 ALLOW_HEX = 1, 00080 ALLOW_OCTALS = 2, 00081 ALLOW_TRAILING_JUNK = 4 00082 }; 00083 00084 00085 // Convert from Number object to C integer. 00086 static inline int32_t NumberToInt32(Object* number); 00087 static inline uint32_t NumberToUint32(Object* number); 00088 00089 00090 // Converts a string into a double value according to ECMA-262 9.3.1 00091 double StringToDouble(const char* str, int flags, double empty_string_val = 0); 00092 double StringToDouble(String* str, int flags, double empty_string_val = 0); 00093 00094 // Converts a string into an integer. 00095 int StringToInt(String* str, int index, int radix, double* value); 00096 int StringToInt(const char* str, int index, int radix, double* value); 00097 00098 // Converts a double to a string value according to ECMA-262 9.8.1. 00099 // The buffer should be large enough for any floating point number. 00100 // 100 characters is enough. 00101 const char* DoubleToCString(double value, Vector<char> buffer); 00102 00103 // Convert an int to a null-terminated string. The returned string is 00104 // located inside the buffer, but not necessarily at the start. 00105 const char* IntToCString(int n, Vector<char> buffer); 00106 00107 // Additional number to string conversions for the number type. 00108 // The caller is responsible for calling free on the returned pointer. 00109 char* DoubleToFixedCString(double value, int f); 00110 char* DoubleToExponentialCString(double value, int f); 00111 char* DoubleToPrecisionCString(double value, int f); 00112 char* DoubleToRadixCString(double value, int radix); 00113 00114 } } // namespace v8::internal 00115 00116 #endif // V8_CONVERSIONS_H_