v8-util.h | v8-util.h | |||
---|---|---|---|---|
skipping to change at line 141 | skipping to change at line 141 | |||
* directly in std containers. | * directly in std containers. | |||
* | * | |||
* The map relies on a backing map, whose type and accessors are described | * The map relies on a backing map, whose type and accessors are described | |||
* by the Traits class. The backing map will handle values of type | * by the Traits class. The backing map will handle values of type | |||
* PersistentContainerValue, with all conversion into and out of V8 | * PersistentContainerValue, with all conversion into and out of V8 | |||
* handles being transparently handled by this class. | * handles being transparently handled by this class. | |||
*/ | */ | |||
template<typename K, typename V, typename Traits> | template<typename K, typename V, typename Traits> | |||
class PersistentValueMap { | class PersistentValueMap { | |||
public: | public: | |||
V8_INLINE explicit PersistentValueMap(Isolate* isolate) : isolate_(isolat e) {} | explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {} | |||
V8_INLINE ~PersistentValueMap() { Clear(); } | ~PersistentValueMap() { Clear(); } | |||
V8_INLINE Isolate* GetIsolate() { return isolate_; } | Isolate* GetIsolate() { return isolate_; } | |||
/** | /** | |||
* Return size of the map. | * Return size of the map. | |||
*/ | */ | |||
V8_INLINE size_t Size() { return Traits::Size(&impl_); } | size_t Size() { return Traits::Size(&impl_); } | |||
/** | /** | |||
* Return whether the map holds weak persistents. | * Return whether the map holds weak persistents. | |||
*/ | */ | |||
V8_INLINE bool IsWeak() { return Traits::kCallbackType != kNotWeak; } | bool IsWeak() { return Traits::kCallbackType != kNotWeak; } | |||
/** | /** | |||
* Get value stored in map. | * Get value stored in map. | |||
*/ | */ | |||
V8_INLINE Local<V> Get(const K& key) { | Local<V> Get(const K& key) { | |||
return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key))); | return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key))); | |||
} | } | |||
/** | /** | |||
* Check whether a value is contained in the map. | * Check whether a value is contained in the map. | |||
*/ | */ | |||
V8_INLINE bool Contains(const K& key) { | bool Contains(const K& key) { | |||
return Traits::Get(&impl_, key) != kPersistentContainerNotFound; | return Traits::Get(&impl_, key) != kPersistentContainerNotFound; | |||
} | } | |||
/** | /** | |||
* Get value stored in map and set it in returnValue. | * Get value stored in map and set it in returnValue. | |||
* Return true if a value was found. | * Return true if a value was found. | |||
*/ | */ | |||
V8_INLINE bool SetReturnValue(const K& key, | bool SetReturnValue(const K& key, | |||
ReturnValue<Value> returnValue) { | ReturnValue<Value> returnValue) { | |||
return SetReturnValueFromVal(returnValue, Traits::Get(&impl_, key)); | return SetReturnValueFromVal(returnValue, Traits::Get(&impl_, key)); | |||
} | } | |||
/** | /** | |||
* Call Isolate::SetReference with the given parent and the map value. | * Call Isolate::SetReference with the given parent and the map value. | |||
*/ | */ | |||
V8_INLINE void SetReference(const K& key, | void SetReference(const K& key, | |||
const Persistent<Object>& parent) { | const Persistent<Object>& parent) { | |||
GetIsolate()->SetReference( | GetIsolate()->SetReference( | |||
reinterpret_cast<internal::Object**>(parent.val_), | reinterpret_cast<internal::Object**>(parent.val_), | |||
reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key) ))); | reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key) ))); | |||
} | } | |||
/** | /** | |||
* Put value into map. Depending on Traits::kIsWeak, the value will be he ld | * Put value into map. Depending on Traits::kIsWeak, the value will be he ld | |||
* by the map strongly or weakly. | * by the map strongly or weakly. | |||
* Returns old value as UniquePersistent. | * Returns old value as UniquePersistent. | |||
skipping to change at line 282 | skipping to change at line 282 | |||
/** | /** | |||
* Get a reference to a map value. This enables fast, repeated access | * Get a reference to a map value. This enables fast, repeated access | |||
* to a value stored in the map while the map remains unchanged. | * to a value stored in the map while the map remains unchanged. | |||
* | * | |||
* Careful: This is potentially unsafe, so please use with care. | * Careful: This is potentially unsafe, so please use with care. | |||
* The value will become invalid if the value for this key changes | * The value will become invalid if the value for this key changes | |||
* in the underlying map, as a result of Set or Remove for the same | * in the underlying map, as a result of Set or Remove for the same | |||
* key; as a result of the weak callback for the same key; or as a | * key; as a result of the weak callback for the same key; or as a | |||
* result of calling Clear() or destruction of the map. | * result of calling Clear() or destruction of the map. | |||
*/ | */ | |||
V8_INLINE PersistentValueReference GetReference(const K& key) { | PersistentValueReference GetReference(const K& key) { | |||
return PersistentValueReference(Traits::Get(&impl_, key)); | return PersistentValueReference(Traits::Get(&impl_, key)); | |||
} | } | |||
/** | /** | |||
* Put a value into the map and update the reference. | * Put a value into the map and update the reference. | |||
* Restrictions of GetReference apply here as well. | * Restrictions of GetReference apply here as well. | |||
*/ | */ | |||
UniquePersistent<V> Set(const K& key, UniquePersistent<V> value, | UniquePersistent<V> Set(const K& key, UniquePersistent<V> value, | |||
PersistentValueReference* reference) { | PersistentValueReference* reference) { | |||
*reference = Leak(&value); | *reference = Leak(&value); | |||
skipping to change at line 326 | skipping to change at line 326 | |||
const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& dat a) { | const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& dat a) { | |||
if (Traits::kCallbackType != kNotWeak) { | if (Traits::kCallbackType != kNotWeak) { | |||
PersistentValueMap<K, V, Traits>* persistentValueMap = | PersistentValueMap<K, V, Traits>* persistentValueMap = | |||
Traits::MapFromWeakCallbackData(data); | Traits::MapFromWeakCallbackData(data); | |||
K key = Traits::KeyFromWeakCallbackData(data); | K key = Traits::KeyFromWeakCallbackData(data); | |||
Traits::Dispose(data.GetIsolate(), | Traits::Dispose(data.GetIsolate(), | |||
persistentValueMap->Remove(key).Pass(), key); | persistentValueMap->Remove(key).Pass(), key); | |||
} | } | |||
} | } | |||
V8_INLINE static V* FromVal(PersistentContainerValue v) { | static V* FromVal(PersistentContainerValue v) { | |||
return reinterpret_cast<V*>(v); | return reinterpret_cast<V*>(v); | |||
} | } | |||
V8_INLINE static bool SetReturnValueFromVal( | static bool SetReturnValueFromVal( | |||
ReturnValue<Value>& returnValue, PersistentContainerValue value) { | ReturnValue<Value>& returnValue, PersistentContainerValue value) { | |||
bool hasValue = value != kPersistentContainerNotFound; | bool hasValue = value != kPersistentContainerNotFound; | |||
if (hasValue) { | if (hasValue) { | |||
returnValue.SetInternal( | returnValue.SetInternal( | |||
*reinterpret_cast<internal::Object**>(FromVal(value))); | *reinterpret_cast<internal::Object**>(FromVal(value))); | |||
} | } | |||
return hasValue; | return hasValue; | |||
} | } | |||
V8_INLINE static PersistentContainerValue ClearAndLeak( | static PersistentContainerValue ClearAndLeak( | |||
UniquePersistent<V>* persistent) { | UniquePersistent<V>* persistent) { | |||
V* v = persistent->val_; | V* v = persistent->val_; | |||
persistent->val_ = 0; | persistent->val_ = 0; | |||
return reinterpret_cast<PersistentContainerValue>(v); | return reinterpret_cast<PersistentContainerValue>(v); | |||
} | } | |||
V8_INLINE static PersistentContainerValue Leak( | static PersistentContainerValue Leak( | |||
UniquePersistent<V>* persistent) { | UniquePersistent<V>* persistent) { | |||
return reinterpret_cast<PersistentContainerValue>(persistent->val_); | return reinterpret_cast<PersistentContainerValue>(persistent->val_); | |||
} | } | |||
/** | /** | |||
* Return a container value as UniquePersistent and make sure the weak | * Return a container value as UniquePersistent and make sure the weak | |||
* callback is properly disposed of. All remove functionality should go | * callback is properly disposed of. All remove functionality should go | |||
* through this. | * through this. | |||
*/ | */ | |||
static UniquePersistent<V> Release(PersistentContainerValue v) { | static UniquePersistent<V> Release(PersistentContainerValue v) { | |||
End of changes. 14 change blocks. | ||||
14 lines changed or deleted | 14 lines changed or added | |||