17
18
19
20
21
22
25template <
typename K,
typename V,
typename Traits>
29static const uintptr_t kPersistentContainerNotFound = 0;
38
39
40
41
42
43template<
typename K,
typename V>
50 static bool Empty(
Impl* impl) {
return impl->empty(); }
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;
83
84
85
86
87
88
89
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*> {
156
157
158
159
160
161
162
163
164
165template <
typename K,
typename V,
typename Traits>
171
172
176
177
181
182
184 V* p =
FromVal(Traits::Get(&impl_, key)
);
185#ifdef V8_ENABLE_DIRECT_HANDLE
186 if (p ==
nullptr)
return Local<V>();
188 return Local<V>::New(isolate_, p);
192
193
195 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
199
200
201
204 return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
208
209
211 return Release(Traits::Remove(&impl_, key)
).Pass();
215
216
217
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(),
233
234
235
240 : value_(other.value_) { }
243 return Local<V>::New(isolate,
244 internal::ValueHelper::SlotAsValue<V>(
248 return value_ == kPersistentContainerNotFound;
252 return SetReturnValueFromVal(&returnValue, value_);
255 value_ = kPersistentContainerNotFound;
258 value_ = other.value_;
263 friend class PersistentValueMap<K, V, Traits>;
264 friend class GlobalValueMap<K, V, Traits>;
277
278
279
280
281
282
283
284
285
292 : isolate_(isolate), label_(
nullptr) {}
294 : isolate_(isolate), label_(label) {}
302 return internal::ValueHelper::SlotAsValue<V>(
317
318
319
320
324 if (Traits::kCallbackType !=
kNotWeak && p.IsWeak()) {
325 Traits::DisposeCallbackData(
326 p.
template ClearWeak<
typename Traits::WeakCallbackDataType>());
339 persistent->AnnotateStrongRetainer(label_);
348 bool hasValue = value != kPersistentContainerNotFound;
350 returnValue->SetInternal(*
reinterpret_cast<
internal::
Address*>(value));
356 typename Traits::Impl impl_;
360template <
typename K,
typename V,
typename Traits>
373
374
375
376
378 Global<V> persistent(
this->isolate(), value);
383
384
390
391
392
394 if (Traits::kCallbackType ==
kNotWeak) {
395 this->AnnotateStrongRetainer(persistent);
401 auto value =
Local<V>::New(
this->isolate(), *persistent);
402 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
403 Traits::WeakCallbackParameter(
this, key, value), WeakCallback,
407 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
408 return this->Release(old_value).Pass();
412
413
414
417 *reference =
this->Leak(&value);
422 static void WeakCallback(
424 if (Traits::kCallbackType !=
kNotWeak) {
425 PersistentValueMap<K, V, Traits>* persistentValueMap =
426 Traits::MapFromWeakCallbackInfo(data);
427 K key = Traits::KeyFromWeakCallbackInfo(data);
428 Traits::Dispose(data.GetIsolate(),
429 persistentValueMap->Remove(key).Pass(), key);
430 Traits::DisposeCallbackData(data.GetParameter());
436template <
typename K,
typename V,
typename Traits>
449
450
451
452
454 Global<V> persistent(
this->isolate(), value);
459
460
466
467
468
470 if (Traits::kCallbackType ==
kNotWeak) {
471 this->AnnotateStrongRetainer(persistent);
477 auto value =
Local<V>::New(
this->isolate(), *persistent);
478 persistent->
template SetWeak<
typename Traits::WeakCallbackDataType>(
479 Traits::WeakCallbackParameter(
this, key, value), OnWeakCallback,
483 Traits::Set(
this->impl(), key,
this->ClearAndLeak(persistent));
484 return this->Release(old_value).Pass();
488
489
490
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);
505 data.SetSecondPassCallback(SecondWeakCallback);
509 static void SecondWeakCallback(
511 Traits::DisposeWeak(data);
517
518
519
520
521
522
523template<
typename K,
typename V,
528 : PersistentValueMap<K, V, Traits>(isolate) {}
533
534
535
536
537
538
539template <
typename K,
typename V,
544 : GlobalValueMap<K, V, Traits>(isolate) {}
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)
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)
V8_INLINE HandleScope(Isolate *isolate)
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)
friend class PersistentValueMapBase
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)
PersistentContainerCallbackType
@ kWeakWithInternalFields
uintptr_t PersistentContainerValue