OLD | NEW |
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 { | 48 { |
49 V8PerIsolateData::from(data.GetIsolate())->getStringCache()->InvalidateLastS
tring(); | 49 V8PerIsolateData::from(data.GetIsolate())->getStringCache()->InvalidateLastS
tring(); |
50 data.GetParameter()->deref(); | 50 data.GetParameter()->deref(); |
51 } | 51 } |
52 | 52 |
53 void StringCacheMapTraits::OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbac
kDataType>& data) | 53 void StringCacheMapTraits::OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbac
kDataType>& data) |
54 { | 54 { |
55 V8PerIsolateData::from(data.GetIsolate())->getStringCache()->InvalidateLastS
tring(); | 55 V8PerIsolateData::from(data.GetIsolate())->getStringCache()->InvalidateLastS
tring(); |
56 } | 56 } |
57 | 57 |
58 | |
59 CompressibleStringCacheMapTraits::MapType* CompressibleStringCacheMapTraits::Map
FromWeakCallbackInfo( | |
60 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) | |
61 { | |
62 return &(V8PerIsolateData::from(data.GetIsolate())->getStringCache()->m_comp
ressibleStringCache); | |
63 } | |
64 | |
65 void CompressibleStringCacheMapTraits::Dispose( | |
66 v8::Isolate* isolate, v8::Global<v8::String> value, CompressibleStringImpl*
key) | |
67 { | |
68 key->deref(); | |
69 } | |
70 | |
71 void CompressibleStringCacheMapTraits::DisposeWeak(const v8::WeakCallbackInfo<We
akCallbackDataType>& data) | |
72 { | |
73 data.GetParameter()->deref(); | |
74 } | |
75 | |
76 void CompressibleStringCacheMapTraits::OnWeakCallback(const v8::WeakCallbackInfo
<WeakCallbackDataType>& data) | |
77 { | |
78 } | |
79 | |
80 | |
81 void StringCache::dispose() | 58 void StringCache::dispose() |
82 { | 59 { |
83 // The MapType::Dispose callback calls StringCache::InvalidateLastString, | 60 // The MapType::Dispose callback calls StringCache::InvalidateLastString, |
84 // which will only work while the destructor has not yet finished. Thus, | 61 // which will only work while the destructor has not yet finished. Thus, |
85 // we need to clear the map before the destructor has completed. | 62 // we need to clear the map before the destructor has completed. |
86 m_stringCache.Clear(); | 63 m_stringCache.Clear(); |
87 } | 64 } |
88 | 65 |
89 static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate, const Stri
ng& string) | 66 static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate, const Stri
ng& string) |
90 { | 67 { |
91 if (string.is8Bit()) { | 68 if (string.is8Bit()) { |
92 WebCoreStringResource8* stringResource = new WebCoreStringResource8(stri
ng); | 69 WebCoreStringResource8* stringResource = new WebCoreStringResource8(stri
ng); |
93 v8::Local<v8::String> newString; | 70 v8::Local<v8::String> newString; |
94 if (!v8::String::NewExternalOneByte(isolate, stringResource).ToLocal(&ne
wString)) { | 71 if (!v8::String::NewExternalOneByte(isolate, stringResource).ToLocal(&ne
wString)) { |
95 delete stringResource; | 72 delete stringResource; |
96 return v8::String::Empty(isolate); | 73 return v8::String::Empty(isolate); |
97 } | 74 } |
98 return newString; | 75 return newString; |
99 } | 76 } |
100 | 77 |
101 WebCoreStringResource16* stringResource = new WebCoreStringResource16(string
); | 78 WebCoreStringResource16* stringResource = new WebCoreStringResource16(string
); |
102 v8::Local<v8::String> newString; | 79 v8::Local<v8::String> newString; |
103 if (!v8::String::NewExternalTwoByte(isolate, stringResource).ToLocal(&newStr
ing)) { | 80 if (!v8::String::NewExternalTwoByte(isolate, stringResource).ToLocal(&newStr
ing)) { |
104 delete stringResource; | 81 delete stringResource; |
105 return v8::String::Empty(isolate); | 82 return v8::String::Empty(isolate); |
106 } | 83 } |
107 return newString; | 84 return newString; |
108 } | 85 } |
109 | 86 |
110 static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate, const Comp
ressibleString& string) | |
111 { | |
112 if (string.is8Bit()) { | |
113 WebCoreCompressibleStringResource8* stringResource = new WebCoreCompress
ibleStringResource8(string); | |
114 v8::Local<v8::String> newString; | |
115 if (!v8::String::NewExternalOneByte(isolate, stringResource).ToLocal(&ne
wString)) { | |
116 delete stringResource; | |
117 return v8::String::Empty(isolate); | |
118 } | |
119 return newString; | |
120 } | |
121 | |
122 WebCoreCompressibleStringResource16* stringResource = new WebCoreCompressibl
eStringResource16(string); | |
123 v8::Local<v8::String> newString; | |
124 if (!v8::String::NewExternalTwoByte(isolate, stringResource).ToLocal(&newStr
ing)) { | |
125 delete stringResource; | |
126 return v8::String::Empty(isolate); | |
127 } | |
128 return newString; | |
129 } | |
130 | |
131 v8::Local<v8::String> StringCache::v8ExternalStringSlow(v8::Isolate* isolate, St
ringImpl* stringImpl) | 87 v8::Local<v8::String> StringCache::v8ExternalStringSlow(v8::Isolate* isolate, St
ringImpl* stringImpl) |
132 { | 88 { |
133 if (!stringImpl->length()) | 89 if (!stringImpl->length()) |
134 return v8::String::Empty(isolate); | 90 return v8::String::Empty(isolate); |
135 | 91 |
136 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s
tringCache.GetReference(stringImpl); | 92 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s
tringCache.GetReference(stringImpl); |
137 if (!cachedV8String.IsEmpty()) { | 93 if (!cachedV8String.IsEmpty()) { |
138 m_lastStringImpl = stringImpl; | 94 m_lastStringImpl = stringImpl; |
139 m_lastV8String = cachedV8String; | 95 m_lastV8String = cachedV8String; |
140 return m_lastV8String.NewLocal(isolate); | 96 return m_lastV8String.NewLocal(isolate); |
141 } | 97 } |
142 | 98 |
143 return createStringAndInsertIntoCache(isolate, stringImpl); | 99 return createStringAndInsertIntoCache(isolate, stringImpl); |
144 } | 100 } |
145 | 101 |
146 v8::Local<v8::String> StringCache::v8ExternalStringSlow(v8::Isolate* isolate, co
nst CompressibleString& string) | |
147 { | |
148 if (!string.length()) | |
149 return v8::String::Empty(isolate); | |
150 | |
151 CompressibleStringCacheMapTraits::MapType::PersistentValueReference cachedV8
String = m_compressibleStringCache.GetReference(string.impl()); | |
152 if (!cachedV8String.IsEmpty()) | |
153 return cachedV8String.NewLocal(isolate); | |
154 | |
155 return createStringAndInsertIntoCache(isolate, string); | |
156 } | |
157 | |
158 void StringCache::setReturnValueFromStringSlow(v8::ReturnValue<v8::Value> return
Value, StringImpl* stringImpl) | 102 void StringCache::setReturnValueFromStringSlow(v8::ReturnValue<v8::Value> return
Value, StringImpl* stringImpl) |
159 { | 103 { |
160 if (!stringImpl->length()) { | 104 if (!stringImpl->length()) { |
161 returnValue.SetEmptyString(); | 105 returnValue.SetEmptyString(); |
162 return; | 106 return; |
163 } | 107 } |
164 | 108 |
165 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s
tringCache.GetReference(stringImpl); | 109 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s
tringCache.GetReference(stringImpl); |
166 if (!cachedV8String.IsEmpty()) { | 110 if (!cachedV8String.IsEmpty()) { |
167 m_lastStringImpl = stringImpl; | 111 m_lastStringImpl = stringImpl; |
(...skipping 17 matching lines...) Expand all Loading... |
185 v8::UniquePersistent<v8::String> wrapper(isolate, newString); | 129 v8::UniquePersistent<v8::String> wrapper(isolate, newString); |
186 | 130 |
187 stringImpl->ref(); | 131 stringImpl->ref(); |
188 wrapper.MarkIndependent(); | 132 wrapper.MarkIndependent(); |
189 m_stringCache.Set(stringImpl, std::move(wrapper), &m_lastV8String); | 133 m_stringCache.Set(stringImpl, std::move(wrapper), &m_lastV8String); |
190 m_lastStringImpl = stringImpl; | 134 m_lastStringImpl = stringImpl; |
191 | 135 |
192 return newString; | 136 return newString; |
193 } | 137 } |
194 | 138 |
195 v8::Local<v8::String> StringCache::createStringAndInsertIntoCache(v8::Isolate* i
solate, const CompressibleString& string) | |
196 { | |
197 CompressibleStringImpl* stringImpl = string.impl(); | |
198 | |
199 ASSERT(!m_compressibleStringCache.Contains(stringImpl)); | |
200 ASSERT(stringImpl->originalLength()); | |
201 | |
202 v8::Local<v8::String> newString = makeExternalString(isolate, string); | |
203 ASSERT(!newString.IsEmpty()); | |
204 ASSERT(newString->Length()); | |
205 | |
206 v8::UniquePersistent<v8::String> wrapper(isolate, newString); | |
207 | |
208 stringImpl->ref(); | |
209 wrapper.MarkIndependent(); | |
210 // CompressibleStringImpl objects are NOT cached on |m_stringCache| or | |
211 // |m_lastStringImpl|. It's because if even one objects holds a StringImpl | |
212 // object in a CompressibleStringImpl, uncompressed string will exists even | |
213 // when compressing the string. | |
214 CompressibleStringCacheMapTraits::MapType::PersistentValueReference unused; | |
215 m_compressibleStringCache.Set(stringImpl, std::move(wrapper), &unused); | |
216 | |
217 return newString; | |
218 } | |
219 | |
220 void StringCache::InvalidateLastString() | 139 void StringCache::InvalidateLastString() |
221 { | 140 { |
222 m_lastStringImpl = nullptr; | 141 m_lastStringImpl = nullptr; |
223 m_lastV8String.Reset(); | 142 m_lastV8String.Reset(); |
224 } | 143 } |
225 | 144 |
226 } // namespace blink | 145 } // namespace blink |
OLD | NEW |