22 typedef uintptr_t PersistentContainerValue;
23 static const uintptr_t kPersistentContainerNotFound = 0;
24 enum PersistentContainerCallbackType {
28 kWeakWithInternalFields,
29 kWeak = kWeakWithParameter
39 template<
typename K,
typename V>
43 typedef std::map<K, PersistentContainerValue> Impl;
44 typedef typename Impl::iterator Iterator;
46 static bool Empty(Impl* impl) {
return impl->empty(); }
47 static size_t Size(Impl* impl) {
return impl->size(); }
48 static void Swap(Impl& a, Impl& b) { std::swap(a, b); }
49 static Iterator Begin(Impl* impl) {
return impl->begin(); }
50 static Iterator End(Impl* impl) {
return impl->end(); }
51 static K Key(Iterator it) {
return it->first; }
52 static PersistentContainerValue
Value(Iterator it) {
return it->second; }
53 static PersistentContainerValue
Set(Impl* impl, K key,
54 PersistentContainerValue value) {
55 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
56 PersistentContainerValue old_value = kPersistentContainerNotFound;
58 old_value = res.first->second;
59 res.first->second = value;
63 static PersistentContainerValue Get(Impl* impl, K key) {
64 Iterator it = impl->find(key);
65 if (it == impl->end())
return kPersistentContainerNotFound;
68 static PersistentContainerValue Remove(Impl* impl, K key) {
69 Iterator it = impl->find(key);
70 if (it == impl->end())
return kPersistentContainerNotFound;
71 PersistentContainerValue value = it->second;
86 template<
typename K,
typename V>
90 static const PersistentContainerCallbackType kCallbackType = kNotWeak;
93 typedef void WeakCallbackDataType;
95 static WeakCallbackDataType* WeakCallbackParameter(
99 static MapType* MapFromWeakCallbackInfo(
103 static K KeyFromWeakCallbackInfo(
107 static void DisposeCallbackData(WeakCallbackDataType* data) { }
112 template <
typename K,
typename V>
115 template <
typename T>
116 struct RemovePointer;
120 static const PersistentContainerCallbackType kCallbackType = kNotWeak;
122 typedef void WeakCallbackDataType;
124 static WeakCallbackDataType* WeakCallbackParameter(
MapType* map,
const K& key,
128 static MapType* MapFromWeakCallbackInfo(
132 static K KeyFromWeakCallbackInfo(
136 static void DisposeCallbackData(WeakCallbackDataType* data) {}
137 static void OnWeakCallback(
144 template <
typename T>
145 struct RemovePointer<T*> {
161 template <
typename K,
typename V,
typename Traits>
164 Isolate* GetIsolate() {
return isolate_; }
169 size_t Size() {
return Traits::Size(&impl_); }
174 bool IsWeak() {
return Traits::kCallbackType != kNotWeak; }
180 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
187 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
196 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
205 V8::RegisterExternallyReferencedObject(
206 reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))),
207 reinterpret_cast<internal::Isolate*>(GetIsolate()));
214 return Release(Traits::Remove(&impl_, key)).Pass();
222 typedef typename Traits::Iterator It;
225 while (!Traits::Empty(&impl_)) {
226 typename Traits::Impl impl;
227 Traits::Swap(impl_, impl);
228 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
229 Traits::Dispose(isolate_,
Release(Traits::Value(i)).Pass(),
243 : value_(other.value_) { }
248 bool IsEmpty()
const {
249 return value_ == kPersistentContainerNotFound;
253 return SetReturnValueFromVal(&returnValue, value_);
256 value_ = kPersistentContainerNotFound;
259 value_ = other.value_;
270 void operator=(PersistentContainerValue value) {
274 PersistentContainerValue value_;
294 ~PersistentValueMapBase() {
Clear(); }
296 Isolate* isolate() {
return isolate_; }
297 typename Traits::Impl* impl() {
return &impl_; }
299 static V* FromVal(PersistentContainerValue v) {
300 return reinterpret_cast<V*
>(v);
303 static PersistentContainerValue ClearAndLeak(Global<V>* persistent) {
304 V* v = persistent->val_;
305 persistent->val_ = 0;
306 return reinterpret_cast<PersistentContainerValue
>(v);
309 static PersistentContainerValue Leak(Global<V>* persistent) {
310 return reinterpret_cast<PersistentContainerValue
>(persistent->val_);
321 if (Traits::kCallbackType != kNotWeak && p.
IsWeak()) {
322 Traits::DisposeCallbackData(
323 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
328 void RemoveWeak(
const K& key) {
330 p.val_ = FromVal(Traits::Remove(&impl_, key));
335 PersistentValueMapBase(PersistentValueMapBase&);
336 void operator=(PersistentValueMapBase&);
338 static bool SetReturnValueFromVal(ReturnValue<Value>* returnValue,
339 PersistentContainerValue value) {
340 bool hasValue = value != kPersistentContainerNotFound;
342 returnValue->SetInternal(
343 *reinterpret_cast<internal::Object**>(FromVal(value)));
349 typename Traits::Impl impl_;
353 template <
typename K,
typename V,
typename Traits>
369 Global<V> persistent(this->isolate(), value);
385 if (Traits::kCallbackType != kNotWeak) {
386 WeakCallbackType callback_type =
387 Traits::kCallbackType == kWeakWithInternalFields
388 ? WeakCallbackType::kInternalFields
389 : WeakCallbackType::kParameter;
391 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
392 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
395 PersistentContainerValue old_value =
396 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
397 return this->
Release(old_value).Pass();
406 *reference = this->Leak(&value);
411 static void WeakCallback(
413 if (Traits::kCallbackType != kNotWeak) {
415 Traits::MapFromWeakCallbackInfo(data);
416 K key = Traits::KeyFromWeakCallbackInfo(data);
417 Traits::Dispose(data.GetIsolate(),
418 persistentValueMap->
Remove(key).Pass(), key);
419 Traits::DisposeCallbackData(data.GetParameter());
425 template <
typename K,
typename V,
typename Traits>
441 Global<V> persistent(this->isolate(), value);
457 if (Traits::kCallbackType != kNotWeak) {
458 WeakCallbackType callback_type =
459 Traits::kCallbackType == kWeakWithInternalFields
460 ? WeakCallbackType::kInternalFields
461 : WeakCallbackType::kParameter;
463 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
464 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
467 PersistentContainerValue old_value =
468 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
469 return this->
Release(old_value).Pass();
478 *reference = this->Leak(&value);
483 static void OnWeakCallback(
485 if (Traits::kCallbackType != kNotWeak) {
486 auto map = Traits::MapFromWeakCallbackInfo(data);
487 K key = Traits::KeyFromWeakCallbackInfo(data);
488 map->RemoveWeak(key);
489 Traits::OnWeakCallback(data);
490 data.SetSecondPassCallback(SecondWeakCallback);
494 static void SecondWeakCallback(
495 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
496 Traits::DisposeWeak(data);
508 template<
typename K,
typename V,
509 typename Traits = DefaultPersistentValueMapTraits<K, V> >
524 template <
typename K,
typename V,
535 typedef std::vector<PersistentContainerValue> Impl;
537 static void Append(Impl* impl, PersistentContainerValue value) {
538 impl->push_back(value);
540 static bool IsEmpty(
const Impl* impl) {
541 return impl->empty();
543 static size_t Size(
const Impl* impl) {
546 static PersistentContainerValue Get(
const Impl* impl,
size_t i) {
547 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
549 static void ReserveCapacity(Impl* impl,
size_t capacity) {
550 impl->reserve(capacity);
552 static void Clear(Impl* impl) {
568 template<
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
582 Traits::Append(&impl_, ClearAndLeak(&persistent));
589 Traits::Append(&impl_, ClearAndLeak(&persistent));
596 return Traits::IsEmpty(&impl_);
603 return Traits::Size(&impl_);
610 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
617 size_t length = Traits::Size(&impl_);
618 for (
size_t i = 0; i < length; i++) {
620 p.val_ = FromVal(Traits::Get(&impl_, i));
622 Traits::Clear(&impl_);
630 Traits::ReserveCapacity(&impl_, capacity);
634 static PersistentContainerValue ClearAndLeak(
Global<V>* persistent) {
635 V* v = persistent->val_;
636 persistent->val_ = 0;
637 return reinterpret_cast<PersistentContainerValue
>(v);
640 static V* FromVal(PersistentContainerValue v) {
641 return reinterpret_cast<V*
>(v);
645 typename Traits::Impl impl_;
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:448
size_t Size() const
Definition: v8-util.h:602
size_t Size()
Definition: v8-util.h:169
bool IsEmpty() const
Definition: v8-util.h:595
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:404
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:384
V8_INLINE bool IsWeak() const
Definition: v8.h:9684
Definition: v8-util.h:239
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:376
static Global< V > Release(PersistentContainerValue v)
Definition: v8-util.h:318
Definition: v8-util.h:162
void ReserveCapacity(size_t capacity)
Definition: v8-util.h:629
PersistentValueReference GetReference(const K &key)
Definition: v8-util.h:287
Local< V > Get(const K &key)
Definition: v8-util.h:179
Definition: v8-platform.h:16
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:440
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
Definition: v8-util.h:194
Definition: v8-util.h:426
void RegisterExternallyReferencedObject(K &key)
Definition: v8-util.h:203
Local< V > Get(size_t index) const
Definition: v8-util.h:609
Definition: v8-util.h:569
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:368
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8.h:9589
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:476
Definition: v8-util.h:526
void Clear()
Definition: v8-util.h:221
void Append(Local< V > value)
Definition: v8-util.h:580
Definition: v8-util.h:113
Global< V > Remove(const K &key)
Definition: v8-util.h:213
Definition: v8-util.h:533
Definition: v8-util.h:510
bool IsWeak()
Definition: v8-util.h:174
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:456
void Clear()
Definition: v8-util.h:616
V8_INLINE void Reset()
Definition: v8.h:9693
Definition: v8-util.h:354
Global Pass()
Definition: v8.h:926
bool Contains(const K &key)
Definition: v8-util.h:186
void Append(Global< V > persistent)
Definition: v8-util.h:588