| 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" // NOLINT(build/include) | 8 #include "v8.h" // NOLINT(build/include) |
| 9 #include <assert.h> | 9 #include <assert.h> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * Support for Persistent containers. | 14 * Support for Persistent containers. |
| 15 * | 15 * |
| 16 * C++11 embedders can use STL containers with Global values, | 16 * C++11 embedders can use STL containers with Global values, |
| 17 * but pre-C++11 does not support the required move semantic and hence | 17 * but pre-C++11 does not support the required move semantic and hence |
| 18 * may want these container classes. | 18 * may want these container classes. |
| 19 */ | 19 */ |
| 20 namespace v8 { | 20 namespace v8 { |
| 21 | 21 |
| 22 typedef uintptr_t PersistentContainerValue; | 22 typedef uintptr_t PersistentContainerValue; |
| 23 static const uintptr_t kPersistentContainerNotFound = 0; | 23 static const uintptr_t kPersistentContainerNotFound = 0; |
| 24 enum PersistentContainerCallbackType { | 24 enum PersistentContainerCallbackType { |
| 25 kNotWeak, | 25 kNotWeak, |
| 26 // These correspond to v8::WeakCallbackType | 26 // These correspond to v8::WeakCallbackType |
| 27 kWeakWithParameter, | 27 kWeakWithParameter, |
| 28 kWeakWithInternalFields, | 28 kWeakWithEmbedderFields, |
| 29 kWeakWithInternalFields = kWeakWithEmbedderFields, // Deprecate. |
| 29 kWeak = kWeakWithParameter // For backwards compatibility. Deprecate. | 30 kWeak = kWeakWithParameter // For backwards compatibility. Deprecate. |
| 30 }; | 31 }; |
| 31 | 32 |
| 32 | |
| 33 /** | 33 /** |
| 34 * A default trait implemenation for PersistentValueMap which uses std::map | 34 * A default trait implemenation for PersistentValueMap which uses std::map |
| 35 * as a backing map. | 35 * as a backing map. |
| 36 * | 36 * |
| 37 * Users will have to implement their own weak callbacks & dispose traits. | 37 * Users will have to implement their own weak callbacks & dispose traits. |
| 38 */ | 38 */ |
| 39 template<typename K, typename V> | 39 template<typename K, typename V> |
| 40 class StdMapTraits { | 40 class StdMapTraits { |
| 41 public: | 41 public: |
| 42 // STL map & related: | 42 // STL map & related: |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 } | 455 } |
| 456 | 456 |
| 457 /** | 457 /** |
| 458 * Put the value into the map, and set the 'weak' callback when demanded | 458 * Put the value into the map, and set the 'weak' callback when demanded |
| 459 * by the Traits class. | 459 * by the Traits class. |
| 460 */ | 460 */ |
| 461 Global<V> SetUnique(const K& key, Global<V>* persistent) { | 461 Global<V> SetUnique(const K& key, Global<V>* persistent) { |
| 462 if (Traits::kCallbackType != kNotWeak) { | 462 if (Traits::kCallbackType != kNotWeak) { |
| 463 WeakCallbackType callback_type = | 463 WeakCallbackType callback_type = |
| 464 Traits::kCallbackType == kWeakWithInternalFields | 464 Traits::kCallbackType == kWeakWithInternalFields |
| 465 ? WeakCallbackType::kInternalFields | 465 ? WeakCallbackType::kEmbedderFields |
| 466 : WeakCallbackType::kParameter; | 466 : WeakCallbackType::kParameter; |
| 467 Local<V> value(Local<V>::New(this->isolate(), *persistent)); | 467 Local<V> value(Local<V>::New(this->isolate(), *persistent)); |
| 468 persistent->template SetWeak<typename Traits::WeakCallbackDataType>( | 468 persistent->template SetWeak<typename Traits::WeakCallbackDataType>( |
| 469 Traits::WeakCallbackParameter(this, key, value), OnWeakCallback, | 469 Traits::WeakCallbackParameter(this, key, value), OnWeakCallback, |
| 470 callback_type); | 470 callback_type); |
| 471 } | 471 } |
| 472 PersistentContainerValue old_value = | 472 PersistentContainerValue old_value = |
| 473 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent)); | 473 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent)); |
| 474 return this->Release(old_value).Pass(); | 474 return this->Release(old_value).Pass(); |
| 475 } | 475 } |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 return reinterpret_cast<V*>(v); | 646 return reinterpret_cast<V*>(v); |
| 647 } | 647 } |
| 648 | 648 |
| 649 Isolate* isolate_; | 649 Isolate* isolate_; |
| 650 typename Traits::Impl impl_; | 650 typename Traits::Impl impl_; |
| 651 }; | 651 }; |
| 652 | 652 |
| 653 } // namespace v8 | 653 } // namespace v8 |
| 654 | 654 |
| 655 #endif // V8_UTIL_H | 655 #endif // V8_UTIL_H |
| OLD | NEW |