| 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); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 else | 143 else |
| 153 base = static_cast<WebCoreStringResource16*>(resource); | 144 base = static_cast<WebCoreStringResource16*>(resource); |
| 154 return StringTraits<StringType>::fromStringResource(base); | 145 return StringTraits<StringType>::fromStringResource(base); |
| 155 } | 146 } |
| 156 } | 147 } |
| 157 | 148 |
| 158 int length = v8String->Length(); | 149 int length = v8String->Length(); |
| 159 if (UNLIKELY(!length)) | 150 if (UNLIKELY(!length)) |
| 160 return String(""); | 151 return String(""); |
| 161 | 152 |
| 162 bool oneByte = v8String->IsOneByte(); | 153 bool oneByte = v8String->ContainsOnlyOneByte(); |
| 163 StringType result(oneByte ? StringTraits<StringType>::template fromV8String<
true>(v8String, length) : StringTraits<StringType>::template fromV8String<false>
(v8String, length)); | 154 StringType result(oneByte ? StringTraits<StringType>::template fromV8String<
true>(v8String, length) : StringTraits<StringType>::template fromV8String<false>
(v8String, length)); |
| 164 | 155 |
| 165 if (external != Externalize || !v8String->CanMakeExternal()) | 156 if (external != Externalize || !v8String->CanMakeExternal()) |
| 166 return result; | 157 return result; |
| 167 | 158 |
| 168 if (oneByte && !StringTraits<StringType>::is16BitAtomicString(result)) { | 159 if (result.is8Bit()) { |
| 169 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu
lt); | 160 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu
lt); |
| 170 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | 161 if (UNLIKELY(!v8String->MakeExternal(stringResource))) |
| 171 delete stringResource; | 162 delete stringResource; |
| 172 } else { | 163 } else { |
| 173 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re
sult); | 164 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re
sult); |
| 174 if (UNLIKELY(!v8String->MakeExternal(stringResource))) | 165 if (UNLIKELY(!v8String->MakeExternal(stringResource))) |
| 175 delete stringResource; | 166 delete stringResource; |
| 176 } | 167 } |
| 177 return result; | 168 return result; |
| 178 } | 169 } |
| 179 | 170 |
| 180 // Explicitly instantiate the above template with the expected parameterizations
, | 171 // 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. | 172 // 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); | 173 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, External
Mode); |
| 183 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin
g>, ExternalMode); | 174 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin
g>, ExternalMode); |
| 184 | 175 |
| 185 // Fast but non thread-safe version. | 176 // Fast but non thread-safe version. |
| 186 String int32ToWebCoreStringFast(int value) | 177 String int32ToWebCoreStringFast(int value) |
| 187 { | 178 { |
| 188 // Caching of small strings below is not thread safe: newly constructed Atom
icString | 179 // Caching of small strings below is not thread safe: newly constructed Atom
icString |
| 189 // are not safely published. | 180 // are not safely published. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 207 | 198 |
| 208 String int32ToWebCoreString(int value) | 199 String int32ToWebCoreString(int value) |
| 209 { | 200 { |
| 210 // If we are on the main thread (this should always true for non-workers), c
all the faster one. | 201 // If we are on the main thread (this should always true for non-workers), c
all the faster one. |
| 211 if (isMainThread()) | 202 if (isMainThread()) |
| 212 return int32ToWebCoreStringFast(value); | 203 return int32ToWebCoreStringFast(value); |
| 213 return String::number(value); | 204 return String::number(value); |
| 214 } | 205 } |
| 215 | 206 |
| 216 } // namespace WebCore | 207 } // namespace WebCore |
| OLD | NEW |