| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | 4 |
| 26 #include "bindings/core/v8/V8StringResource.h" | 5 #include "bindings/core/v8/StringResource.h" |
| 27 | 6 |
| 28 #include "bindings/core/v8/V8Binding.h" | 7 #include "bindings/core/v8/V8Binding.h" |
| 29 | 8 |
| 30 namespace blink { | 9 namespace blink { |
| 31 | 10 |
| 32 template <class StringClass> | 11 template <class StringClass> |
| 33 struct StringTraits { | 12 struct StringTraits { |
| 34 static const StringClass& FromStringResource(WebCoreStringResourceBase*); | 13 static const StringClass& FromStringResource(StringResourceBase*); |
| 35 template <typename V8StringTrait> | 14 template <typename V8StringTrait> |
| 36 static StringClass FromV8String(v8::Local<v8::String>, int); | 15 static StringClass FromV8String(v8::Local<v8::String>, int); |
| 37 }; | 16 }; |
| 38 | 17 |
| 39 template <> | 18 template <> |
| 40 struct StringTraits<String> { | 19 struct StringTraits<String> { |
| 41 static const String& FromStringResource(WebCoreStringResourceBase* resource) { | 20 static const String& FromStringResource(StringResourceBase* resource) { |
| 42 return resource->WebcoreString(); | 21 return resource->WebcoreString(); |
| 43 } | 22 } |
| 44 template <typename V8StringTrait> | 23 template <typename V8StringTrait> |
| 45 static String FromV8String(v8::Local<v8::String>, int); | 24 static String FromV8String(v8::Local<v8::String>, int); |
| 46 }; | 25 }; |
| 47 | 26 |
| 48 template <> | 27 template <> |
| 49 struct StringTraits<AtomicString> { | 28 struct StringTraits<AtomicString> { |
| 50 static const AtomicString& FromStringResource( | 29 static const AtomicString& FromStringResource(StringResourceBase* resource) { |
| 51 WebCoreStringResourceBase* resource) { | |
| 52 return resource->GetAtomicString(); | 30 return resource->GetAtomicString(); |
| 53 } | 31 } |
| 54 template <typename V8StringTrait> | 32 template <typename V8StringTrait> |
| 55 static AtomicString FromV8String(v8::Local<v8::String>, int); | 33 static AtomicString FromV8String(v8::Local<v8::String>, int); |
| 56 }; | 34 }; |
| 57 | 35 |
| 58 struct V8StringTwoBytesTrait { | 36 struct V8StringTwoBytesTrait { |
| 59 typedef UChar CharType; | 37 typedef UChar CharType; |
| 60 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, | 38 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, |
| 61 CharType* buffer, | 39 CharType* buffer, |
| 62 int length) { | 40 int length) { |
| 63 v8_string->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | 41 v8_string->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); |
| 64 } | 42 } |
| 65 }; | 43 }; |
| 66 | 44 |
| 67 struct V8StringOneByteTrait { | 45 struct V8StringOneByteTrait { |
| 68 typedef LChar CharType; | 46 typedef LChar CharType; |
| 69 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, | 47 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, |
| 70 CharType* buffer, | 48 CharType* buffer, |
| 71 int length) { | 49 int length) { |
| 72 v8_string->WriteOneByte(buffer, 0, length); | 50 v8_string->WriteOneByte(buffer, 0, length); |
| 73 } | 51 } |
| 74 }; | 52 }; |
| 75 | 53 |
| 76 template <typename V8StringTrait> | 54 template <typename V8StringTrait> |
| 77 String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, | 55 String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, |
| 78 int length) { | 56 int length) { |
| 79 DCHECK_EQ(v8_string->Length(), length); | 57 DCHECK(v8_string->Length() == length); |
| 80 typename V8StringTrait::CharType* buffer; | 58 typename V8StringTrait::CharType* buffer; |
| 81 String result = String::CreateUninitialized(length, buffer); | 59 String result = String::CreateUninitialized(length, buffer); |
| 82 V8StringTrait::Write(v8_string, buffer, length); | 60 V8StringTrait::Write(v8_string, buffer, length); |
| 83 return result; | 61 return result; |
| 84 } | 62 } |
| 85 | 63 |
| 86 template <typename V8StringTrait> | 64 template <typename V8StringTrait> |
| 87 AtomicString StringTraits<AtomicString>::FromV8String( | 65 AtomicString StringTraits<AtomicString>::FromV8String( |
| 88 v8::Local<v8::String> v8_string, | 66 v8::Local<v8::String> v8_string, |
| 89 int length) { | 67 int length) { |
| 90 DCHECK_EQ(v8_string->Length(), length); | 68 DCHECK(v8_string->Length() == length); |
| 91 static const int kInlineBufferSize = | 69 static const int kInlineBufferSize = |
| 92 32 / sizeof(typename V8StringTrait::CharType); | 70 32 / sizeof(typename V8StringTrait::CharType); |
| 93 if (length <= kInlineBufferSize) { | 71 if (length <= kInlineBufferSize) { |
| 94 typename V8StringTrait::CharType inline_buffer[kInlineBufferSize]; | 72 typename V8StringTrait::CharType inline_buffer[kInlineBufferSize]; |
| 95 V8StringTrait::Write(v8_string, inline_buffer, length); | 73 V8StringTrait::Write(v8_string, inline_buffer, length); |
| 96 return AtomicString(inline_buffer, length); | 74 return AtomicString(inline_buffer, length); |
| 97 } | 75 } |
| 98 typename V8StringTrait::CharType* buffer; | 76 typename V8StringTrait::CharType* buffer; |
| 99 String string = String::CreateUninitialized(length, buffer); | 77 String string = String::CreateUninitialized(length, buffer); |
| 100 V8StringTrait::Write(v8_string, buffer, length); | 78 V8StringTrait::Write(v8_string, buffer, length); |
| 101 return AtomicString(string); | 79 return AtomicString(string); |
| 102 } | 80 } |
| 103 | 81 |
| 104 template <typename StringType> | 82 template <typename StringType> |
| 105 StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string, | 83 StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string, |
| 106 ExternalMode external) { | 84 ExternalMode external) { |
| 107 { | 85 { |
| 108 // This portion of this function is very hot in certain Dromeao benchmarks. | 86 // This portion of this function is very hot in certain Dromeao benchmarks. |
| 109 v8::String::Encoding encoding; | 87 v8::String::Encoding encoding; |
| 110 v8::String::ExternalStringResourceBase* resource = | 88 v8::String::ExternalStringResourceBase* resource = |
| 111 v8_string->GetExternalStringResourceBase(&encoding); | 89 v8_string->GetExternalStringResourceBase(&encoding); |
| 112 if (LIKELY(!!resource)) { | 90 if (LIKELY(!!resource)) { |
| 113 WebCoreStringResourceBase* base; | 91 StringResourceBase* base; |
| 114 if (encoding == v8::String::ONE_BYTE_ENCODING) | 92 if (encoding == v8::String::ONE_BYTE_ENCODING) |
| 115 base = static_cast<WebCoreStringResource8*>(resource); | 93 base = static_cast<StringResource8*>(resource); |
| 116 else | 94 else |
| 117 base = static_cast<WebCoreStringResource16*>(resource); | 95 base = static_cast<StringResource16*>(resource); |
| 118 return StringTraits<StringType>::FromStringResource(base); | 96 return StringTraits<StringType>::FromStringResource(base); |
| 119 } | 97 } |
| 120 } | 98 } |
| 121 | 99 |
| 122 int length = v8_string->Length(); | 100 int length = v8_string->Length(); |
| 123 if (UNLIKELY(!length)) | 101 if (UNLIKELY(!length)) |
| 124 return StringType(""); | 102 return StringType(""); |
| 125 | 103 |
| 126 bool one_byte = v8_string->ContainsOnlyOneByte(); | 104 bool one_byte = v8_string->ContainsOnlyOneByte(); |
| 127 StringType result(one_byte ? StringTraits<StringType>::template FromV8String< | 105 StringType result(one_byte ? StringTraits<StringType>::template FromV8String< |
| 128 V8StringOneByteTrait>(v8_string, length) | 106 V8StringOneByteTrait>(v8_string, length) |
| 129 : StringTraits<StringType>::template FromV8String< | 107 : StringTraits<StringType>::template FromV8String< |
| 130 V8StringTwoBytesTrait>(v8_string, length)); | 108 V8StringTwoBytesTrait>(v8_string, length)); |
| 131 | 109 |
| 132 if (external != kExternalize || !v8_string->CanMakeExternal()) | 110 if (external != kExternalize || !v8_string->CanMakeExternal()) |
| 133 return result; | 111 return result; |
| 134 | 112 |
| 135 if (result.Is8Bit()) { | 113 if (result.Is8Bit()) { |
| 136 WebCoreStringResource8* string_resource = | 114 StringResource8* string_resource = new StringResource8(result); |
| 137 new WebCoreStringResource8(result); | |
| 138 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) | 115 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) |
| 139 delete string_resource; | 116 delete string_resource; |
| 140 } else { | 117 } else { |
| 141 WebCoreStringResource16* string_resource = | 118 StringResource16* string_resource = new StringResource16(result); |
| 142 new WebCoreStringResource16(result); | |
| 143 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) | 119 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) |
| 144 delete string_resource; | 120 delete string_resource; |
| 145 } | 121 } |
| 146 return result; | 122 return result; |
| 147 } | 123 } |
| 148 | 124 |
| 149 // Explicitly instantiate the above template with the expected | 125 // Explicitly instantiate the above template with the expected |
| 150 // parameterizations, to ensure the compiler generates the code; otherwise link | 126 // parameterizations, to ensure the compiler generates the code; otherwise link |
| 151 // errors can result in GCC 4.4. | 127 // errors can result in GCC 4.4. |
| 152 template String V8StringToWebCoreString<String>(v8::Local<v8::String>, | 128 template String V8StringToWebCoreString<String>(v8::Local<v8::String>, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 181 | 157 |
| 182 String Int32ToWebCoreString(int value) { | 158 String Int32ToWebCoreString(int value) { |
| 183 // If we are on the main thread (this should always true for non-workers), | 159 // If we are on the main thread (this should always true for non-workers), |
| 184 // call the faster one. | 160 // call the faster one. |
| 185 if (IsMainThread()) | 161 if (IsMainThread()) |
| 186 return Int32ToWebCoreStringFast(value); | 162 return Int32ToWebCoreStringFast(value); |
| 187 return String::Number(value); | 163 return String::Number(value); |
| 188 } | 164 } |
| 189 | 165 |
| 190 } // namespace blink | 166 } // namespace blink |
| OLD | NEW |