25template <
typename K,
typename V,
typename Traits>
29static const uintptr_t kPersistentContainerNotFound = 0;
43template<
typename K,
typename V>
47 typedef std::map<K, PersistentContainerValue>
Impl;
50 static bool Empty(
Impl* impl) {
return impl->empty(); }
51 static size_t Size(
Impl* impl) {
return impl->size(); }
59 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
62 old_value = res.first->second;
63 res.first->second = value;
69 if (it == impl->end())
return kPersistentContainerNotFound;
74 if (it == impl->end())
return kPersistentContainerNotFound;
90template<
typename K,
typename V>
116template <
typename K,
typename V>
119 template <
typename T>
120 struct RemovePointer;
148 template <
typename T>
149 struct RemovePointer<T*> {
165template <
typename K,
typename V,
typename Traits>
173 size_t Size() {
return Traits::Size(&impl_); }
184 V* p =
FromVal(Traits::Get(&impl_, key));
185#ifdef V8_ENABLE_DIRECT_LOCAL
186 if (p ==
nullptr)
return Local<V>();
195 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
204 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
211 return Release(Traits::Remove(&impl_, key)).Pass();
219 typedef typename Traits::Iterator It;
222 while (!Traits::Empty(&impl_)) {
223 typename Traits::Impl
impl;
224 Traits::Swap(impl_,
impl);
225 for (It i = Traits::Begin(&
impl); i != Traits::End(&
impl); ++i) {
226 Traits::Dispose(isolate_,
Release(Traits::Value(i)).Pass(),
240 : value_(other.value_) { }
244 internal::ValueHelper::SlotAsValue<V>(
248 return value_ == kPersistentContainerNotFound;
252 return SetReturnValueFromVal(&returnValue, value_);
255 value_ = kPersistentContainerNotFound;
258 value_ = other.value_;
292 : isolate_(
isolate), label_(nullptr) {}
294 : isolate_(
isolate), label_(label) {}
299 typename Traits::Impl*
impl() {
return &impl_; }
302 return internal::ValueHelper::SlotAsValue<V>(
325 Traits::DisposeCallbackData(
326 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
348 bool hasValue = value != kPersistentContainerNotFound;
356 typename Traits::Impl impl_;
360template <
typename K,
typename V,
typename Traits>
394 if (Traits::kCallbackType ==
kNotWeak) {
402 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
403 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
408 return this->
Release(old_value).Pass();
417 *reference = this->
Leak(&value);
422 static void WeakCallback(
424 if (Traits::kCallbackType !=
kNotWeak) {
426 Traits::MapFromWeakCallbackInfo(data);
427 K key = Traits::KeyFromWeakCallbackInfo(data);
429 persistentValueMap->
Remove(key).Pass(), key);
436template <
typename K,
typename V,
typename Traits>
470 if (Traits::kCallbackType ==
kNotWeak) {
478 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
479 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
484 return this->
Release(old_value).Pass();
493 *reference = this->
Leak(&value);
498 static void OnWeakCallback(
500 if (Traits::kCallbackType !=
kNotWeak) {
501 auto map = Traits::MapFromWeakCallbackInfo(data);
502 K key = Traits::KeyFromWeakCallbackInfo(data);
503 map->RemoveWeak(key);
504 Traits::OnWeakCallback(data);
509 static void SecondWeakCallback(
510 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
511 Traits::DisposeWeak(data);
523template<
typename K,
typename V,
524 typename Traits = DefaultPersistentValueMapTraits<K, V> >
539template <
typename K,
typename V,
540 typename Traits = DefaultGlobalMapTraits<K, V> >
549 typedef std::vector<PersistentContainerValue>
Impl;
552 impl->push_back(value);
555 return impl->empty();
561 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
564 impl->reserve(capacity);
581template <
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
584 explicit PersistentValueVector(Isolate* isolate) : isolate_(isolate) { }
586 ~PersistentValueVector() {
593 void Append(Local<V> value) {
594 Global<V> persistent(isolate_, value);
595 Traits::Append(&impl_, ClearAndLeak(&persistent));
601 void Append(Global<V> persistent) {
602 Traits::Append(&impl_, ClearAndLeak(&persistent));
608 bool IsEmpty()
const {
609 return Traits::IsEmpty(&impl_);
615 size_t Size()
const {
616 return Traits::Size(&impl_);
622 Local<V> Get(
size_t index)
const {
623 return Local<V>::New(isolate_, internal::ValueHelper::SlotAsValue<V>(
624 Traits::Get(&impl_, index)));
631 size_t length = Traits::Size(&impl_);
632 for (
size_t i = 0; i < length; i++) {
636 Traits::Clear(&impl_);
643 void ReserveCapacity(
size_t capacity) {
644 Traits::ReserveCapacity(&impl_, capacity);
649 auto slot = persistent->slot();
655 return internal::ValueHelper::SlotAsValue<V>(
660 typename Traits::Impl impl_;
static const PersistentContainerCallbackType kCallbackType
static void Dispose(Isolate *isolate, Global< V > value, K key)
static void DisposeWeak(const WeakCallbackInfo< WeakCallbackDataType > &data)
void WeakCallbackDataType
static void OnWeakCallback(const WeakCallbackInfo< WeakCallbackDataType > &data)
GlobalValueMap< K, V, DefaultGlobalMapTraits< K, V > > MapType
static void DisposeCallbackData(WeakCallbackDataType *data)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static const PersistentContainerCallbackType kCallbackType
static void Dispose(Isolate *isolate, Global< V > value, K key)
PersistentValueMap< K, V, DefaultPersistentValueMapTraits< K, V > > MapType
void WeakCallbackDataType
static void DisposeCallbackData(WeakCallbackDataType *data)
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
static void ReserveCapacity(Impl *impl, size_t capacity)
static bool IsEmpty(const Impl *impl)
static PersistentContainerValue Get(const Impl *impl, size_t i)
static size_t Size(const Impl *impl)
static void Append(Impl *impl, PersistentContainerValue value)
static void Clear(Impl *impl)
std::vector< PersistentContainerValue > Impl
GlobalValueMap(Isolate *isolate)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > Set(const K &key, Local< V > value)
GlobalValueMap(Isolate *isolate, const char *label)
Global< V > Set(const K &key, Global< V > value)
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Global< V > SetUnique(const K &key, Global< V > *persistent)
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
V8_INLINE bool IsWeak() const
V8_INLINE void AnnotateStrongRetainer(const char *label)
Local< V > NewLocal(Isolate *isolate) const
bool SetReturnValue(ReturnValue< T > returnValue)
void operator=(const PersistentValueReference &other)
PersistentValueReference(const PersistentValueReference &other)
PersistentValueReference()
static Global< V > Release(PersistentContainerValue v)
static PersistentContainerValue Leak(Global< V > *persistent)
~PersistentValueMapBase()
Global< V > Remove(const K &key)
PersistentValueMapBase(Isolate *isolate, const char *label)
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
void RemoveWeak(const K &key)
static V * FromVal(PersistentContainerValue v)
PersistentValueReference GetReference(const K &key)
bool Contains(const K &key)
static PersistentContainerValue ClearAndLeak(Global< V > *persistent)
PersistentValueMapBase(Isolate *isolate)
void AnnotateStrongRetainer(Global< V > *persistent)
Local< V > Get(const K &key)
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Global< V > Set(const K &key, Local< V > value)
PersistentValueMap(Isolate *isolate, const char *label)
PersistentValueMap(Isolate *isolate)
Global< V > Set(const K &key, Global< V > value)
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Global< V > SetUnique(const K &key, Global< V > *persistent)
StdGlobalValueMap(Isolate *isolate)
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
static Iterator End(Impl *impl)
static PersistentContainerValue Value(Iterator it)
static void Swap(Impl &a, Impl &b)
static K Key(Iterator it)
static PersistentContainerValue Remove(Impl *impl, K key)
static PersistentContainerValue Get(Impl *impl, K key)
static size_t Size(Impl *impl)
static bool Empty(Impl *impl)
std::map< K, PersistentContainerValue > Impl
static Iterator Begin(Impl *impl)
StdPersistentValueMap(Isolate *isolate)
V8_INLINE T * GetParameter() const
void SetSecondPassCallback(Callback callback) const
V8_INLINE Isolate * GetIsolate() const
V8_INLINE internal::Address *const & slot() const
PersistentContainerCallbackType
@ kWeakWithInternalFields
uintptr_t PersistentContainerValue