Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Unified Diff: third_party/WebKit/Source/bindings/core/v8/V8StringResource.cpp

Issue 2810413003: Extract WebCoreStringResouce out of V8StringResource (Closed)
Patch Set: Fix comment Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/V8StringResource.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/V8StringResource.cpp b/third_party/WebKit/Source/bindings/core/v8/V8StringResource.cpp
deleted file mode 100644
index ee2cc84c19830743a896d72bc515be35ad2561b1..0000000000000000000000000000000000000000
--- a/third_party/WebKit/Source/bindings/core/v8/V8StringResource.cpp
+++ /dev/null
@@ -1,190 +0,0 @@
-/*
- * Copyright (C) 2009 Google Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. 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.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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.
- */
-
-#include "bindings/core/v8/V8StringResource.h"
-
-#include "bindings/core/v8/V8Binding.h"
-
-namespace blink {
-
-template <class StringClass>
-struct StringTraits {
- static const StringClass& FromStringResource(WebCoreStringResourceBase*);
- template <typename V8StringTrait>
- static StringClass FromV8String(v8::Local<v8::String>, int);
-};
-
-template <>
-struct StringTraits<String> {
- static const String& FromStringResource(WebCoreStringResourceBase* resource) {
- return resource->WebcoreString();
- }
- template <typename V8StringTrait>
- static String FromV8String(v8::Local<v8::String>, int);
-};
-
-template <>
-struct StringTraits<AtomicString> {
- static const AtomicString& FromStringResource(
- WebCoreStringResourceBase* resource) {
- return resource->GetAtomicString();
- }
- template <typename V8StringTrait>
- static AtomicString FromV8String(v8::Local<v8::String>, int);
-};
-
-struct V8StringTwoBytesTrait {
- typedef UChar CharType;
- ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string,
- CharType* buffer,
- int length) {
- v8_string->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
- }
-};
-
-struct V8StringOneByteTrait {
- typedef LChar CharType;
- ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string,
- CharType* buffer,
- int length) {
- v8_string->WriteOneByte(buffer, 0, length);
- }
-};
-
-template <typename V8StringTrait>
-String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string,
- int length) {
- DCHECK_EQ(v8_string->Length(), length);
- typename V8StringTrait::CharType* buffer;
- String result = String::CreateUninitialized(length, buffer);
- V8StringTrait::Write(v8_string, buffer, length);
- return result;
-}
-
-template <typename V8StringTrait>
-AtomicString StringTraits<AtomicString>::FromV8String(
- v8::Local<v8::String> v8_string,
- int length) {
- DCHECK_EQ(v8_string->Length(), length);
- static const int kInlineBufferSize =
- 32 / sizeof(typename V8StringTrait::CharType);
- if (length <= kInlineBufferSize) {
- typename V8StringTrait::CharType inline_buffer[kInlineBufferSize];
- V8StringTrait::Write(v8_string, inline_buffer, length);
- return AtomicString(inline_buffer, length);
- }
- typename V8StringTrait::CharType* buffer;
- String string = String::CreateUninitialized(length, buffer);
- V8StringTrait::Write(v8_string, buffer, length);
- return AtomicString(string);
-}
-
-template <typename StringType>
-StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string,
- ExternalMode external) {
- {
- // This portion of this function is very hot in certain Dromeao benchmarks.
- v8::String::Encoding encoding;
- v8::String::ExternalStringResourceBase* resource =
- v8_string->GetExternalStringResourceBase(&encoding);
- if (LIKELY(!!resource)) {
- WebCoreStringResourceBase* base;
- if (encoding == v8::String::ONE_BYTE_ENCODING)
- base = static_cast<WebCoreStringResource8*>(resource);
- else
- base = static_cast<WebCoreStringResource16*>(resource);
- return StringTraits<StringType>::FromStringResource(base);
- }
- }
-
- int length = v8_string->Length();
- if (UNLIKELY(!length))
- return StringType("");
-
- bool one_byte = v8_string->ContainsOnlyOneByte();
- StringType result(one_byte ? StringTraits<StringType>::template FromV8String<
- V8StringOneByteTrait>(v8_string, length)
- : StringTraits<StringType>::template FromV8String<
- V8StringTwoBytesTrait>(v8_string, length));
-
- if (external != kExternalize || !v8_string->CanMakeExternal())
- return result;
-
- if (result.Is8Bit()) {
- WebCoreStringResource8* string_resource =
- new WebCoreStringResource8(result);
- if (UNLIKELY(!v8_string->MakeExternal(string_resource)))
- delete string_resource;
- } else {
- WebCoreStringResource16* string_resource =
- new WebCoreStringResource16(result);
- if (UNLIKELY(!v8_string->MakeExternal(string_resource)))
- delete string_resource;
- }
- return result;
-}
-
-// Explicitly instantiate the above template with the expected
-// parameterizations, to ensure the compiler generates the code; otherwise link
-// errors can result in GCC 4.4.
-template String V8StringToWebCoreString<String>(v8::Local<v8::String>,
- ExternalMode);
-template AtomicString V8StringToWebCoreString<AtomicString>(
- v8::Local<v8::String>,
- ExternalMode);
-
-// Fast but non thread-safe version.
-String Int32ToWebCoreStringFast(int value) {
- // Caching of small strings below is not thread safe: newly constructed
- // AtomicString are not safely published.
- DCHECK(IsMainThread());
-
- // Most numbers used are <= 100. Even if they aren't used there's very little
- // cost in using the space.
- const int kLowNumbers = 100;
- DEFINE_STATIC_LOCAL(Vector<AtomicString>, low_numbers, (kLowNumbers + 1));
- String web_core_string;
- if (0 <= value && value <= kLowNumbers) {
- web_core_string = low_numbers[value];
- if (!web_core_string) {
- AtomicString value_string = AtomicString::Number(value);
- low_numbers[value] = value_string;
- web_core_string = value_string;
- }
- } else {
- web_core_string = String::Number(value);
- }
- return web_core_string;
-}
-
-String Int32ToWebCoreString(int value) {
- // If we are on the main thread (this should always true for non-workers),
- // call the faster one.
- if (IsMainThread())
- return Int32ToWebCoreStringFast(value);
- return String::Number(value);
-}
-
-} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698