| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium 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 #include "bindings/core/v8/StringResource.h" | |
| 6 | |
| 7 #include "bindings/core/v8/V8Binding.h" | |
| 8 | |
| 9 namespace blink { | |
| 10 | |
| 11 template <class StringClass> | |
| 12 struct StringTraits { | |
| 13 static const StringClass& FromStringResource(StringResourceBase*); | |
| 14 template <typename V8StringTrait> | |
| 15 static StringClass FromV8String(v8::Local<v8::String>, int); | |
| 16 }; | |
| 17 | |
| 18 template <> | |
| 19 struct StringTraits<String> { | |
| 20 static const String& FromStringResource(StringResourceBase* resource) { | |
| 21 return resource->WebcoreString(); | |
| 22 } | |
| 23 template <typename V8StringTrait> | |
| 24 static String FromV8String(v8::Local<v8::String>, int); | |
| 25 }; | |
| 26 | |
| 27 template <> | |
| 28 struct StringTraits<AtomicString> { | |
| 29 static const AtomicString& FromStringResource(StringResourceBase* resource) { | |
| 30 return resource->GetAtomicString(); | |
| 31 } | |
| 32 template <typename V8StringTrait> | |
| 33 static AtomicString FromV8String(v8::Local<v8::String>, int); | |
| 34 }; | |
| 35 | |
| 36 struct V8StringTwoBytesTrait { | |
| 37 typedef UChar CharType; | |
| 38 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, | |
| 39 CharType* buffer, | |
| 40 int length) { | |
| 41 v8_string->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 struct V8StringOneByteTrait { | |
| 46 typedef LChar CharType; | |
| 47 ALWAYS_INLINE static void Write(v8::Local<v8::String> v8_string, | |
| 48 CharType* buffer, | |
| 49 int length) { | |
| 50 v8_string->WriteOneByte(buffer, 0, length); | |
| 51 } | |
| 52 }; | |
| 53 | |
| 54 template <typename V8StringTrait> | |
| 55 String StringTraits<String>::FromV8String(v8::Local<v8::String> v8_string, | |
| 56 int length) { | |
| 57 DCHECK(v8_string->Length() == length); | |
| 58 typename V8StringTrait::CharType* buffer; | |
| 59 String result = String::CreateUninitialized(length, buffer); | |
| 60 V8StringTrait::Write(v8_string, buffer, length); | |
| 61 return result; | |
| 62 } | |
| 63 | |
| 64 template <typename V8StringTrait> | |
| 65 AtomicString StringTraits<AtomicString>::FromV8String( | |
| 66 v8::Local<v8::String> v8_string, | |
| 67 int length) { | |
| 68 DCHECK(v8_string->Length() == length); | |
| 69 static const int kInlineBufferSize = | |
| 70 32 / sizeof(typename V8StringTrait::CharType); | |
| 71 if (length <= kInlineBufferSize) { | |
| 72 typename V8StringTrait::CharType inline_buffer[kInlineBufferSize]; | |
| 73 V8StringTrait::Write(v8_string, inline_buffer, length); | |
| 74 return AtomicString(inline_buffer, length); | |
| 75 } | |
| 76 typename V8StringTrait::CharType* buffer; | |
| 77 String string = String::CreateUninitialized(length, buffer); | |
| 78 V8StringTrait::Write(v8_string, buffer, length); | |
| 79 return AtomicString(string); | |
| 80 } | |
| 81 | |
| 82 template <typename StringType> | |
| 83 StringType V8StringToWebCoreString(v8::Local<v8::String> v8_string, | |
| 84 ExternalMode external) { | |
| 85 { | |
| 86 // This portion of this function is very hot in certain Dromeao benchmarks. | |
| 87 v8::String::Encoding encoding; | |
| 88 v8::String::ExternalStringResourceBase* resource = | |
| 89 v8_string->GetExternalStringResourceBase(&encoding); | |
| 90 if (LIKELY(!!resource)) { | |
| 91 StringResourceBase* base; | |
| 92 if (encoding == v8::String::ONE_BYTE_ENCODING) | |
| 93 base = static_cast<StringResource8*>(resource); | |
| 94 else | |
| 95 base = static_cast<StringResource16*>(resource); | |
| 96 return StringTraits<StringType>::FromStringResource(base); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 int length = v8_string->Length(); | |
| 101 if (UNLIKELY(!length)) | |
| 102 return StringType(""); | |
| 103 | |
| 104 bool one_byte = v8_string->ContainsOnlyOneByte(); | |
| 105 StringType result(one_byte ? StringTraits<StringType>::template FromV8String< | |
| 106 V8StringOneByteTrait>(v8_string, length) | |
| 107 : StringTraits<StringType>::template FromV8String< | |
| 108 V8StringTwoBytesTrait>(v8_string, length)); | |
| 109 | |
| 110 if (external != kExternalize || !v8_string->CanMakeExternal()) | |
| 111 return result; | |
| 112 | |
| 113 if (result.Is8Bit()) { | |
| 114 StringResource8* string_resource = new StringResource8(result); | |
| 115 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) | |
| 116 delete string_resource; | |
| 117 } else { | |
| 118 StringResource16* string_resource = new StringResource16(result); | |
| 119 if (UNLIKELY(!v8_string->MakeExternal(string_resource))) | |
| 120 delete string_resource; | |
| 121 } | |
| 122 return result; | |
| 123 } | |
| 124 | |
| 125 // Explicitly instantiate the above template with the expected | |
| 126 // parameterizations, to ensure the compiler generates the code; otherwise link | |
| 127 // errors can result in GCC 4.4. | |
| 128 template String V8StringToWebCoreString<String>(v8::Local<v8::String>, | |
| 129 ExternalMode); | |
| 130 template AtomicString V8StringToWebCoreString<AtomicString>( | |
| 131 v8::Local<v8::String>, | |
| 132 ExternalMode); | |
| 133 | |
| 134 // Fast but non thread-safe version. | |
| 135 String Int32ToWebCoreStringFast(int value) { | |
| 136 // Caching of small strings below is not thread safe: newly constructed | |
| 137 // AtomicString are not safely published. | |
| 138 DCHECK(IsMainThread()); | |
| 139 | |
| 140 // Most numbers used are <= 100. Even if they aren't used there's very little | |
| 141 // cost in using the space. | |
| 142 const int kLowNumbers = 100; | |
| 143 DEFINE_STATIC_LOCAL(Vector<AtomicString>, low_numbers, (kLowNumbers + 1)); | |
| 144 String web_core_string; | |
| 145 if (0 <= value && value <= kLowNumbers) { | |
| 146 web_core_string = low_numbers[value]; | |
| 147 if (!web_core_string) { | |
| 148 AtomicString value_string = AtomicString::Number(value); | |
| 149 low_numbers[value] = value_string; | |
| 150 web_core_string = value_string; | |
| 151 } | |
| 152 } else { | |
| 153 web_core_string = String::Number(value); | |
| 154 } | |
| 155 return web_core_string; | |
| 156 } | |
| 157 | |
| 158 String Int32ToWebCoreString(int value) { | |
| 159 // If we are on the main thread (this should always true for non-workers), | |
| 160 // call the faster one. | |
| 161 if (IsMainThread()) | |
| 162 return Int32ToWebCoreStringFast(value); | |
| 163 return String::Number(value); | |
| 164 } | |
| 165 | |
| 166 } // namespace blink | |
| OLD | NEW |