23 static const uintptr_t kPersistentContainerNotFound = 0;
37 template<
typename K,
typename V>
41 typedef std::map<K, PersistentContainerValue>
Impl;
44 static bool Empty(
Impl* impl) {
return impl->empty(); }
45 static size_t Size(
Impl* impl) {
return impl->size(); }
53 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
56 old_value = res.first->second;
57 res.first->second = value;
63 if (it == impl->end())
return kPersistentContainerNotFound;
68 if (it == impl->end())
return kPersistentContainerNotFound;
84 template<
typename K,
typename V>
110 template <
typename K,
typename V>
113 template <
typename T>
114 struct RemovePointer;
142 template <
typename T>
143 struct RemovePointer<T*> {
159 template <
typename K,
typename V,
typename Traits>
167 size_t Size() {
return Traits::Size(&impl_); }
185 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
194 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
202 "Used TracedGlobal and EmbedderHeapTracer::RegisterEmbedderReference",
203 inline void RegisterExternallyReferencedObject(K& key));
209 return Release(Traits::Remove(&impl_, key)).Pass();
217 typedef typename Traits::Iterator It;
220 while (!Traits::Empty(&impl_)) {
221 typename Traits::Impl
impl;
222 Traits::Swap(impl_,
impl);
223 for (It i = Traits::Begin(&
impl); i != Traits::End(&
impl); ++i) {
224 Traits::Dispose(isolate_,
Release(Traits::Value(i)).Pass(),
238 : value_(other.value_) { }
244 return value_ == kPersistentContainerNotFound;
248 return SetReturnValueFromVal(&returnValue, value_);
251 value_ = kPersistentContainerNotFound;
254 value_ = other.value_;
288 : isolate_(
isolate), label_(nullptr) {}
290 : isolate_(
isolate), label_(label) {}
295 typename Traits::Impl*
impl() {
return &impl_; }
298 return reinterpret_cast<V*>(v);
302 V* v = persistent->val_;
303 persistent->val_ =
nullptr;
304 return reinterpret_cast<PersistentContainerValue>(v);
308 return reinterpret_cast<PersistentContainerValue>(persistent->val_);
320 Traits::DisposeCallbackData(
321 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
328 p.val_ =
FromVal(Traits::Remove(&impl_, key));
342 bool hasValue = value != kPersistentContainerNotFound;
344 returnValue->SetInternal(
345 *reinterpret_cast<internal::Address*>(
FromVal(value)));
351 typename Traits::Impl impl_;
355 template <
typename K,
typename V,
typename Traits>
357 PersistentValueMapBase<K, V, Traits>::RegisterExternallyReferencedObject(
359 assert(Contains(key));
360 V8::RegisterExternallyReferencedObject(
361 reinterpret_cast<internal::Address*>(FromVal(Traits::Get(&impl_, key))),
362 reinterpret_cast<internal::Isolate*>(GetIsolate()));
365 template <
typename K,
typename V,
typename Traits>
399 if (Traits::kCallbackType ==
kNotWeak) {
407 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
408 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
413 return this->
Release(old_value).Pass();
422 *reference = this->
Leak(&value);
427 static void WeakCallback(
429 if (Traits::kCallbackType !=
kNotWeak) {
431 Traits::MapFromWeakCallbackInfo(data);
432 K key = Traits::KeyFromWeakCallbackInfo(data);
434 persistentValueMap->
Remove(key).Pass(), key);
441 template <
typename K,
typename V,
typename Traits>
475 if (Traits::kCallbackType ==
kNotWeak) {
483 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
484 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
489 return this->
Release(old_value).Pass();
498 *reference = this->
Leak(&value);
503 static void OnWeakCallback(
505 if (Traits::kCallbackType !=
kNotWeak) {
506 auto map = Traits::MapFromWeakCallbackInfo(data);
507 K key = Traits::KeyFromWeakCallbackInfo(data);
508 map->RemoveWeak(key);
509 Traits::OnWeakCallback(data);
514 static void SecondWeakCallback(
515 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
516 Traits::DisposeWeak(data);
528 template<
typename K,
typename V,
529 typename Traits = DefaultPersistentValueMapTraits<K, V> >
544 template <
typename K,
typename V,
545 typename Traits = DefaultGlobalMapTraits<K, V> >
555 typedef std::vector<PersistentContainerValue>
Impl;
558 impl->push_back(value);
561 return impl->empty();
567 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
570 impl->reserve(capacity);
588 template<
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
602 Traits::Append(&impl_, ClearAndLeak(&persistent));
609 Traits::Append(&impl_, ClearAndLeak(&persistent));
616 return Traits::IsEmpty(&impl_);
623 return Traits::Size(&impl_);
630 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
637 size_t length = Traits::Size(&impl_);
638 for (
size_t i = 0; i < length; i++) {
640 p.val_ = FromVal(Traits::Get(&impl_, i));
642 Traits::Clear(&impl_);
650 Traits::ReserveCapacity(&impl_, capacity);
655 V* v = persistent->val_;
656 persistent->val_ =
nullptr;
657 return reinterpret_cast<PersistentContainerValue>(v);
661 return reinterpret_cast<V*>(v);
665 typename Traits::Impl impl_;
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Global< V > Set(const K &key, Global< V > value)
static void Clear(Impl *impl)
PersistentValueVector(Isolate *isolate)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
~PersistentValueMapBase()
static size_t Size(const Impl *impl)
Global< V > SetUnique(const K &key, Global< V > *persistent)
static void OnWeakCallback(const WeakCallbackInfo< WeakCallbackDataType > &data)
V8_INLINE T * GetParameter() const
Global< V > Set(const K &key, Local< V > value)
StdGlobalValueMap(Isolate *isolate)
static PersistentContainerValue Remove(Impl *impl, K key)
static PersistentContainerValue Leak(Global< V > *persistent)
static PersistentContainerValue ClearAndLeak(Global< V > *persistent)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > SetUnique(const K &key, Global< V > *persistent)
PersistentValueReference GetReference(const K &key)
static size_t Size(Impl *impl)
static bool IsEmpty(const Impl *impl)
static const PersistentContainerCallbackType kCallbackType
static void Append(Impl *impl, PersistentContainerValue value)
void ReserveCapacity(size_t capacity)
std::vector< PersistentContainerValue > Impl
static void ReserveCapacity(Impl *impl, size_t capacity)
static void DisposeWeak(const WeakCallbackInfo< WeakCallbackDataType > &data)
static V * FromVal(PersistentContainerValue v)
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static void Dispose(Isolate *isolate, Global< V > value, K key)
Global< V > Remove(const K &key)
static Global< V > Release(PersistentContainerValue v)
V8_INLINE void AnnotateStrongRetainer(const char *label)
bool Contains(const K &key)
PersistentValueMap(Isolate *isolate)
GlobalValueMap< K, V, DefaultGlobalMapTraits< K, V > > MapType
PersistentContainerCallbackType
void WeakCallbackDataType
PersistentValueMapBase(Isolate *isolate, const char *label)
static bool Empty(Impl *impl)
static PersistentContainerValue Get(const Impl *impl, size_t i)
uintptr_t PersistentContainerValue
static const PersistentContainerCallbackType kCallbackType
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
GlobalValueMap(Isolate *isolate, const char *label)
static K Key(Iterator it)
bool SetReturnValue(ReturnValue< T > returnValue)
static Iterator Begin(Impl *impl)
V8_INLINE Isolate * GetIsolate() const
static Iterator End(Impl *impl)
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
void AnnotateStrongRetainer(Global< V > *persistent)
static void Dispose(Isolate *isolate, Global< V > value, K key)
Local< V > Get(size_t index) const
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
V8_INLINE bool IsWeak() const
PersistentValueReference(const PersistentValueReference &other)
StdPersistentValueMap(Isolate *isolate)
Local< V > Get(const K &key)
V8_DEPRECATE_SOON("Used TracedGlobal and EmbedderHeapTracer::RegisterEmbedderReference", inline void RegisterExternallyReferencedObject(K &key))
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
void SetSecondPassCallback(Callback callback) const
Global< V > Set(const K &key, Local< V > value)
static void DisposeCallbackData(WeakCallbackDataType *data)
static PersistentContainerValue Value(Iterator it)
void WeakCallbackDataType
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
void operator=(const PersistentValueReference &other)
Local< V > NewLocal(Isolate *isolate) const
void RemoveWeak(const K &key)
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
PersistentValueMapBase(Isolate *isolate)
std::map< K, PersistentContainerValue > Impl
static void Swap(Impl &a, Impl &b)
void Append(Local< V > value)
GlobalValueMap(Isolate *isolate)
PersistentValueMap< K, V, DefaultPersistentValueMapTraits< K, V > > MapType
void Append(Global< V > persistent)
PersistentValueReference()
PersistentValueMap(Isolate *isolate, const char *label)
Global< V > Set(const K &key, Global< V > value)
static PersistentContainerValue Get(Impl *impl, K key)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static void DisposeCallbackData(WeakCallbackDataType *data)