diff options
Diffstat (limited to 'chromium/v8/src/conversions.cc')
-rw-r--r-- | chromium/v8/src/conversions.cc | 129 |
1 files changed, 93 insertions, 36 deletions
diff --git a/chromium/v8/src/conversions.cc b/chromium/v8/src/conversions.cc index 397f3c57fb8..4efe9039048 100644 --- a/chromium/v8/src/conversions.cc +++ b/chromium/v8/src/conversions.cc @@ -1,39 +1,21 @@ // Copyright 2011 the V8 project authors. All rights reserved. -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following -// disclaimer in the documentation and/or other materials provided -// with the distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived -// from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. #include <stdarg.h> #include <limits.h> #include <cmath> -#include "conversions-inl.h" -#include "dtoa.h" -#include "list-inl.h" -#include "strtod.h" -#include "utils.h" +#include "src/v8.h" + +#include "src/assert-scope.h" +#include "src/conversions.h" +#include "src/conversions-inl.h" +#include "src/dtoa.h" +#include "src/factory.h" +#include "src/list-inl.h" +#include "src/strtod.h" +#include "src/utils.h" #ifndef _STLP_VENDOR_CSTD // STLPort doesn't import fpclassify into the std namespace. @@ -44,6 +26,47 @@ namespace v8 { namespace internal { +namespace { + +// C++-style iterator adaptor for StringCharacterStream +// (unlike C++ iterators the end-marker has different type). +class StringCharacterStreamIterator { + public: + class EndMarker {}; + + explicit StringCharacterStreamIterator(StringCharacterStream* stream); + + uint16_t operator*() const; + void operator++(); + bool operator==(EndMarker const&) const { return end_; } + bool operator!=(EndMarker const& m) const { return !end_; } + + private: + StringCharacterStream* const stream_; + uint16_t current_; + bool end_; +}; + + +StringCharacterStreamIterator::StringCharacterStreamIterator( + StringCharacterStream* stream) : stream_(stream) { + ++(*this); +} + +uint16_t StringCharacterStreamIterator::operator*() const { + return current_; +} + + +void StringCharacterStreamIterator::operator++() { + end_ = !stream_->HasMore(); + if (!end_) { + current_ = stream_->GetNext(); + } +} +} // End anonymous namespace. + + double StringToDouble(UnicodeCache* unicode_cache, const char* str, int flags, double empty_string_val) { // We cast to const uint8_t* here to avoid instantiating the @@ -56,7 +79,7 @@ double StringToDouble(UnicodeCache* unicode_cache, double StringToDouble(UnicodeCache* unicode_cache, - Vector<const char> str, + Vector<const uint8_t> str, int flags, double empty_string_val) { // We cast to const uint8_t* here to avoid instantiating the @@ -78,6 +101,23 @@ double StringToDouble(UnicodeCache* unicode_cache, } +// Converts a string into an integer. +double StringToInt(UnicodeCache* unicode_cache, + Vector<const uint8_t> vector, + int radix) { + return InternalStringToInt( + unicode_cache, vector.start(), vector.start() + vector.length(), radix); +} + + +double StringToInt(UnicodeCache* unicode_cache, + Vector<const uc16> vector, + int radix) { + return InternalStringToInt( + unicode_cache, vector.start(), vector.start() + vector.length(), radix); +} + + const char* DoubleToCString(double v, Vector<char> buffer) { switch (fpclassify(v)) { case FP_NAN: return "NaN"; @@ -256,7 +296,6 @@ static char* CreateExponentialRepresentation(char* decimal_rep, } - char* DoubleToExponentialCString(double value, int f) { const int kMaxDigitsAfterPoint = 20; // f might be -1 to signal that f was undefined in JavaScript. @@ -394,14 +433,14 @@ char* DoubleToRadixCString(double value, int radix) { if (is_negative) value = -value; // Get the integer part and the decimal part. - double integer_part = floor(value); + double integer_part = std::floor(value); double decimal_part = value - integer_part; // Convert the integer part starting from the back. Always generate // at least one digit. int integer_pos = kBufferSize - 2; do { - double remainder = fmod(integer_part, radix); + double remainder = std::fmod(integer_part, radix); integer_buffer[integer_pos--] = chars[static_cast<int>(remainder)]; integer_part -= remainder; integer_part /= radix; @@ -424,8 +463,8 @@ char* DoubleToRadixCString(double value, int radix) { while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) { decimal_part *= radix; decimal_buffer[decimal_pos++] = - chars[static_cast<int>(floor(decimal_part))]; - decimal_part -= floor(decimal_part); + chars[static_cast<int>(std::floor(decimal_part))]; + decimal_part -= std::floor(decimal_part); } decimal_buffer[decimal_pos] = '\0'; @@ -443,4 +482,22 @@ char* DoubleToRadixCString(double value, int radix) { return builder.Finalize(); } + +double StringToDouble(UnicodeCache* unicode_cache, + String* string, + int flags, + double empty_string_val) { + DisallowHeapAllocation no_gc; + String::FlatContent flat = string->GetFlatContent(); + // ECMA-262 section 15.1.2.3, empty string is NaN + if (flat.IsAscii()) { + return StringToDouble( + unicode_cache, flat.ToOneByteVector(), flags, empty_string_val); + } else { + return StringToDouble( + unicode_cache, flat.ToUC16Vector(), flags, empty_string_val); + } +} + + } } // namespace v8::internal |