| OLD | NEW |
| (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 #include "bindings/core/v8/V8ValueCache.h" | |
| 27 | |
| 28 #include <utility> | |
| 29 #include "bindings/core/v8/V8Binding.h" | |
| 30 #include "platform/wtf/text/StringHash.h" | |
| 31 | |
| 32 namespace blink { | |
| 33 | |
| 34 StringCacheMapTraits::MapType* StringCacheMapTraits::MapFromWeakCallbackInfo( | |
| 35 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | |
| 36 return &(V8PerIsolateData::From(data.GetIsolate()) | |
| 37 ->GetStringCache() | |
| 38 ->string_cache_); | |
| 39 } | |
| 40 | |
| 41 void StringCacheMapTraits::Dispose(v8::Isolate* isolate, | |
| 42 v8::Global<v8::String> value, | |
| 43 StringImpl* key) { | |
| 44 V8PerIsolateData::From(isolate)->GetStringCache()->InvalidateLastString(); | |
| 45 key->Deref(); | |
| 46 } | |
| 47 | |
| 48 void StringCacheMapTraits::DisposeWeak( | |
| 49 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | |
| 50 V8PerIsolateData::From(data.GetIsolate()) | |
| 51 ->GetStringCache() | |
| 52 ->InvalidateLastString(); | |
| 53 data.GetParameter()->Deref(); | |
| 54 } | |
| 55 | |
| 56 void StringCacheMapTraits::OnWeakCallback( | |
| 57 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | |
| 58 V8PerIsolateData::From(data.GetIsolate()) | |
| 59 ->GetStringCache() | |
| 60 ->InvalidateLastString(); | |
| 61 } | |
| 62 | |
| 63 void StringCache::Dispose() { | |
| 64 // The MapType::Dispose callback calls StringCache::InvalidateLastString, | |
| 65 // which will only work while the destructor has not yet finished. Thus, | |
| 66 // we need to clear the map before the destructor has completed. | |
| 67 string_cache_.Clear(); | |
| 68 } | |
| 69 | |
| 70 static v8::Local<v8::String> MakeExternalString(v8::Isolate* isolate, | |
| 71 const String& string) { | |
| 72 if (string.Is8Bit()) { | |
| 73 StringResource8* string_resource = new StringResource8(string); | |
| 74 v8::Local<v8::String> new_string; | |
| 75 if (!v8::String::NewExternalOneByte(isolate, string_resource) | |
| 76 .ToLocal(&new_string)) { | |
| 77 delete string_resource; | |
| 78 return v8::String::Empty(isolate); | |
| 79 } | |
| 80 return new_string; | |
| 81 } | |
| 82 | |
| 83 StringResource16* string_resource = new StringResource16(string); | |
| 84 v8::Local<v8::String> new_string; | |
| 85 if (!v8::String::NewExternalTwoByte(isolate, string_resource) | |
| 86 .ToLocal(&new_string)) { | |
| 87 delete string_resource; | |
| 88 return v8::String::Empty(isolate); | |
| 89 } | |
| 90 return new_string; | |
| 91 } | |
| 92 | |
| 93 v8::Local<v8::String> StringCache::V8ExternalStringSlow( | |
| 94 v8::Isolate* isolate, | |
| 95 StringImpl* string_impl) { | |
| 96 if (!string_impl->length()) | |
| 97 return v8::String::Empty(isolate); | |
| 98 | |
| 99 StringCacheMapTraits::MapType::PersistentValueReference cached_v8_string = | |
| 100 string_cache_.GetReference(string_impl); | |
| 101 if (!cached_v8_string.IsEmpty()) { | |
| 102 last_string_impl_ = string_impl; | |
| 103 last_v8_string_ = cached_v8_string; | |
| 104 return last_v8_string_.NewLocal(isolate); | |
| 105 } | |
| 106 | |
| 107 return CreateStringAndInsertIntoCache(isolate, string_impl); | |
| 108 } | |
| 109 | |
| 110 void StringCache::SetReturnValueFromStringSlow( | |
| 111 v8::ReturnValue<v8::Value> return_value, | |
| 112 StringImpl* string_impl) { | |
| 113 if (!string_impl->length()) { | |
| 114 return_value.SetEmptyString(); | |
| 115 return; | |
| 116 } | |
| 117 | |
| 118 StringCacheMapTraits::MapType::PersistentValueReference cached_v8_string = | |
| 119 string_cache_.GetReference(string_impl); | |
| 120 if (!cached_v8_string.IsEmpty()) { | |
| 121 last_string_impl_ = string_impl; | |
| 122 last_v8_string_ = cached_v8_string; | |
| 123 last_v8_string_.SetReturnValue(return_value); | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 return_value.Set( | |
| 128 CreateStringAndInsertIntoCache(return_value.GetIsolate(), string_impl)); | |
| 129 } | |
| 130 | |
| 131 v8::Local<v8::String> StringCache::CreateStringAndInsertIntoCache( | |
| 132 v8::Isolate* isolate, | |
| 133 StringImpl* string_impl) { | |
| 134 DCHECK(!string_cache_.Contains(string_impl)); | |
| 135 DCHECK(string_impl->length()); | |
| 136 | |
| 137 v8::Local<v8::String> new_string = | |
| 138 MakeExternalString(isolate, String(string_impl)); | |
| 139 DCHECK(!new_string.IsEmpty()); | |
| 140 DCHECK(new_string->Length()); | |
| 141 | |
| 142 v8::UniquePersistent<v8::String> wrapper(isolate, new_string); | |
| 143 | |
| 144 string_impl->Ref(); | |
| 145 wrapper.MarkIndependent(); | |
| 146 string_cache_.Set(string_impl, std::move(wrapper), &last_v8_string_); | |
| 147 last_string_impl_ = string_impl; | |
| 148 | |
| 149 return new_string; | |
| 150 } | |
| 151 | |
| 152 void StringCache::InvalidateLastString() { | |
| 153 last_string_impl_ = nullptr; | |
| 154 last_v8_string_.Reset(); | |
| 155 } | |
| 156 | |
| 157 } // namespace blink | |
| OLD | NEW |