Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: Source/bindings/v8/V8StringResource.cpp

Issue 238533002: Use Traits in V8StringResource to avoid code duplication (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Slight clean up Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 template<bool oneByte> 55 template <typename V8StringTrait>
56 static StringClass fromV8String(v8::Handle<v8::String>, int); 56 static StringClass fromV8String(v8::Handle<v8::String>, int);
57 }; 57 };
58 58
59 template<> 59 template<>
60 struct StringTraits<String> { 60 struct StringTraits<String> {
61 static const String& fromStringResource(WebCoreStringResourceBase* resource) 61 static const String& fromStringResource(WebCoreStringResourceBase* resource)
62 { 62 {
63 return resource->webcoreString(); 63 return resource->webcoreString();
64 } 64 }
65 template<bool oneByte> 65 template <typename V8StringTrait>
66 static String fromV8String(v8::Handle<v8::String>, int); 66 static String fromV8String(v8::Handle<v8::String>, int);
67 }; 67 };
68 68
69 template<> 69 template<>
70 struct StringTraits<AtomicString> { 70 struct StringTraits<AtomicString> {
71 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource) 71 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource)
72 { 72 {
73 return resource->atomicString(); 73 return resource->atomicString();
74 } 74 }
75 template<bool oneByte> 75 template <typename V8StringTrait>
76 static AtomicString fromV8String(v8::Handle<v8::String>, int); 76 static AtomicString fromV8String(v8::Handle<v8::String>, int);
77 }; 77 };
78 78
79 template<> 79 struct V8StringTwoBytesTrait {
80 String StringTraits<String>::fromV8String<false>(v8::Handle<v8::String> v8String , int length) 80 typedef UChar CharType;
81 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b uffer, int length)
82 {
83 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
84 }
85 };
86
87 struct V8StringOneByteTrait {
88 typedef LChar CharType;
89 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b uffer, int length)
90 {
91 v8String->WriteOneByte(buffer, 0, length);
92 }
93 };
94
95 template <typename V8StringTrait>
96 String StringTraits<String>::fromV8String(v8::Handle<v8::String> v8String, int l ength)
81 { 97 {
82 ASSERT(v8String->Length() == length); 98 ASSERT(v8String->Length() == length);
83 UChar* buffer; 99 typename V8StringTrait::CharType* buffer;
84 String result = String::createUninitialized(length, buffer); 100 String result = String::createUninitialized(length, buffer);
85 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); 101 V8StringTrait::write(v8String, buffer, length);
86 return result; 102 return result;
87 } 103 }
88 104
89 template<> 105 template <typename V8StringTrait>
90 AtomicString StringTraits<AtomicString>::fromV8String<false>(v8::Handle<v8::Stri ng> v8String, int length) 106 AtomicString StringTraits<AtomicString>::fromV8String(v8::Handle<v8::String> v8S tring, int length)
91 { 107 {
92 ASSERT(v8String->Length() == length); 108 ASSERT(v8String->Length() == length);
93 static const int inlineBufferSize = 16; 109 static const int inlineBufferSize = 32 / sizeof(typename V8StringTrait::Char Type);
94 if (length <= inlineBufferSize) { 110 if (length <= inlineBufferSize) {
95 UChar inlineBuffer[inlineBufferSize]; 111 typename V8StringTrait::CharType inlineBuffer[inlineBufferSize];
96 v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length); 112 V8StringTrait::write(v8String, inlineBuffer, length);
97 return AtomicString(inlineBuffer, length); 113 return AtomicString(inlineBuffer, length);
98 } 114 }
99 UChar* buffer; 115 typename V8StringTrait::CharType* buffer;
100 String result = String::createUninitialized(length, buffer);
101 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
102 return AtomicString(result);
103 }
104
105 template<>
106 String StringTraits<String>::fromV8String<true>(v8::Handle<v8::String> v8String, int length)
107 {
108 ASSERT(v8String->Length() == length);
109 LChar* buffer;
110 String result = String::createUninitialized(length, buffer);
111 v8String->WriteOneByte(buffer, 0, length);
112 return result;
113 }
114
115 template<>
116 AtomicString StringTraits<AtomicString>::fromV8String<true>(v8::Handle<v8::Strin g> v8String, int length)
117 {
118 ASSERT(v8String->Length() == length);
119 static const int inlineBufferSize = 32;
120 if (length <= inlineBufferSize) {
121 LChar inlineBuffer[inlineBufferSize];
122 v8String->WriteOneByte(inlineBuffer, 0, length);
123 return AtomicString(inlineBuffer, length);
124 }
125 LChar* buffer;
126 String string = String::createUninitialized(length, buffer); 116 String string = String::createUninitialized(length, buffer);
127 v8String->WriteOneByte(buffer, 0, length); 117 V8StringTrait::write(v8String, buffer, length);
128 return AtomicString(string); 118 return AtomicString(string);
129 } 119 }
130 120
131 template<typename StringType> 121 template<typename StringType>
132 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external) 122 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external)
133 { 123 {
134 { 124 {
135 // A lot of WebCoreStringResourceBase::toWebCoreStringResourceBase is co pied here by hand for performance reasons. 125 // A lot of WebCoreStringResourceBase::toWebCoreStringResourceBase is co pied here by hand for performance reasons.
136 // This portion of this function is very hot in certain Dromeao benchmar ks. 126 // This portion of this function is very hot in certain Dromeao benchmar ks.
137 v8::String::Encoding encoding; 127 v8::String::Encoding encoding;
138 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding); 128 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding);
139 if (LIKELY(!!resource)) { 129 if (LIKELY(!!resource)) {
140 WebCoreStringResourceBase* base; 130 WebCoreStringResourceBase* base;
141 if (encoding == v8::String::ONE_BYTE_ENCODING) 131 if (encoding == v8::String::ONE_BYTE_ENCODING)
142 base = static_cast<WebCoreStringResource8*>(resource); 132 base = static_cast<WebCoreStringResource8*>(resource);
143 else 133 else
144 base = static_cast<WebCoreStringResource16*>(resource); 134 base = static_cast<WebCoreStringResource16*>(resource);
145 return StringTraits<StringType>::fromStringResource(base); 135 return StringTraits<StringType>::fromStringResource(base);
146 } 136 }
147 } 137 }
148 138
149 int length = v8String->Length(); 139 int length = v8String->Length();
150 if (UNLIKELY(!length)) 140 if (UNLIKELY(!length))
151 return StringType(""); 141 return StringType("");
152 142
153 bool oneByte = v8String->ContainsOnlyOneByte(); 143 bool oneByte = v8String->ContainsOnlyOneByte();
154 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< true>(v8String, length) : StringTraits<StringType>::template fromV8String<false> (v8String, length)); 144 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fro mV8String<V8StringTwoBytesTrait>(v8String, length));
155 145
156 if (external != Externalize || !v8String->CanMakeExternal()) 146 if (external != Externalize || !v8String->CanMakeExternal())
157 return result; 147 return result;
158 148
159 if (result.is8Bit()) { 149 if (result.is8Bit()) {
160 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt); 150 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt);
161 if (UNLIKELY(!v8String->MakeExternal(stringResource))) 151 if (UNLIKELY(!v8String->MakeExternal(stringResource)))
162 delete stringResource; 152 delete stringResource;
163 } else { 153 } else {
164 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult); 154 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 188
199 String int32ToWebCoreString(int value) 189 String int32ToWebCoreString(int value)
200 { 190 {
201 // If we are on the main thread (this should always true for non-workers), c all the faster one. 191 // If we are on the main thread (this should always true for non-workers), c all the faster one.
202 if (isMainThread()) 192 if (isMainThread())
203 return int32ToWebCoreStringFast(value); 193 return int32ToWebCoreStringFast(value);
204 return String::number(value); 194 return String::number(value);
205 } 195 }
206 196
207 } // namespace WebCore 197 } // namespace WebCore
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698