Chromium Code Reviews| 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())->stringCache()->InvalidateLastStri ng(); | 49 V8PerIsolateData::from(data.GetIsolate())->stringCache()->InvalidateLastStri ng(); |
| 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())->stringCache()->InvalidateLastStri ng(); | 55 V8PerIsolateData::from(data.GetIsolate())->stringCache()->InvalidateLastStri ng(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 | |
| 59 CompressibleStringCacheMapTraits::MapType* CompressibleStringCacheMapTraits::Map FromWeakCallbackInfo( | |
| 60 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) | |
| 61 { | |
| 62 return &(V8PerIsolateData::from(data.GetIsolate())->stringCache()->m_compres sibleStringCache); | |
| 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 | |
| 58 void StringCache::dispose() | 81 void StringCache::dispose() |
| 59 { | 82 { |
| 60 // The MapType::Dispose callback calls StringCache::InvalidateLastString, | 83 // The MapType::Dispose callback calls StringCache::InvalidateLastString, |
| 61 // which will only work while the destructor has not yet finished. Thus, | 84 // which will only work while the destructor has not yet finished. Thus, |
| 62 // we need to clear the map before the destructor has completed. | 85 // we need to clear the map before the destructor has completed. |
| 63 m_stringCache.Clear(); | 86 m_stringCache.Clear(); |
| 64 } | 87 } |
| 65 | 88 |
| 66 static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate, const Stri ng& string) | 89 static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate, const Stri ng& string) |
| 67 { | 90 { |
| 68 if (string.is8Bit()) { | 91 if (string.is8Bit()) { |
| 69 WebCoreStringResource8* stringResource = new WebCoreStringResource8(stri ng); | 92 WebCoreStringResource8* stringResource = new WebCoreStringResource8(stri ng); |
| 70 v8::Local<v8::String> newString; | 93 v8::Local<v8::String> newString; |
| 71 if (!v8::String::NewExternalOneByte(isolate, stringResource).ToLocal(&ne wString)) { | 94 if (!v8::String::NewExternalOneByte(isolate, stringResource).ToLocal(&ne wString)) { |
| 72 delete stringResource; | 95 delete stringResource; |
| 73 return v8::String::Empty(isolate); | 96 return v8::String::Empty(isolate); |
| 74 } | 97 } |
| 75 return newString; | 98 return newString; |
| 76 } | 99 } |
| 77 | 100 |
| 78 WebCoreStringResource16* stringResource = new WebCoreStringResource16(string ); | 101 WebCoreStringResource16* stringResource = new WebCoreStringResource16(string ); |
| 79 v8::Local<v8::String> newString; | 102 v8::Local<v8::String> newString; |
| 80 if (!v8::String::NewExternalTwoByte(isolate, stringResource).ToLocal(&newStr ing)) { | 103 if (!v8::String::NewExternalTwoByte(isolate, stringResource).ToLocal(&newStr ing)) { |
| 81 delete stringResource; | 104 delete stringResource; |
| 82 return v8::String::Empty(isolate); | 105 return v8::String::Empty(isolate); |
| 83 } | 106 } |
| 84 return newString; | 107 return newString; |
| 85 } | 108 } |
| 86 | 109 |
| 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 | |
| 87 v8::Local<v8::String> StringCache::v8ExternalStringSlow(v8::Isolate* isolate, St ringImpl* stringImpl) | 131 v8::Local<v8::String> StringCache::v8ExternalStringSlow(v8::Isolate* isolate, St ringImpl* stringImpl) |
| 88 { | 132 { |
| 89 if (!stringImpl->length()) | 133 if (!stringImpl->length()) |
| 90 return v8::String::Empty(isolate); | 134 return v8::String::Empty(isolate); |
| 91 | 135 |
| 92 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s tringCache.GetReference(stringImpl); | 136 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s tringCache.GetReference(stringImpl); |
| 93 if (!cachedV8String.IsEmpty()) { | 137 if (!cachedV8String.IsEmpty()) { |
| 94 m_lastStringImpl = stringImpl; | 138 m_lastStringImpl = stringImpl; |
| 95 m_lastV8String = cachedV8String; | 139 m_lastV8String = cachedV8String; |
| 96 return m_lastV8String.NewLocal(isolate); | 140 return m_lastV8String.NewLocal(isolate); |
| 97 } | 141 } |
| 98 | 142 |
| 99 return createStringAndInsertIntoCache(isolate, stringImpl); | 143 return createStringAndInsertIntoCache(isolate, stringImpl); |
| 100 } | 144 } |
| 101 | 145 |
| 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 return createStringAndInsertIntoCache(isolate, string); | |
| 152 } | |
| 153 | |
| 102 void StringCache::setReturnValueFromStringSlow(v8::ReturnValue<v8::Value> return Value, StringImpl* stringImpl) | 154 void StringCache::setReturnValueFromStringSlow(v8::ReturnValue<v8::Value> return Value, StringImpl* stringImpl) |
| 103 { | 155 { |
| 104 if (!stringImpl->length()) { | 156 if (!stringImpl->length()) { |
| 105 returnValue.SetEmptyString(); | 157 returnValue.SetEmptyString(); |
| 106 return; | 158 return; |
| 107 } | 159 } |
| 108 | 160 |
| 109 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s tringCache.GetReference(stringImpl); | 161 StringCacheMapTraits::MapType::PersistentValueReference cachedV8String = m_s tringCache.GetReference(stringImpl); |
| 110 if (!cachedV8String.IsEmpty()) { | 162 if (!cachedV8String.IsEmpty()) { |
| 111 m_lastStringImpl = stringImpl; | 163 m_lastStringImpl = stringImpl; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 129 v8::UniquePersistent<v8::String> wrapper(isolate, newString); | 181 v8::UniquePersistent<v8::String> wrapper(isolate, newString); |
| 130 | 182 |
| 131 stringImpl->ref(); | 183 stringImpl->ref(); |
| 132 wrapper.MarkIndependent(); | 184 wrapper.MarkIndependent(); |
| 133 m_stringCache.Set(stringImpl, wrapper.Pass(), &m_lastV8String); | 185 m_stringCache.Set(stringImpl, wrapper.Pass(), &m_lastV8String); |
| 134 m_lastStringImpl = stringImpl; | 186 m_lastStringImpl = stringImpl; |
| 135 | 187 |
| 136 return newString; | 188 return newString; |
| 137 } | 189 } |
| 138 | 190 |
| 191 v8::Local<v8::String> StringCache::createStringAndInsertIntoCache(v8::Isolate* i solate, const CompressibleString& string) | |
|
haraken
2015/11/26 11:50:03
The implementation of the StringCache looks correc
hajimehoshi
2015/11/27 11:03:58
Thanks.
| |
| 192 { | |
| 193 CompressibleStringImpl* stringImpl = string.impl().get(); | |
| 194 | |
| 195 ASSERT(!m_compressibleStringCache.Contains(stringImpl)); | |
| 196 ASSERT(stringImpl->originalLength()); | |
| 197 | |
| 198 v8::Local<v8::String> newString = makeExternalString(isolate, string); | |
| 199 ASSERT(!newString.IsEmpty()); | |
| 200 ASSERT(newString->Length()); | |
| 201 | |
| 202 v8::UniquePersistent<v8::String> wrapper(isolate, newString); | |
| 203 | |
| 204 stringImpl->ref(); | |
| 205 wrapper.MarkIndependent(); | |
| 206 CompressibleStringCacheMapTraits::MapType::PersistentValueReference unused; | |
| 207 m_compressibleStringCache.Set(stringImpl, wrapper.Pass(), &unused); | |
| 208 | |
| 209 return newString; | |
| 210 } | |
| 211 | |
| 139 void StringCache::InvalidateLastString() | 212 void StringCache::InvalidateLastString() |
| 140 { | 213 { |
| 141 m_lastStringImpl = nullptr; | 214 m_lastStringImpl = nullptr; |
| 142 m_lastV8String.Reset(); | 215 m_lastV8String.Reset(); |
| 143 } | 216 } |
| 144 | 217 |
| 145 } // namespace blink | 218 } // namespace blink |
| OLD | NEW |