21 typedef uintptr_t PersistentContainerValue;
22 static const uintptr_t kPersistentContainerNotFound = 0;
23 enum PersistentContainerCallbackType {
27 kWeakWithInternalFields,
28 kWeak = kWeakWithParameter
38 template<
typename K,
typename V>
42 typedef std::map<K, PersistentContainerValue> Impl;
43 typedef typename Impl::iterator Iterator;
45 static bool Empty(Impl* impl) {
return impl->empty(); }
46 static size_t Size(Impl* impl) {
return impl->size(); }
47 static void Swap(Impl& a, Impl& b) { std::swap(a, b); }
48 static Iterator Begin(Impl* impl) {
return impl->begin(); }
49 static Iterator End(Impl* impl) {
return impl->end(); }
50 static K Key(Iterator it) {
return it->first; }
51 static PersistentContainerValue
Value(Iterator it) {
return it->second; }
52 static PersistentContainerValue Set(Impl* impl, K key,
53 PersistentContainerValue value) {
54 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
55 PersistentContainerValue old_value = kPersistentContainerNotFound;
57 old_value = res.first->second;
58 res.first->second = value;
62 static PersistentContainerValue Get(Impl* impl, K key) {
63 Iterator it = impl->find(key);
64 if (it == impl->end())
return kPersistentContainerNotFound;
67 static PersistentContainerValue Remove(Impl* impl, K key) {
68 Iterator it = impl->find(key);
69 if (it == impl->end())
return kPersistentContainerNotFound;
70 PersistentContainerValue value = it->second;
85 template<
typename K,
typename V>
89 static const PersistentContainerCallbackType kCallbackType = kNotWeak;
92 typedef void WeakCallbackDataType;
94 static WeakCallbackDataType* WeakCallbackParameter(
95 MapType* map,
const K& key,
Local<V> value) {
98 static MapType* MapFromWeakCallbackData(
102 static K KeyFromWeakCallbackData(
106 static void DisposeCallbackData(WeakCallbackDataType* data) { }
111 template <
typename K,
typename V>
114 template <
typename T>
115 struct RemovePointer;
119 static const PersistentContainerCallbackType kCallbackType = kNotWeak;
121 typedef void WeakCallbackDataType;
123 static WeakCallbackDataType* WeakCallbackParameter(MapType* map,
const K& key,
127 static MapType* MapFromWeakCallbackInfo(
131 static K KeyFromWeakCallbackInfo(
135 static void DisposeCallbackData(WeakCallbackDataType* data) {}
141 template <
typename T>
142 struct RemovePointer<T*> {
158 template <
typename K,
typename V,
typename Traits>
161 Isolate* GetIsolate() {
return isolate_; }
166 size_t Size() {
return Traits::Size(&impl_); }
171 bool IsWeak() {
return Traits::kCallbackType != kNotWeak; }
177 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
184 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
193 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
202 reinterpret_cast<internal::Object**>(parent.val_),
203 reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))));
210 return Release(Traits::Remove(&impl_, key)).Pass();
218 typedef typename Traits::Iterator It;
221 while (!Traits::Empty(&impl_)) {
222 typename Traits::Impl impl;
223 Traits::Swap(impl_, impl);
224 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
225 Traits::Dispose(isolate_,
Release(Traits::Value(i)).Pass(),
239 : value_(other.value_) { }
244 bool IsEmpty()
const {
245 return value_ == kPersistentContainerNotFound;
249 return SetReturnValueFromVal(&returnValue, value_);
252 value_ = kPersistentContainerNotFound;
255 value_ = other.value_;
266 void operator=(PersistentContainerValue value) {
270 PersistentContainerValue value_;
290 ~PersistentValueMapBase() {
Clear(); }
292 Isolate* isolate() {
return isolate_; }
293 typename Traits::Impl* impl() {
return &impl_; }
295 static V* FromVal(PersistentContainerValue v) {
296 return reinterpret_cast<V*
>(v);
299 static PersistentContainerValue ClearAndLeak(Global<V>* persistent) {
300 V* v = persistent->val_;
301 persistent->val_ = 0;
302 return reinterpret_cast<PersistentContainerValue
>(v);
305 static PersistentContainerValue Leak(Global<V>* persistent) {
306 return reinterpret_cast<PersistentContainerValue
>(persistent->val_);
317 if (Traits::kCallbackType != kNotWeak && p.
IsWeak()) {
318 Traits::DisposeCallbackData(
319 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
324 void RemoveWeak(
const K& key) {
326 p.val_ = FromVal(Traits::Remove(&impl_, key));
331 PersistentValueMapBase(PersistentValueMapBase&);
332 void operator=(PersistentValueMapBase&);
334 static bool SetReturnValueFromVal(ReturnValue<Value>* returnValue,
335 PersistentContainerValue value) {
336 bool hasValue = value != kPersistentContainerNotFound;
338 returnValue->SetInternal(
339 *reinterpret_cast<internal::Object**>(FromVal(value)));
345 typename Traits::Impl impl_;
349 template <
typename K,
typename V,
typename Traits>
365 Global<V> persistent(this->isolate(), value);
381 if (Traits::kCallbackType != kNotWeak) {
383 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
384 Traits::WeakCallbackParameter(
this, key, value), WeakCallback);
386 PersistentContainerValue old_value =
387 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
388 return this->
Release(old_value).Pass();
396 PersistentValueReference* reference) {
397 *reference = this->Leak(&value);
402 static void WeakCallback(
404 if (Traits::kCallbackType != kNotWeak) {
406 Traits::MapFromWeakCallbackData(data);
407 K key = Traits::KeyFromWeakCallbackData(data);
408 Traits::Dispose(data.GetIsolate(),
409 persistentValueMap->
Remove(key).Pass(), key);
410 Traits::DisposeCallbackData(data.GetParameter());
416 template <
typename K,
typename V,
typename Traits>
424 PersistentValueReference;
432 Global<V> persistent(this->isolate(), value);
448 if (Traits::kCallbackType != kNotWeak) {
449 WeakCallbackType callback_type =
450 Traits::kCallbackType == kWeakWithInternalFields
451 ? WeakCallbackType::kInternalFields
452 : WeakCallbackType::kParameter;
454 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
455 Traits::WeakCallbackParameter(
this, key, value), FirstWeakCallback,
458 PersistentContainerValue old_value =
459 Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
460 return this->
Release(old_value).Pass();
468 PersistentValueReference* reference) {
469 *reference = this->Leak(&value);
474 static void FirstWeakCallback(
476 if (Traits::kCallbackType != kNotWeak) {
477 auto map = Traits::MapFromWeakCallbackInfo(data);
478 K key = Traits::KeyFromWeakCallbackInfo(data);
479 map->RemoveWeak(key);
480 data.SetSecondPassCallback(SecondWeakCallback);
484 static void SecondWeakCallback(
485 const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
486 Traits::DisposeWeak(data);
498 template<
typename K,
typename V,
499 typename Traits = DefaultPersistentValueMapTraits<K, V> >
514 template <
typename K,
typename V,
525 typedef std::vector<PersistentContainerValue> Impl;
527 static void Append(Impl* impl, PersistentContainerValue value) {
528 impl->push_back(value);
530 static bool IsEmpty(
const Impl* impl) {
531 return impl->empty();
533 static size_t Size(
const Impl* impl) {
536 static PersistentContainerValue Get(
const Impl* impl,
size_t i) {
537 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
539 static void ReserveCapacity(Impl* impl,
size_t capacity) {
540 impl->reserve(capacity);
542 static void Clear(Impl* impl) {
558 template<
typename V,
typename Traits = DefaultPersistentValueVectorTraits>
572 Traits::Append(&impl_, ClearAndLeak(&persistent));
579 Traits::Append(&impl_, ClearAndLeak(&persistent));
586 return Traits::IsEmpty(&impl_);
593 return Traits::Size(&impl_);
600 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
607 size_t length = Traits::Size(&impl_);
608 for (
size_t i = 0; i < length; i++) {
610 p.val_ = FromVal(Traits::Get(&impl_, i));
612 Traits::Clear(&impl_);
620 Traits::ReserveCapacity(&impl_, capacity);
624 static PersistentContainerValue ClearAndLeak(
Global<V>* persistent) {
625 V* v = persistent->val_;
626 persistent->val_ = 0;
627 return reinterpret_cast<PersistentContainerValue
>(v);
630 static V* FromVal(PersistentContainerValue v) {
631 return reinterpret_cast<V*
>(v);
635 typename Traits::Impl impl_;
bool IsWeak()
Definition: v8-util.h:171
V8_INLINE void Reset()
Definition: v8.h:6934
bool IsEmpty() const
Definition: v8-util.h:585
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:439
void Clear()
Definition: v8-util.h:606
Definition: v8-util.h:417
size_t Size()
Definition: v8-util.h:166
Local< V > Get(size_t index) const
Definition: v8-util.h:599
Definition: v8-util.h:516
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:364
void ReserveCapacity(size_t capacity)
Definition: v8-util.h:619
void Append(Global< V > persistent)
Definition: v8-util.h:578
Global< V > Remove(const K &key)
Definition: v8-util.h:209
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:380
Definition: v8-util.h:235
bool Contains(const K &key)
Definition: v8-util.h:183
size_t Size() const
Definition: v8-util.h:592
static Global< V > Release(PersistentContainerValue v)
Definition: v8-util.h:314
Definition: libplatform.h:10
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:372
void Append(Local< V > value)
Definition: v8-util.h:570
void SetReference(const Persistent< T > &parent, const Persistent< S > &child)
Definition: v8.h:7866
Definition: v8-util.h:112
Definition: v8-util.h:500
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:467
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:447
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8.h:6830
Global Pass()
Definition: v8.h:830
Definition: v8-util.h:159
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:395
void Clear()
Definition: v8-util.h:217
Definition: v8-util.h:559
PersistentValueReference GetReference(const K &key)
Definition: v8-util.h:283
void SetReference(const K &key, const Persistent< Object > &parent)
Definition: v8-util.h:199
Local< V > Get(const K &key)
Definition: v8-util.h:176
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:431
Definition: v8-util.h:523
Definition: v8-util.h:350
V8_INLINE bool IsWeak() const
Definition: v8.h:6925
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
Definition: v8-util.h:191