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 "core/CoreExport.h"
31 #include "wtf/Allocator.h"
32 #include "wtf/Threading.h"
33 #include "wtf/text/AtomicString.h"
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 USING_FAST_MALLOC(WebCoreStringResourceBase);
42 WTF_MAKE_NONCOPYABLE(WebCoreStringResourceBase);
45 explicit WebCoreStringResourceBase(const String& string)
46 : m_plainString(string) {
48 m_threadId = WTF::currentThread();
50 ASSERT(!string.isNull());
51 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
52 string.charactersSizeInBytes());
55 explicit WebCoreStringResourceBase(const AtomicString& string)
56 : m_plainString(string.getString()), m_atomicString(string) {
58 m_threadId = WTF::currentThread();
60 ASSERT(!string.isNull());
61 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
62 string.charactersSizeInBytes());
65 virtual ~WebCoreStringResourceBase() {
67 ASSERT(m_threadId == WTF::currentThread());
69 int64_t reducedExternalMemory = m_plainString.charactersSizeInBytes();
70 if (m_plainString.impl() != m_atomicString.impl() &&
71 !m_atomicString.isNull())
72 reducedExternalMemory += m_atomicString.charactersSizeInBytes();
73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
74 -reducedExternalMemory);
77 const String& webcoreString() { return m_plainString; }
79 const AtomicString& getAtomicString() {
81 ASSERT(m_threadId == WTF::currentThread());
83 if (m_atomicString.isNull()) {
84 m_atomicString = AtomicString(m_plainString);
85 ASSERT(!m_atomicString.isNull());
86 if (m_plainString.impl() != m_atomicString.impl())
87 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
88 m_atomicString.charactersSizeInBytes());
90 return m_atomicString;
94 // A shallow copy of the string. Keeps the string buffer alive until the V8
95 // engine garbage collects it.
97 // If this string is atomic or has been made atomic earlier the
98 // atomic string is held here. In the case where the string starts
99 // off non-atomic and becomes atomic later it is necessary to keep
100 // the original string alive because v8 may keep derived pointers
102 AtomicString m_atomicString;
106 WTF::ThreadIdentifier m_threadId;
110 class WebCoreStringResource16 final
111 : public WebCoreStringResourceBase,
112 public v8::String::ExternalStringResource {
113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16);
116 explicit WebCoreStringResource16(const String& string)
117 : WebCoreStringResourceBase(string) {
118 ASSERT(!string.is8Bit());
121 explicit WebCoreStringResource16(const AtomicString& string)
122 : WebCoreStringResourceBase(string) {
123 ASSERT(!string.is8Bit());
126 size_t length() const override { return m_plainString.impl()->length(); }
127 const uint16_t* data() const override {
128 return reinterpret_cast<const uint16_t*>(
129 m_plainString.impl()->characters16());
133 class WebCoreStringResource8 final
134 : public WebCoreStringResourceBase,
135 public v8::String::ExternalOneByteStringResource {
136 WTF_MAKE_NONCOPYABLE(WebCoreStringResource8);
139 explicit WebCoreStringResource8(const String& string)
140 : WebCoreStringResourceBase(string) {
141 ASSERT(string.is8Bit());
144 explicit WebCoreStringResource8(const AtomicString& string)
145 : WebCoreStringResourceBase(string) {
146 ASSERT(string.is8Bit());
149 size_t length() const override { return m_plainString.impl()->length(); }
150 const char* data() const override {
151 return reinterpret_cast<const char*>(m_plainString.impl()->characters8());
155 enum ExternalMode { Externalize, DoNotExternalize };
157 template <typename StringType>
158 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>,
160 CORE_EXPORT 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 {
176 V8StringResource() : m_mode(Externalize) {}
178 V8StringResource(v8::Local<v8::Value> object)
179 : m_v8Object(object), m_mode(Externalize) {}
181 V8StringResource(const String& string)
182 : m_mode(Externalize), m_string(string) {}
184 void operator=(v8::Local<v8::Value> object) { m_v8Object = object; }
186 void operator=(const String& string) { setString(string); }
188 void operator=(std::nullptr_t) { setString(String()); }
190 bool prepare() { // DEPRECATED
194 return m_v8Object->ToString(v8::Isolate::GetCurrent()->GetCurrentContext())
195 .ToLocal(&m_v8Object);
198 bool prepare(v8::Isolate* isolate, ExceptionState& exceptionState) {
199 return prepareFast() || prepareSlow(isolate, exceptionState);
202 bool prepare(ExceptionState& exceptionState) { // DEPRECATED
203 return prepareFast() ||
204 prepareSlow(v8::Isolate::GetCurrent(), exceptionState);
207 operator String() const { return toString<String>(); }
208 operator AtomicString() const { return toString<AtomicString>(); }
212 if (m_v8Object.IsEmpty())
216 setString(fallbackString());
220 if (LIKELY(m_v8Object->IsString()))
223 if (LIKELY(m_v8Object->IsInt32())) {
224 setString(int32ToWebCoreString(m_v8Object.As<v8::Int32>()->Value()));
228 m_mode = DoNotExternalize;
232 bool prepareSlow(v8::Isolate* isolate, ExceptionState& exceptionState) {
233 v8::TryCatch tryCatch(isolate);
234 if (!m_v8Object->ToString(isolate->GetCurrentContext())
235 .ToLocal(&m_v8Object)) {
236 exceptionState.rethrowV8Exception(tryCatch.Exception());
242 bool isValid() const;
243 String fallbackString() const;
245 void setString(const String& string) {
247 m_v8Object.Clear(); // To signal that String is ready.
250 template <class StringType>
251 StringType toString() const {
252 if (LIKELY(!m_v8Object.IsEmpty()))
253 return v8StringToWebCoreString<StringType>(
254 const_cast<v8::Local<v8::Value>*>(&m_v8Object)->As<v8::String>(),
257 return StringType(m_string);
260 v8::Local<v8::Value> m_v8Object;
266 inline bool V8StringResource<DefaultMode>::isValid() const {
271 inline String V8StringResource<DefaultMode>::fallbackString() const {
272 ASSERT_NOT_REACHED();
277 inline bool V8StringResource<TreatNullAsEmptyString>::isValid() const {
278 return !m_v8Object->IsNull();
282 inline String V8StringResource<TreatNullAsEmptyString>::fallbackString() const {
283 return emptyString();
287 inline bool V8StringResource<TreatNullAsNullString>::isValid() const {
288 return !m_v8Object->IsNull();
292 inline String V8StringResource<TreatNullAsNullString>::fallbackString() const {
297 inline bool V8StringResource<TreatNullAndUndefinedAsNullString>::isValid()
299 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined();
304 V8StringResource<TreatNullAndUndefinedAsNullString>::fallbackString() const {
310 #endif // V8StringResource_h