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