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

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

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 #ifndef SKY_ENGINE_BINDINGS_CORE_V8_V8STRINGRESOURCE_H_
27 #define SKY_ENGINE_BINDINGS_CORE_V8_V8STRINGRESOURCE_H_
28
29 #include "sky/engine/wtf/Threading.h"
30 #include "sky/engine/wtf/text/AtomicString.h"
31 #include "sky/engine/wtf/text/WTFString.h"
32 #include "v8/include/v8.h"
33
34 namespace blink {
35
36 class ExternalStringVisitor;
37
38 // WebCoreStringResource is a helper class for v8ExternalString. It is used
39 // to manage the life-cycle of the underlying buffer of the external string.
40 class WebCoreStringResourceBase {
41 public:
42 explicit WebCoreStringResourceBase(const String& string)
43 : m_plainString(string)
44 {
45 #if ENABLE(ASSERT)
46 m_threadId = WTF::currentThread();
47 #endif
48 ASSERT(!string.isNull());
49 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC onsumption(string));
50 }
51
52 explicit WebCoreStringResourceBase(const AtomicString& string)
53 : m_plainString(string.string())
54 , m_atomicString(string)
55 {
56 #if ENABLE(ASSERT)
57 m_threadId = WTF::currentThread();
58 #endif
59 ASSERT(!string.isNull());
60 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC onsumption(string));
61 }
62
63 virtual ~WebCoreStringResourceBase()
64 {
65 #if ENABLE(ASSERT)
66 ASSERT(m_threadId == WTF::currentThread());
67 #endif
68 int reducedExternalMemory = -memoryConsumption(m_plainString);
69 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isN ull())
70 reducedExternalMemory -= memoryConsumption(m_atomicString.string());
71 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced ExternalMemory);
72 }
73
74 const String& webcoreString() { return m_plainString; }
75
76 const AtomicString& atomicString()
77 {
78 #if ENABLE(ASSERT)
79 ASSERT(m_threadId == WTF::currentThread());
80 #endif
81 if (m_atomicString.isNull()) {
82 m_atomicString = AtomicString(m_plainString);
83 ASSERT(!m_atomicString.isNull());
84 if (m_plainString.impl() != m_atomicString.impl())
85 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory (memoryConsumption(m_atomicString.string()));
86 }
87 return m_atomicString;
88 }
89
90 protected:
91 // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it.
92 String m_plainString;
93 // If this string is atomic or has been made atomic earlier the
94 // atomic string is held here. In the case where the string starts
95 // off non-atomic and becomes atomic later it is necessary to keep
96 // the original string alive because v8 may keep derived pointers
97 // into that string.
98 AtomicString m_atomicString;
99
100 private:
101 static int memoryConsumption(const String& string)
102 {
103 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar ));
104 }
105 #if ENABLE(ASSERT)
106 WTF::ThreadIdentifier m_threadId;
107 #endif
108 };
109
110 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v 8::String::ExternalStringResource {
111 public:
112 explicit WebCoreStringResource16(const String& string)
113 : WebCoreStringResourceBase(string)
114 {
115 ASSERT(!string.is8Bit());
116 }
117
118 explicit WebCoreStringResource16(const AtomicString& string)
119 : WebCoreStringResourceBase(string)
120 {
121 ASSERT(!string.is8Bit());
122 }
123
124 virtual size_t length() const override { return m_plainString.impl()->length (); }
125 virtual const uint16_t* data() const override
126 {
127 return reinterpret_cast<const uint16_t*>(m_plainString.impl()->character s16());
128 }
129 };
130
131 class WebCoreStringResource8 final : public WebCoreStringResourceBase, public v8 ::String::ExternalOneByteStringResource {
132 public:
133 explicit WebCoreStringResource8(const String& string)
134 : WebCoreStringResourceBase(string)
135 {
136 ASSERT(string.is8Bit());
137 }
138
139 explicit WebCoreStringResource8(const AtomicString& string)
140 : WebCoreStringResourceBase(string)
141 {
142 ASSERT(string.is8Bit());
143 }
144
145 virtual size_t length() const override { return m_plainString.impl()->length (); }
146 virtual const char* data() const override
147 {
148 return reinterpret_cast<const char*>(m_plainString.impl()->characters8() );
149 }
150 };
151
152 enum ExternalMode {
153 Externalize,
154 DoNotExternalize
155 };
156
157 template <typename StringType>
158 StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode);
159 String int32ToWebCoreString(int value);
160
161 // V8StringResource is an adapter class that converts V8 values to Strings
162 // or AtomicStrings as appropriate, using multiple typecast operators.
163 enum V8StringResourceMode {
164 DefaultMode,
165 TreatNullAsEmptyString,
166 TreatNullAsNullString,
167 TreatNullAndUndefinedAsNullString
168 };
169
170 template <V8StringResourceMode Mode = DefaultMode>
171 class V8StringResource {
172 public:
173 V8StringResource()
174 : m_mode(Externalize)
175 {
176 }
177
178 V8StringResource(v8::Handle<v8::Value> object)
179 : m_v8Object(object)
180 , m_mode(Externalize)
181 {
182 }
183
184 void operator=(v8::Handle<v8::Value> object)
185 {
186 m_v8Object = object;
187 }
188
189 void operator=(const String& string)
190 {
191 setString(string);
192 }
193
194 void operator=(std::nullptr_t)
195 {
196 setString(String());
197 }
198
199 bool prepare()
200 {
201 if (m_v8Object.IsEmpty())
202 return true;
203
204 if (!isValid()) {
205 setString(fallbackString());
206 return true;
207 }
208
209 if (LIKELY(m_v8Object->IsString()))
210 return true;
211
212 if (LIKELY(m_v8Object->IsInt32())) {
213 setString(int32ToWebCoreString(m_v8Object->Int32Value()));
214 return true;
215 }
216
217 m_mode = DoNotExternalize;
218 v8::TryCatch block;
219 m_v8Object = m_v8Object->ToString();
220 // Handle the case where an exception is thrown as part of invoking toSt ring on the object.
221 if (block.HasCaught()) {
222 block.ReThrow();
223 return false;
224 }
225 return true;
226 }
227 operator String() const { return toString<String>(); }
228 operator AtomicString() const { return toString<AtomicString>(); }
229
230 private:
231 bool isValid() const;
232 String fallbackString() const;
233
234 void setString(const String& string)
235 {
236 m_string = string;
237 m_v8Object.Clear(); // To signal that String is ready.
238 }
239
240 template <class StringType>
241 StringType toString() const
242 {
243 if (LIKELY(!m_v8Object.IsEmpty()))
244 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8: :Value>*>(&m_v8Object)->As<v8::String>(), m_mode);
245
246 return StringType(m_string);
247 }
248
249 v8::Handle<v8::Value> m_v8Object;
250 ExternalMode m_mode;
251 String m_string;
252 };
253
254 template<> inline bool V8StringResource<DefaultMode>::isValid() const
255 {
256 return true;
257 }
258
259 template<> inline String V8StringResource<DefaultMode>::fallbackString() const
260 {
261 ASSERT_NOT_REACHED();
262 return String();
263 }
264
265 template<> inline bool V8StringResource<TreatNullAsEmptyString>::isValid() const
266 {
267 return !m_v8Object->IsNull();
268 }
269
270 template<> inline String V8StringResource<TreatNullAsEmptyString>::fallbackStrin g() const
271 {
272 return emptyString();
273 }
274
275 template<> inline bool V8StringResource<TreatNullAsNullString>::isValid() const
276 {
277 return !m_v8Object->IsNull();
278 }
279
280 template<> inline String V8StringResource<TreatNullAsNullString>::fallbackString () const
281 {
282 return String();
283 }
284
285 template<> inline bool V8StringResource<TreatNullAndUndefinedAsNullString>::isVa lid() const
286 {
287 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined();
288 }
289
290 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa llbackString() const
291 {
292 return String();
293 }
294
295 } // namespace blink
296
297 #endif // SKY_ENGINE_BINDINGS_CORE_V8_V8STRINGRESOURCE_H_
OLDNEW
« no previous file with comments | « sky/engine/bindings/core/v8/V8ScriptRunner.cpp ('k') | sky/engine/bindings/core/v8/V8StringResource.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698