1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
13 * Support for Persistent containers.
15 * C++11 embedders can use STL containers with UniquePersistent values,
16 * but pre-C++11 does not support the required move semantic and hence
17 * may want these container classes.
21 typedef uintptr_t PersistentContainerValue;
22 static const uintptr_t kPersistentContainerNotFound = 0;
23 enum PersistentContainerCallbackType {
30 * A default trait implemenation for PersistentValueMap which uses std::map
33 * Users will have to implement their own weak callbacks & dispose traits.
35 template<typename K, typename V>
39 typedef std::map<K, PersistentContainerValue> Impl;
40 typedef typename Impl::iterator Iterator;
42 static bool Empty(Impl* impl) { return impl->empty(); }
43 static size_t Size(Impl* impl) { return impl->size(); }
44 static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT
45 static Iterator Begin(Impl* impl) { return impl->begin(); }
46 static Iterator End(Impl* impl) { return impl->end(); }
47 static K Key(Iterator it) { return it->first; }
48 static PersistentContainerValue Value(Iterator it) { return it->second; }
49 static PersistentContainerValue Set(Impl* impl, K key,
50 PersistentContainerValue value) {
51 std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
52 PersistentContainerValue old_value = kPersistentContainerNotFound;
54 old_value = res.first->second;
55 res.first->second = value;
59 static PersistentContainerValue Get(Impl* impl, K key) {
60 Iterator it = impl->find(key);
61 if (it == impl->end()) return kPersistentContainerNotFound;
64 static PersistentContainerValue Remove(Impl* impl, K key) {
65 Iterator it = impl->find(key);
66 if (it == impl->end()) return kPersistentContainerNotFound;
67 PersistentContainerValue value = it->second;
75 * A default trait implementation for PersistentValueMap, which inherits
76 * a std:map backing map from StdMapTraits and holds non-weak persistent
77 * objects and has no special Dispose handling.
79 * You should not derive from this class, since MapType depends on the
80 * surrounding class, and hence a subclass cannot simply inherit the methods.
82 template<typename K, typename V>
83 class DefaultPersistentValueMapTraits : public StdMapTraits<K, V> {
85 // Weak callback & friends:
86 static const PersistentContainerCallbackType kCallbackType = kNotWeak;
87 typedef PersistentValueMap<K, V, DefaultPersistentValueMapTraits<K, V> >
89 typedef void WeakCallbackDataType;
91 static WeakCallbackDataType* WeakCallbackParameter(
92 MapType* map, const K& key, Local<V> value) {
95 static MapType* MapFromWeakCallbackData(
96 const WeakCallbackData<V, WeakCallbackDataType>& data) {
99 static K KeyFromWeakCallbackData(
100 const WeakCallbackData<V, WeakCallbackDataType>& data) {
103 static void DisposeCallbackData(WeakCallbackDataType* data) { }
104 static void Dispose(Isolate* isolate, UniquePersistent<V> value, K key) { }
109 * A map wrapper that allows using UniquePersistent as a mapped value.
110 * C++11 embedders don't need this class, as they can use UniquePersistent
111 * directly in std containers.
113 * The map relies on a backing map, whose type and accessors are described
114 * by the Traits class. The backing map will handle values of type
115 * PersistentContainerValue, with all conversion into and out of V8
116 * handles being transparently handled by this class.
118 template<typename K, typename V, typename Traits>
119 class PersistentValueMap {
121 explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {}
123 ~PersistentValueMap() { Clear(); }
125 Isolate* GetIsolate() { return isolate_; }
128 * Return size of the map.
130 size_t Size() { return Traits::Size(&impl_); }
133 * Return whether the map holds weak persistents.
135 bool IsWeak() { return Traits::kCallbackType != kNotWeak; }
138 * Get value stored in map.
140 Local<V> Get(const K& key) {
141 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
145 * Check whether a value is contained in the map.
147 bool Contains(const K& key) {
148 return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
152 * Get value stored in map and set it in returnValue.
153 * Return true if a value was found.
155 bool SetReturnValue(const K& key,
156 ReturnValue<Value> returnValue) {
157 return SetReturnValueFromVal(returnValue, Traits::Get(&impl_, key));
161 * Call Isolate::SetReference with the given parent and the map value.
163 void SetReference(const K& key,
164 const Persistent<Object>& parent) {
165 GetIsolate()->SetReference(
166 reinterpret_cast<internal::Object**>(parent.val_),
167 reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))));
171 * Put value into map. Depending on Traits::kIsWeak, the value will be held
172 * by the map strongly or weakly.
173 * Returns old value as UniquePersistent.
175 UniquePersistent<V> Set(const K& key, Local<V> value) {
176 UniquePersistent<V> persistent(isolate_, value);
177 return SetUnique(key, &persistent);
181 * Put value into map, like Set(const K&, Local<V>).
183 UniquePersistent<V> Set(const K& key, UniquePersistent<V> value) {
184 return SetUnique(key, &value);
188 * Return value for key and remove it from the map.
190 UniquePersistent<V> Remove(const K& key) {
191 return Release(Traits::Remove(&impl_, key)).Pass();
195 * Traverses the map repeatedly,
196 * in case side effects of disposal cause insertions.
199 typedef typename Traits::Iterator It;
200 HandleScope handle_scope(isolate_);
201 // TODO(dcarney): figure out if this swap and loop is necessary.
202 while (!Traits::Empty(&impl_)) {
203 typename Traits::Impl impl;
204 Traits::Swap(impl_, impl);
205 for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
206 Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(),
213 * Helper class for GetReference/SetWithReference. Do not use outside
216 class PersistentValueReference {
218 PersistentValueReference() : value_(kPersistentContainerNotFound) { }
219 PersistentValueReference(const PersistentValueReference& other)
220 : value_(other.value_) { }
222 Local<V> NewLocal(Isolate* isolate) const {
223 return Local<V>::New(isolate, FromVal(value_));
225 bool IsEmpty() const {
226 return value_ == kPersistentContainerNotFound;
229 bool SetReturnValue(ReturnValue<T> returnValue) {
230 return SetReturnValueFromVal(returnValue, value_);
233 value_ = kPersistentContainerNotFound;
235 void operator=(const PersistentValueReference& other) {
236 value_ = other.value_;
240 friend class PersistentValueMap;
242 explicit PersistentValueReference(PersistentContainerValue value)
245 void operator=(PersistentContainerValue value) {
249 PersistentContainerValue value_;
253 * Get a reference to a map value. This enables fast, repeated access
254 * to a value stored in the map while the map remains unchanged.
256 * Careful: This is potentially unsafe, so please use with care.
257 * The value will become invalid if the value for this key changes
258 * in the underlying map, as a result of Set or Remove for the same
259 * key; as a result of the weak callback for the same key; or as a
260 * result of calling Clear() or destruction of the map.
262 PersistentValueReference GetReference(const K& key) {
263 return PersistentValueReference(Traits::Get(&impl_, key));
267 * Put a value into the map and update the reference.
268 * Restrictions of GetReference apply here as well.
270 UniquePersistent<V> Set(const K& key, UniquePersistent<V> value,
271 PersistentValueReference* reference) {
272 *reference = Leak(&value);
273 return SetUnique(key, &value);
277 PersistentValueMap(PersistentValueMap&);
278 void operator=(PersistentValueMap&);
281 * Put the value into the map, and set the 'weak' callback when demanded
282 * by the Traits class.
284 UniquePersistent<V> SetUnique(const K& key, UniquePersistent<V>* persistent) {
285 if (Traits::kCallbackType != kNotWeak) {
286 Local<V> value(Local<V>::New(isolate_, *persistent));
287 persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
288 Traits::WeakCallbackParameter(this, key, value), WeakCallback);
290 PersistentContainerValue old_value =
291 Traits::Set(&impl_, key, ClearAndLeak(persistent));
292 return Release(old_value).Pass();
295 static void WeakCallback(
296 const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) {
297 if (Traits::kCallbackType != kNotWeak) {
298 PersistentValueMap<K, V, Traits>* persistentValueMap =
299 Traits::MapFromWeakCallbackData(data);
300 K key = Traits::KeyFromWeakCallbackData(data);
301 Traits::Dispose(data.GetIsolate(),
302 persistentValueMap->Remove(key).Pass(), key);
306 static V* FromVal(PersistentContainerValue v) {
307 return reinterpret_cast<V*>(v);
310 static bool SetReturnValueFromVal(
311 ReturnValue<Value>& returnValue, PersistentContainerValue value) {
312 bool hasValue = value != kPersistentContainerNotFound;
314 returnValue.SetInternal(
315 *reinterpret_cast<internal::Object**>(FromVal(value)));
320 static PersistentContainerValue ClearAndLeak(
321 UniquePersistent<V>* persistent) {
322 V* v = persistent->val_;
323 persistent->val_ = 0;
324 return reinterpret_cast<PersistentContainerValue>(v);
327 static PersistentContainerValue Leak(
328 UniquePersistent<V>* persistent) {
329 return reinterpret_cast<PersistentContainerValue>(persistent->val_);
333 * Return a container value as UniquePersistent and make sure the weak
334 * callback is properly disposed of. All remove functionality should go
337 static UniquePersistent<V> Release(PersistentContainerValue v) {
338 UniquePersistent<V> p;
340 if (Traits::kCallbackType != kNotWeak && !p.IsEmpty()) {
341 Traits::DisposeCallbackData(
342 p.template ClearWeak<typename Traits::WeakCallbackDataType>());
348 typename Traits::Impl impl_;
353 * A map that uses UniquePersistent as value and std::map as the backing
354 * implementation. Persistents are held non-weak.
356 * C++11 embedders don't need this class, as they can use
357 * UniquePersistent directly in std containers.
359 template<typename K, typename V,
360 typename Traits = DefaultPersistentValueMapTraits<K, V> >
361 class StdPersistentValueMap : public PersistentValueMap<K, V, Traits> {
363 explicit StdPersistentValueMap(Isolate* isolate)
364 : PersistentValueMap<K, V, Traits>(isolate) {}
368 class DefaultPersistentValueVectorTraits {
370 typedef std::vector<PersistentContainerValue> Impl;
372 static void Append(Impl* impl, PersistentContainerValue value) {
373 impl->push_back(value);
375 static bool IsEmpty(const Impl* impl) {
376 return impl->empty();
378 static size_t Size(const Impl* impl) {
381 static PersistentContainerValue Get(const Impl* impl, size_t i) {
382 return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
384 static void ReserveCapacity(Impl* impl, size_t capacity) {
385 impl->reserve(capacity);
387 static void Clear(Impl* impl) {
394 * A vector wrapper that safely stores UniquePersistent values.
395 * C++11 embedders don't need this class, as they can use UniquePersistent
396 * directly in std containers.
398 * This class relies on a backing vector implementation, whose type and methods
399 * are described by the Traits class. The backing map will handle values of type
400 * PersistentContainerValue, with all conversion into and out of V8
401 * handles being transparently handled by this class.
403 template<typename V, typename Traits = DefaultPersistentValueVectorTraits>
404 class PersistentValueVector {
406 explicit PersistentValueVector(Isolate* isolate) : isolate_(isolate) { }
408 ~PersistentValueVector() {
413 * Append a value to the vector.
415 void Append(Local<V> value) {
416 UniquePersistent<V> persistent(isolate_, value);
417 Traits::Append(&impl_, ClearAndLeak(&persistent));
421 * Append a persistent's value to the vector.
423 void Append(UniquePersistent<V> persistent) {
424 Traits::Append(&impl_, ClearAndLeak(&persistent));
428 * Are there any values in the vector?
430 bool IsEmpty() const {
431 return Traits::IsEmpty(&impl_);
435 * How many elements are in the vector?
437 size_t Size() const {
438 return Traits::Size(&impl_);
442 * Retrieve the i-th value in the vector.
444 Local<V> Get(size_t index) const {
445 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
449 * Remove all elements from the vector.
452 size_t length = Traits::Size(&impl_);
453 for (size_t i = 0; i < length; i++) {
454 UniquePersistent<V> p;
455 p.val_ = FromVal(Traits::Get(&impl_, i));
457 Traits::Clear(&impl_);
461 * Reserve capacity in the vector.
462 * (Efficiency gains depend on the backing implementation.)
464 void ReserveCapacity(size_t capacity) {
465 Traits::ReserveCapacity(&impl_, capacity);
469 static PersistentContainerValue ClearAndLeak(
470 UniquePersistent<V>* persistent) {
471 V* v = persistent->val_;
472 persistent->val_ = 0;
473 return reinterpret_cast<PersistentContainerValue>(v);
476 static V* FromVal(PersistentContainerValue v) {
477 return reinterpret_cast<V*>(v);
481 typename Traits::Impl impl_;