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_CONSERVATIVE_STACK_SCANNING
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_) { }
247 return value_ == kPersistentContainerNotFound;
251 return SetReturnValueFromVal(&returnValue, value_);
254 value_ = kPersistentContainerNotFound;
257 value_ = other.value_;
291 : isolate_(
isolate), label_(nullptr) {}
293 : isolate_(
isolate), label_(label) {}
298 typename Traits::Impl*
impl() {
return &impl_; }
301 return reinterpret_cast<V*
>(v);
305 V* v = persistent->val_;
306 persistent->val_ =
nullptr;
323 Traits::DisposeCallbackData(
324 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
331 p.val_ =
FromVal(Traits::Remove(&impl_, key));
345 bool hasValue = value != kPersistentContainerNotFound;
347 returnValue->SetInternal(
354 typename Traits::Impl impl_;
358template <
typename K,
typename V,
typename Traits>
392 if (Traits::kCallbackType ==
kNotWeak) {
400 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
401 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
406 return this->
Release(old_value).Pass();
415 *reference = this->
Leak(&value);
420 static void WeakCallback(
422 if (Traits::kCallbackType !=
kNotWeak) {
424 Traits::MapFromWeakCallbackInfo(data);
425 K key = Traits::KeyFromWeakCallbackInfo(data);
427 persistentValueMap->
Remove(key).Pass(), key);
434template <
typename K,
typename V,
typename Traits>
468 if (Traits::kCallbackType ==
kNotWeak) {
476 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
477 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
482 return this->
Release(old_value).Pass();
491 *reference = this->
Leak(&value);
496 static void OnWeakCallback(
498 if (Traits::kCallbackType !=
kNotWeak) {
499 auto map = Traits::MapFromWeakCallbackInfo(data);
500 K key = Traits::KeyFromWeakCallbackInfo(data);
501 map->RemoveWeak(key);
502 Traits::OnWeakCallback(data);
507 static void SecondWeakCallback(
508 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
509 Traits::DisposeWeak(data);
521template<
typename K,
typename V,
522 typename Traits = DefaultPersistentValueMapTraits<K, V> >
537template <
typename K,
typename V,
538 typename Traits = DefaultGlobalMapTraits<K, V> >
547 typedef std::vector<PersistentContainerValue>
Impl;
550 impl->push_back(value);
553 return impl->empty();
559 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
562 impl->reserve(capacity);
579template <
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
582 explicit PersistentValueVector(Isolate* isolate) : isolate_(isolate) { }
584 ~PersistentValueVector() {
591 void Append(Local<V> value) {
592 Global<V> persistent(isolate_, value);
593 Traits::Append(&impl_, ClearAndLeak(&persistent));
599 void Append(Global<V> persistent) {
600 Traits::Append(&impl_, ClearAndLeak(&persistent));
606 bool IsEmpty()
const {
607 return Traits::IsEmpty(&impl_);
613 size_t Size()
const {
614 return Traits::Size(&impl_);
620 Local<V> Get(
size_t index)
const {
621 return Local<V>::New(isolate_, internal::ValueHelper::SlotAsValue<V>(
622 FromVal(Traits::Get(&impl_, index))));
629 size_t length = Traits::Size(&impl_);
630 for (
size_t i = 0; i < length; i++) {
632 p.val_ = FromVal(Traits::Get(&impl_, i));
634 Traits::Clear(&impl_);
641 void ReserveCapacity(
size_t capacity) {
642 Traits::ReserveCapacity(&impl_, capacity);
647 V* v = persistent->val_;
648 persistent->val_ =
nullptr;
653 return reinterpret_cast<V*
>(v);
657 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
PersistentContainerCallbackType
@ kWeakWithInternalFields
uintptr_t PersistentContainerValue
#define V8_DEPRECATE_SOON(message)