| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_UTIL_H_ | 5 #ifndef V8_UTIL_H_ |
| 6 #define V8_UTIL_H_ | 6 #define V8_UTIL_H_ |
| 7 | 7 |
| 8 #include "v8.h" | 8 #include "v8.h" |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 static MapType* MapFromWeakCallbackInfo( | 127 static MapType* MapFromWeakCallbackInfo( |
| 128 const WeakCallbackInfo<WeakCallbackInfoType>& data) { | 128 const WeakCallbackInfo<WeakCallbackInfoType>& data) { |
| 129 return nullptr; | 129 return nullptr; |
| 130 } | 130 } |
| 131 static K KeyFromWeakCallbackInfo( | 131 static K KeyFromWeakCallbackInfo( |
| 132 const WeakCallbackInfo<WeakCallbackInfoType>& data) { | 132 const WeakCallbackInfo<WeakCallbackInfoType>& data) { |
| 133 return K(); | 133 return K(); |
| 134 } | 134 } |
| 135 static void DisposeCallbackData(WeakCallbackInfoType* data) {} | 135 static void DisposeCallbackData(WeakCallbackInfoType* data) {} |
| 136 static void Dispose(Isolate* isolate, Global<V> value, K key) {} | 136 static void Dispose(Isolate* isolate, Global<V> value, K key) {} |
| 137 static void DisposeWeak(Isolate* isolate, | 137 // This is a second pass callback, so SetSecondPassCallback cannot be called. |
| 138 const WeakCallbackInfo<WeakCallbackInfoType>& data, | 138 static void DisposeWeak(const WeakCallbackInfo<WeakCallbackInfoType>& data) {} |
| 139 K key) {} | |
| 140 | 139 |
| 141 private: | 140 private: |
| 142 template <typename T> | 141 template <typename T> |
| 143 struct RemovePointer<T*> { | 142 struct RemovePointer<T*> { |
| 144 typedef T Type; | 143 typedef T Type; |
| 145 }; | 144 }; |
| 146 }; | 145 }; |
| 147 | 146 |
| 148 | 147 |
| 149 /** | 148 /** |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 * by the Traits class. | 445 * by the Traits class. |
| 447 */ | 446 */ |
| 448 Global<V> SetUnique(const K& key, Global<V>* persistent) { | 447 Global<V> SetUnique(const K& key, Global<V>* persistent) { |
| 449 if (Traits::kCallbackType != kNotWeak) { | 448 if (Traits::kCallbackType != kNotWeak) { |
| 450 WeakCallbackType callback_type = | 449 WeakCallbackType callback_type = |
| 451 Traits::kCallbackType == kWeakWithInternalFields | 450 Traits::kCallbackType == kWeakWithInternalFields |
| 452 ? WeakCallbackType::kInternalFields | 451 ? WeakCallbackType::kInternalFields |
| 453 : WeakCallbackType::kParameter; | 452 : WeakCallbackType::kParameter; |
| 454 Local<V> value(Local<V>::New(this->isolate(), *persistent)); | 453 Local<V> value(Local<V>::New(this->isolate(), *persistent)); |
| 455 persistent->template SetWeak<typename Traits::WeakCallbackDataType>( | 454 persistent->template SetWeak<typename Traits::WeakCallbackDataType>( |
| 456 Traits::WeakCallbackParameter(this, key, value), WeakCallback, | 455 Traits::WeakCallbackParameter(this, key, value), FirstWeakCallback, |
| 457 callback_type); | 456 callback_type); |
| 458 } | 457 } |
| 459 PersistentContainerValue old_value = | 458 PersistentContainerValue old_value = |
| 460 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent)); | 459 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent)); |
| 461 return this->Release(old_value).Pass(); | 460 return this->Release(old_value).Pass(); |
| 462 } | 461 } |
| 463 | 462 |
| 464 /** | 463 /** |
| 465 * Put a value into the map and update the reference. | 464 * Put a value into the map and update the reference. |
| 466 * Restrictions of GetReference apply here as well. | 465 * Restrictions of GetReference apply here as well. |
| 467 */ | 466 */ |
| 468 Global<V> Set(const K& key, Global<V> value, | 467 Global<V> Set(const K& key, Global<V> value, |
| 469 PersistentValueReference* reference) { | 468 PersistentValueReference* reference) { |
| 470 *reference = this->Leak(&value); | 469 *reference = this->Leak(&value); |
| 471 return SetUnique(key, &value); | 470 return SetUnique(key, &value); |
| 472 } | 471 } |
| 473 | 472 |
| 474 private: | 473 private: |
| 475 static void WeakCallback( | 474 static void FirstWeakCallback( |
| 476 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) { | 475 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) { |
| 477 if (Traits::kCallbackType != kNotWeak) { | 476 if (Traits::kCallbackType != kNotWeak) { |
| 478 GlobalValueMap<K, V, Traits>* persistentValueMap = | 477 auto map = Traits::MapFromWeakCallbackInfo(data); |
| 479 Traits::MapFromWeakCallbackInfo(data); | |
| 480 K key = Traits::KeyFromWeakCallbackInfo(data); | 478 K key = Traits::KeyFromWeakCallbackInfo(data); |
| 481 persistentValueMap->RemoveWeak(key); | 479 map->RemoveWeak(key); |
| 482 Traits::DisposeWeak(data.GetIsolate(), data, key); | 480 data.SetSecondPassCallback(SecondWeakCallback); |
| 483 } | 481 } |
| 484 } | 482 } |
| 483 |
| 484 static void SecondWeakCallback( |
| 485 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) { |
| 486 Traits::DisposeWeak(data); |
| 487 } |
| 485 }; | 488 }; |
| 486 | 489 |
| 487 | 490 |
| 488 /** | 491 /** |
| 489 * A map that uses Global as value and std::map as the backing | 492 * A map that uses Global as value and std::map as the backing |
| 490 * implementation. Persistents are held non-weak. | 493 * implementation. Persistents are held non-weak. |
| 491 * | 494 * |
| 492 * C++11 embedders don't need this class, as they can use | 495 * C++11 embedders don't need this class, as they can use |
| 493 * Global directly in std containers. | 496 * Global directly in std containers. |
| 494 */ | 497 */ |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 return reinterpret_cast<V*>(v); | 615 return reinterpret_cast<V*>(v); |
| 613 } | 616 } |
| 614 | 617 |
| 615 Isolate* isolate_; | 618 Isolate* isolate_; |
| 616 typename Traits::Impl impl_; | 619 typename Traits::Impl impl_; |
| 617 }; | 620 }; |
| 618 | 621 |
| 619 } // namespace v8 | 622 } // namespace v8 |
| 620 | 623 |
| 621 #endif // V8_UTIL_H | 624 #endif // V8_UTIL_H |
| OLD | NEW |