Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 | 45 |
| 46 void WebCoreStringResourceBase::visitStrings(ExternalStringVisitor* visitor) | 46 void WebCoreStringResourceBase::visitStrings(ExternalStringVisitor* visitor) |
| 47 { | 47 { |
| 48 visitor->visitJSExternalString(m_plainString.impl()); | 48 visitor->visitJSExternalString(m_plainString.impl()); |
| 49 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull( )) | 49 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull( )) |
| 50 visitor->visitJSExternalString(m_atomicString.impl()); | 50 visitor->visitJSExternalString(m_atomicString.impl()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 template<class StringClass> struct StringTraits { | 53 template<class StringClass> struct StringTraits { |
| 54 static const StringClass& fromStringResource(WebCoreStringResourceBase*); | 54 static const StringClass& fromStringResource(WebCoreStringResourceBase*); |
| 55 static bool is16BitAtomicString(StringClass&); | |
| 56 template<bool oneByte> | 55 template<bool oneByte> |
| 57 static StringClass fromV8String(v8::Handle<v8::String>, int); | 56 static StringClass fromV8String(v8::Handle<v8::String>, int); |
| 58 }; | 57 }; |
| 59 | 58 |
| 60 template<> | 59 template<> |
| 61 struct StringTraits<String> { | 60 struct StringTraits<String> { |
| 62 static const String& fromStringResource(WebCoreStringResourceBase* resource) | 61 static const String& fromStringResource(WebCoreStringResourceBase* resource) |
| 63 { | 62 { |
| 64 return resource->webcoreString(); | 63 return resource->webcoreString(); |
| 65 } | 64 } |
| 66 static bool is16BitAtomicString(String& string) | |
| 67 { | |
| 68 return false; | |
| 69 } | |
| 70 template<bool oneByte> | 65 template<bool oneByte> |
| 71 static String fromV8String(v8::Handle<v8::String>, int); | 66 static String fromV8String(v8::Handle<v8::String>, int); |
| 72 }; | 67 }; |
| 73 | 68 |
| 74 template<> | 69 template<> |
| 75 struct StringTraits<AtomicString> { | 70 struct StringTraits<AtomicString> { |
| 76 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource) | 71 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource) |
| 77 { | 72 { |
| 78 return resource->atomicString(); | 73 return resource->atomicString(); |
| 79 } | 74 } |
| 80 static bool is16BitAtomicString(AtomicString& string) | |
| 81 { | |
| 82 return !string.string().is8Bit(); | |
| 83 } | |
| 84 template<bool oneByte> | 75 template<bool oneByte> |
| 85 static AtomicString fromV8String(v8::Handle<v8::String>, int); | 76 static AtomicString fromV8String(v8::Handle<v8::String>, int); |
| 86 }; | 77 }; |
| 87 | 78 |
| 88 template<> | 79 template<> |
| 89 String StringTraits<String>::fromV8String<false>(v8::Handle<v8::String> v8String , int length) | 80 String StringTraits<String>::fromV8String<false>(v8::Handle<v8::String> v8String , int length) |
| 90 { | 81 { |
| 91 ASSERT(v8String->Length() == length); | 82 ASSERT(v8String->Length() == length); |
| 92 UChar* buffer; | 83 UChar* buffer; |
| 93 String result = String::createUninitialized(length, buffer); | 84 String result = String::createUninitialized(length, buffer); |
| 94 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | 85 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); |
| 86 if (result.containsOnlyLatin1()) | |
|
eseidel
2013/06/06 05:49:08
Every time we hit this it seems this is an error f
abarth-chromium
2013/06/06 06:01:55
I wouldn't say there is an error in V8. It's fine
dcarney
2013/06/06 06:45:20
there are plenty on cases where the cost of checki
| |
| 87 return String::make8BitFrom16BitSource(result.characters16(), result.len gth()); | |
| 95 return result; | 88 return result; |
| 96 } | 89 } |
| 97 | 90 |
| 98 template<> | 91 template<> |
| 99 AtomicString StringTraits<AtomicString>::fromV8String<false>(v8::Handle<v8::Stri ng> v8String, int length) | 92 AtomicString StringTraits<AtomicString>::fromV8String<false>(v8::Handle<v8::Stri ng> v8String, int length) |
| 100 { | 93 { |
| 101 ASSERT(v8String->Length() == length); | 94 ASSERT(v8String->Length() == length); |
| 102 static const int inlineBufferSize = 16; | 95 static const int inlineBufferSize = 16; |
| 103 if (length <= inlineBufferSize) { | 96 if (length <= inlineBufferSize) { |
| 104 UChar inlineBuffer[inlineBufferSize]; | 97 UChar inlineBuffer[inlineBufferSize]; |
|
dcarney
2013/06/06 06:45:20
are small strings not targeted here for a reason?
abarth-chromium
2013/06/06 06:55:26
Yes. The AtomicString(inlineBuffer, length) const
| |
| 105 v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length); | 98 v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length); |
| 106 return AtomicString(inlineBuffer, length); | 99 return AtomicString(inlineBuffer, length); |
| 107 } | 100 } |
| 108 UChar* buffer; | 101 UChar* buffer; |
| 109 String result = String::createUninitialized(length, buffer); | 102 String result = String::createUninitialized(length, buffer); |
| 110 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); | 103 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); |
| 104 if (result.containsOnlyLatin1()) { | |
|
dcarney
2013/06/06 06:45:20
maybe this check should be moved to the atomicstri
| |
| 105 AtomicString result(result.characters16(), result.length()); | |
| 106 ASSERT(result.is8Bit()); | |
| 107 return result; | |
| 108 } | |
| 111 return AtomicString(result); | 109 return AtomicString(result); |
| 112 } | 110 } |
| 113 | 111 |
| 114 template<> | 112 template<> |
| 115 String StringTraits<String>::fromV8String<true>(v8::Handle<v8::String> v8String, int length) | 113 String StringTraits<String>::fromV8String<true>(v8::Handle<v8::String> v8String, int length) |
| 116 { | 114 { |
| 117 ASSERT(v8String->Length() == length); | 115 ASSERT(v8String->Length() == length); |
| 118 LChar* buffer; | 116 LChar* buffer; |
| 119 String result = String::createUninitialized(length, buffer); | 117 String result = String::createUninitialized(length, buffer); |
| 120 v8String->WriteOneByte(buffer, 0, length); | 118 v8String->WriteOneByte(buffer, 0, length); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 150 if (encoding == v8::String::ONE_BYTE_ENCODING) | 148 if (encoding == v8::String::ONE_BYTE_ENCODING) |
| 151 base = static_cast<WebCoreStringResource8*>(resource); | 149 base = static_cast<WebCoreStringResource8*>(resource); |
| 152 else | 150 else |
| 153 base = static_cast<WebCoreStringResource16*>(resource); | 151 base = static_cast<WebCoreStringResource16*>(resource); |
| 154 return StringTraits<StringType>::fromStringResource(base); | 152 return StringTraits<StringType>::fromStringResource(base); |
| 155 } | 153 } |
| 156 } | 154 } |
| 157 | 155 |
| 158 int length = v8String->Length(); | 156 int length = v8String->Length(); |
| 159 if (UNLIKELY(!length)) | 157 if (UNLIKELY(!length)) |
| 160 return String(""); | 158 return String(""); // FIXME: Should return emptyString(); |
|
haraken
2013/06/06 05:22:11
Nit: Why is it hard to fix it now? I'm just curiou
abarth-chromium
2013/06/06 05:24:02
I'd prefer to fix it in a second CL. It should be
| |
| 161 | 159 |
| 162 bool oneByte = v8String->IsOneByte(); | 160 bool oneByte = v8String->IsOneByte(); |
| 163 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< true>(v8String, length) : StringTraits<StringType>::template fromV8String<false> (v8String, length)); | 161 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< true>(v8String, length) : StringTraits<StringType>::template fromV8String<false> (v8String, length)); |
| 164 | 162 |
| 165 if (external != Externalize || !v8String->CanMakeExternal()) | 163 if (external != Externalize || !v8String->CanMakeExternal()) |
| 166 return result; | 164 return result; |
| 167 | 165 |
| 168 if (oneByte && !StringTraits<StringType>::is16BitAtomicString(result)) { | 166 if (result.is8Bit()) { |
| 169 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt); | 167 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt); |
| 170 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | 168 if (UNLIKELY(!v8String->MakeExternal(stringResource))) |
| 171 delete stringResource; | 169 delete stringResource; |
| 172 } else { | 170 } else { |
| 173 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult); | 171 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult); |
| 174 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | 172 if (UNLIKELY(!v8String->MakeExternal(stringResource))) |
| 175 delete stringResource; | 173 delete stringResource; |
| 176 } | 174 } |
| 177 return result; | 175 return result; |
| 178 } | 176 } |
| 179 | 177 |
| 180 // Explicitly instantiate the above template with the expected parameterizations , | 178 // Explicitly instantiate the above template with the expected parameterizations , |
| 181 // to ensure the compiler generates the code; otherwise link errors can result i n GCC 4.4. | 179 // to ensure the compiler generates the code; otherwise link errors can result i n GCC 4.4. |
| 182 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, External Mode); | 180 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, External Mode); |
| 183 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin g>, ExternalMode); | 181 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin g>, ExternalMode); |
| 184 | 182 |
| 185 // Fast but non thread-safe version. | 183 // Fast but non thread-safe version. |
| 186 String int32ToWebCoreStringFast(int value) | 184 String int32ToWebCoreStringFast(int value) |
| 187 { | 185 { |
| 188 // Caching of small strings below is not thread safe: newly constructed Atom icString | 186 // Caching of small strings below is not thread safe: newly constructed Atom icString |
| 189 // are not safely published. | 187 // are not safely published. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 207 | 205 |
| 208 String int32ToWebCoreString(int value) | 206 String int32ToWebCoreString(int value) |
| 209 { | 207 { |
| 210 // If we are on the main thread (this should always true for non-workers), c all the faster one. | 208 // If we are on the main thread (this should always true for non-workers), c all the faster one. |
| 211 if (isMainThread()) | 209 if (isMainThread()) |
| 212 return int32ToWebCoreStringFast(value); | 210 return int32ToWebCoreStringFast(value); |
| 213 return String::number(value); | 211 return String::number(value); |
| 214 } | 212 } |
| 215 | 213 |
| 216 } // namespace WebCore | 214 } // namespace WebCore |
| OLD | NEW |