2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef V8StringResource_h
27 #define V8StringResource_h
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "wtf/Threading.h"
31 #include "wtf/text/AtomicString.h"
32 #include "wtf/text/WTFString.h"
37 class ExternalStringVisitor;
39 // WebCoreStringResource is a helper class for v8ExternalString. It is used
40 // to manage the life-cycle of the underlying buffer of the external string.
41 class WebCoreStringResourceBase {
43 explicit WebCoreStringResourceBase(const String& string)
44 : m_plainString(string)
47 m_threadId = WTF::currentThread();
49 ASSERT(!string.isNull());
50 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(string));
53 explicit WebCoreStringResourceBase(const AtomicString& string)
54 : m_plainString(string.string())
55 , m_atomicString(string)
58 m_threadId = WTF::currentThread();
60 ASSERT(!string.isNull());
61 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(string));
64 virtual ~WebCoreStringResourceBase()
67 ASSERT(m_threadId == WTF::currentThread());
69 int reducedExternalMemory = -memoryConsumption(m_plainString);
70 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull())
71 reducedExternalMemory -= memoryConsumption(m_atomicString.string());
72 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reducedExternalMemory);
75 const String& webcoreString() { return m_plainString; }
77 const AtomicString& atomicString()
80 ASSERT(m_threadId == WTF::currentThread());
82 if (m_atomicString.isNull()) {
83 m_atomicString = AtomicString(m_plainString);
84 ASSERT(!m_atomicString.isNull());
85 if (m_plainString.impl() != m_atomicString.impl())
86 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryConsumption(m_atomicString.string()));
88 return m_atomicString;
92 // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it.
94 // If this string is atomic or has been made atomic earlier the
95 // atomic string is held here. In the case where the string starts
96 // off non-atomic and becomes atomic later it is necessary to keep
97 // the original string alive because v8 may keep derived pointers
99 AtomicString m_atomicString;
102 static int memoryConsumption(const String& string)
104 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar));
107 WTF::ThreadIdentifier m_threadId;
111 class WebCoreStringResource16 FINAL : public WebCoreStringResourceBase, public v8::String::ExternalStringResource {
113 explicit WebCoreStringResource16(const String& string)
114 : WebCoreStringResourceBase(string)
116 ASSERT(!string.is8Bit());
119 explicit WebCoreStringResource16(const AtomicString& string)
120 : WebCoreStringResourceBase(string)
122 ASSERT(!string.is8Bit());
125 virtual size_t length() const OVERRIDE { return m_plainString.impl()->length(); }
126 virtual const uint16_t* data() const OVERRIDE
128 return reinterpret_cast<const uint16_t*>(m_plainString.impl()->characters16());
132 class WebCoreStringResource8 FINAL : public WebCoreStringResourceBase, public v8::String::ExternalAsciiStringResource {
134 explicit WebCoreStringResource8(const String& string)
135 : WebCoreStringResourceBase(string)
137 ASSERT(string.is8Bit());
140 explicit WebCoreStringResource8(const AtomicString& string)
141 : WebCoreStringResourceBase(string)
143 ASSERT(string.is8Bit());
146 virtual size_t length() const OVERRIDE { return m_plainString.impl()->length(); }
147 virtual const char* data() const OVERRIDE
149 return reinterpret_cast<const char*>(m_plainString.impl()->characters8());
158 template <typename StringType>
159 StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode);
160 String int32ToWebCoreString(int value);
162 // V8StringResource is an adapter class that converts V8 values to Strings
163 // or AtomicStrings as appropriate, using multiple typecast operators.
164 enum V8StringResourceMode {
166 TreatNullAsEmptyString,
167 TreatNullAsNullString,
168 TreatNullAndUndefinedAsNullString
171 template <V8StringResourceMode Mode = DefaultMode>
172 class V8StringResource {
175 : m_mode(Externalize)
179 V8StringResource(v8::Handle<v8::Value> object)
181 , m_mode(Externalize)
185 void operator=(v8::Handle<v8::Value> object)
190 void operator=(const String& string)
195 void operator=(std::nullptr_t)
205 m_v8Object = m_v8Object->ToString();
206 // Handle the case where an exception is thrown as part of invoking toString on the object.
207 if (m_v8Object.IsEmpty())
212 bool prepare(ExceptionState& exceptionState)
218 m_v8Object = m_v8Object->ToString();
219 // Handle the case where an exception is thrown as part of invoking toString on the object.
220 if (block.HasCaught()) {
221 exceptionState.rethrowV8Exception(block.Exception());
227 operator String() const { return toString<String>(); }
228 operator AtomicString() const { return toString<AtomicString>(); }
233 if (m_v8Object.IsEmpty())
237 setString(fallbackString());
241 if (LIKELY(m_v8Object->IsString()))
244 if (LIKELY(m_v8Object->IsInt32())) {
245 setString(int32ToWebCoreString(m_v8Object->Int32Value()));
249 m_mode = DoNotExternalize;
253 bool isValid() const;
254 String fallbackString() const;
256 void setString(const String& string)
259 m_v8Object.Clear(); // To signal that String is ready.
262 template <class StringType>
263 StringType toString() const
265 if (LIKELY(!m_v8Object.IsEmpty()))
266 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8::Value>*>(&m_v8Object)->As<v8::String>(), m_mode);
268 return StringType(m_string);
271 v8::Handle<v8::Value> m_v8Object;
276 template<> inline bool V8StringResource<DefaultMode>::isValid() const
281 template<> inline String V8StringResource<DefaultMode>::fallbackString() const
283 ASSERT_NOT_REACHED();
287 template<> inline bool V8StringResource<TreatNullAsEmptyString>::isValid() const
289 return !m_v8Object->IsNull();
292 template<> inline String V8StringResource<TreatNullAsEmptyString>::fallbackString() const
294 return emptyString();
297 template<> inline bool V8StringResource<TreatNullAsNullString>::isValid() const
299 return !m_v8Object->IsNull();
302 template<> inline String V8StringResource<TreatNullAsNullString>::fallbackString() const
307 template<> inline bool V8StringResource<TreatNullAndUndefinedAsNullString>::isValid() const
309 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined();
312 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fallbackString() const
319 #endif // V8StringResource_h