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

Side by Side Diff: sky/engine/bindings/core/v8/V8StringResource.cpp

Issue 922053002: Remove unused V8 integration code in Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
OLDNEW
(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 "sky/engine/config.h"
27 #include "sky/engine/bindings/core/v8/V8StringResource.h"
28
29 #include "sky/engine/bindings/core/v8/V8Binding.h"
30 #include "sky/engine/wtf/MainThread.h"
31
32 namespace blink {
33
34 template<class StringClass> struct StringTraits {
35 static const StringClass& fromStringResource(WebCoreStringResourceBase*);
36 template <typename V8StringTrait>
37 static StringClass fromV8String(v8::Handle<v8::String>, int);
38 };
39
40 template<>
41 struct StringTraits<String> {
42 static const String& fromStringResource(WebCoreStringResourceBase* resource)
43 {
44 return resource->webcoreString();
45 }
46 template <typename V8StringTrait>
47 static String fromV8String(v8::Handle<v8::String>, int);
48 };
49
50 template<>
51 struct StringTraits<AtomicString> {
52 static const AtomicString& fromStringResource(WebCoreStringResourceBase* res ource)
53 {
54 return resource->atomicString();
55 }
56 template <typename V8StringTrait>
57 static AtomicString fromV8String(v8::Handle<v8::String>, int);
58 };
59
60 struct V8StringTwoBytesTrait {
61 typedef UChar CharType;
62 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b uffer, int length)
63 {
64 v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length);
65 }
66 };
67
68 struct V8StringOneByteTrait {
69 typedef LChar CharType;
70 ALWAYS_INLINE static void write(v8::Handle<v8::String> v8String, CharType* b uffer, int length)
71 {
72 v8String->WriteOneByte(buffer, 0, length);
73 }
74 };
75
76 template <typename V8StringTrait>
77 String StringTraits<String>::fromV8String(v8::Handle<v8::String> v8String, int l ength)
78 {
79 ASSERT(v8String->Length() == length);
80 typename V8StringTrait::CharType* buffer;
81 String result = String::createUninitialized(length, buffer);
82 V8StringTrait::write(v8String, buffer, length);
83 return result;
84 }
85
86 template <typename V8StringTrait>
87 AtomicString StringTraits<AtomicString>::fromV8String(v8::Handle<v8::String> v8S tring, int length)
88 {
89 ASSERT(v8String->Length() == length);
90 static const int inlineBufferSize = 32 / sizeof(typename V8StringTrait::Char Type);
91 if (length <= inlineBufferSize) {
92 typename V8StringTrait::CharType inlineBuffer[inlineBufferSize];
93 V8StringTrait::write(v8String, inlineBuffer, length);
94 return AtomicString(inlineBuffer, length);
95 }
96 typename V8StringTrait::CharType* buffer;
97 String string = String::createUninitialized(length, buffer);
98 V8StringTrait::write(v8String, buffer, length);
99 return AtomicString(string);
100 }
101
102 template<typename StringType>
103 StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external)
104 {
105 {
106 // This portion of this function is very hot in certain Dromeao benchmar ks.
107 v8::String::Encoding encoding;
108 v8::String::ExternalStringResourceBase* resource = v8String->GetExternal StringResourceBase(&encoding);
109 if (LIKELY(!!resource)) {
110 WebCoreStringResourceBase* base;
111 if (encoding == v8::String::ONE_BYTE_ENCODING)
112 base = static_cast<WebCoreStringResource8*>(resource);
113 else
114 base = static_cast<WebCoreStringResource16*>(resource);
115 return StringTraits<StringType>::fromStringResource(base);
116 }
117 }
118
119 int length = v8String->Length();
120 if (UNLIKELY(!length))
121 return StringType("");
122
123 bool oneByte = v8String->ContainsOnlyOneByte();
124 StringType result(oneByte ? StringTraits<StringType>::template fromV8String< V8StringOneByteTrait>(v8String, length) : StringTraits<StringType>::template fro mV8String<V8StringTwoBytesTrait>(v8String, length));
125
126 if (external != Externalize || !v8String->CanMakeExternal())
127 return result;
128
129 if (result.is8Bit()) {
130 WebCoreStringResource8* stringResource = new WebCoreStringResource8(resu lt);
131 if (UNLIKELY(!v8String->MakeExternal(stringResource)))
132 delete stringResource;
133 } else {
134 WebCoreStringResource16* stringResource = new WebCoreStringResource16(re sult);
135 if (UNLIKELY(!v8String->MakeExternal(stringResource)))
136 delete stringResource;
137 }
138 return result;
139 }
140
141 // Explicitly instantiate the above template with the expected parameterizations ,
142 // to ensure the compiler generates the code; otherwise link errors can result i n GCC 4.4.
143 template String v8StringToWebCoreString<String>(v8::Handle<v8::String>, External Mode);
144 template AtomicString v8StringToWebCoreString<AtomicString>(v8::Handle<v8::Strin g>, ExternalMode);
145
146 // Fast but non thread-safe version.
147 String int32ToWebCoreStringFast(int value)
148 {
149 // Caching of small strings below is not thread safe: newly constructed Atom icString
150 // are not safely published.
151 ASSERT(isMainThread());
152
153 // Most numbers used are <= 100. Even if they aren't used there's very littl e cost in using the space.
154 const int kLowNumbers = 100;
155 DEFINE_STATIC_LOCAL(Vector<AtomicString>, lowNumbers, (kLowNumbers + 1));
156 String webCoreString;
157 if (0 <= value && value <= kLowNumbers) {
158 webCoreString = lowNumbers[value];
159 if (!webCoreString) {
160 AtomicString valueString = AtomicString::number(value);
161 lowNumbers[value] = valueString;
162 webCoreString = valueString;
163 }
164 } else {
165 webCoreString = String::number(value);
166 }
167 return webCoreString;
168 }
169
170 String int32ToWebCoreString(int value)
171 {
172 // If we are on the main thread (this should always true for non-workers), c all the faster one.
173 if (isMainThread())
174 return int32ToWebCoreStringFast(value);
175 return String::number(value);
176 }
177
178 } // namespace blink
OLDNEW
« no previous file with comments | « sky/engine/bindings/core/v8/V8StringResource.h ('k') | sky/engine/bindings/core/v8/V8ThrowException.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698