| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 * directly in std containers. | 134 * directly in std containers. |
| 135 * | 135 * |
| 136 * The map relies on a backing map, whose type and accessors are described | 136 * The map relies on a backing map, whose type and accessors are described |
| 137 * by the Traits class. The backing map will handle values of type | 137 * by the Traits class. The backing map will handle values of type |
| 138 * PersistentContainerValue, with all conversion into and out of V8 | 138 * PersistentContainerValue, with all conversion into and out of V8 |
| 139 * handles being transparently handled by this class. | 139 * handles being transparently handled by this class. |
| 140 */ | 140 */ |
| 141 template<typename K, typename V, typename Traits> | 141 template<typename K, typename V, typename Traits> |
| 142 class PersistentValueMap { | 142 class PersistentValueMap { |
| 143 public: | 143 public: |
| 144 V8_INLINE explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {} | 144 explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {} |
| 145 | 145 |
| 146 V8_INLINE ~PersistentValueMap() { Clear(); } | 146 ~PersistentValueMap() { Clear(); } |
| 147 | 147 |
| 148 V8_INLINE Isolate* GetIsolate() { return isolate_; } | 148 Isolate* GetIsolate() { return isolate_; } |
| 149 | 149 |
| 150 /** | 150 /** |
| 151 * Return size of the map. | 151 * Return size of the map. |
| 152 */ | 152 */ |
| 153 V8_INLINE size_t Size() { return Traits::Size(&impl_); } | 153 size_t Size() { return Traits::Size(&impl_); } |
| 154 | 154 |
| 155 /** | 155 /** |
| 156 * Return whether the map holds weak persistents. | 156 * Return whether the map holds weak persistents. |
| 157 */ | 157 */ |
| 158 V8_INLINE bool IsWeak() { return Traits::kCallbackType != kNotWeak; } | 158 bool IsWeak() { return Traits::kCallbackType != kNotWeak; } |
| 159 | 159 |
| 160 /** | 160 /** |
| 161 * Get value stored in map. | 161 * Get value stored in map. |
| 162 */ | 162 */ |
| 163 V8_INLINE Local<V> Get(const K& key) { | 163 Local<V> Get(const K& key) { |
| 164 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key))); | 164 return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key))); |
| 165 } | 165 } |
| 166 | 166 |
| 167 /** | 167 /** |
| 168 * Check whether a value is contained in the map. | 168 * Check whether a value is contained in the map. |
| 169 */ | 169 */ |
| 170 V8_INLINE bool Contains(const K& key) { | 170 bool Contains(const K& key) { |
| 171 return Traits::Get(&impl_, key) != kPersistentContainerNotFound; | 171 return Traits::Get(&impl_, key) != kPersistentContainerNotFound; |
| 172 } | 172 } |
| 173 | 173 |
| 174 /** | 174 /** |
| 175 * Get value stored in map and set it in returnValue. | 175 * Get value stored in map and set it in returnValue. |
| 176 * Return true if a value was found. | 176 * Return true if a value was found. |
| 177 */ | 177 */ |
| 178 V8_INLINE bool SetReturnValue(const K& key, | 178 bool SetReturnValue(const K& key, |
| 179 ReturnValue<Value> returnValue) { | 179 ReturnValue<Value> returnValue) { |
| 180 return SetReturnValueFromVal(returnValue, Traits::Get(&impl_, key)); | 180 return SetReturnValueFromVal(returnValue, Traits::Get(&impl_, key)); |
| 181 } | 181 } |
| 182 | 182 |
| 183 /** | 183 /** |
| 184 * Call Isolate::SetReference with the given parent and the map value. | 184 * Call Isolate::SetReference with the given parent and the map value. |
| 185 */ | 185 */ |
| 186 V8_INLINE void SetReference(const K& key, | 186 void SetReference(const K& key, |
| 187 const Persistent<Object>& parent) { | 187 const Persistent<Object>& parent) { |
| 188 GetIsolate()->SetReference( | 188 GetIsolate()->SetReference( |
| 189 reinterpret_cast<internal::Object**>(parent.val_), | 189 reinterpret_cast<internal::Object**>(parent.val_), |
| 190 reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key)))); | 190 reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key)))); |
| 191 } | 191 } |
| 192 | 192 |
| 193 /** | 193 /** |
| 194 * Put value into map. Depending on Traits::kIsWeak, the value will be held | 194 * Put value into map. Depending on Traits::kIsWeak, the value will be held |
| 195 * by the map strongly or weakly. | 195 * by the map strongly or weakly. |
| 196 * Returns old value as UniquePersistent. | 196 * Returns old value as UniquePersistent. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 /** | 275 /** |
| 276 * Get a reference to a map value. This enables fast, repeated access | 276 * Get a reference to a map value. This enables fast, repeated access |
| 277 * to a value stored in the map while the map remains unchanged. | 277 * to a value stored in the map while the map remains unchanged. |
| 278 * | 278 * |
| 279 * Careful: This is potentially unsafe, so please use with care. | 279 * Careful: This is potentially unsafe, so please use with care. |
| 280 * The value will become invalid if the value for this key changes | 280 * The value will become invalid if the value for this key changes |
| 281 * in the underlying map, as a result of Set or Remove for the same | 281 * in the underlying map, as a result of Set or Remove for the same |
| 282 * key; as a result of the weak callback for the same key; or as a | 282 * key; as a result of the weak callback for the same key; or as a |
| 283 * result of calling Clear() or destruction of the map. | 283 * result of calling Clear() or destruction of the map. |
| 284 */ | 284 */ |
| 285 V8_INLINE PersistentValueReference GetReference(const K& key) { | 285 PersistentValueReference GetReference(const K& key) { |
| 286 return PersistentValueReference(Traits::Get(&impl_, key)); | 286 return PersistentValueReference(Traits::Get(&impl_, key)); |
| 287 } | 287 } |
| 288 | 288 |
| 289 /** | 289 /** |
| 290 * Put a value into the map and update the reference. | 290 * Put a value into the map and update the reference. |
| 291 * Restrictions of GetReference apply here as well. | 291 * Restrictions of GetReference apply here as well. |
| 292 */ | 292 */ |
| 293 UniquePersistent<V> Set(const K& key, UniquePersistent<V> value, | 293 UniquePersistent<V> Set(const K& key, UniquePersistent<V> value, |
| 294 PersistentValueReference* reference) { | 294 PersistentValueReference* reference) { |
| 295 *reference = Leak(&value); | 295 *reference = Leak(&value); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 319 const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) { | 319 const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) { |
| 320 if (Traits::kCallbackType != kNotWeak) { | 320 if (Traits::kCallbackType != kNotWeak) { |
| 321 PersistentValueMap<K, V, Traits>* persistentValueMap = | 321 PersistentValueMap<K, V, Traits>* persistentValueMap = |
| 322 Traits::MapFromWeakCallbackData(data); | 322 Traits::MapFromWeakCallbackData(data); |
| 323 K key = Traits::KeyFromWeakCallbackData(data); | 323 K key = Traits::KeyFromWeakCallbackData(data); |
| 324 Traits::Dispose(data.GetIsolate(), | 324 Traits::Dispose(data.GetIsolate(), |
| 325 persistentValueMap->Remove(key).Pass(), key); | 325 persistentValueMap->Remove(key).Pass(), key); |
| 326 } | 326 } |
| 327 } | 327 } |
| 328 | 328 |
| 329 V8_INLINE static V* FromVal(PersistentContainerValue v) { | 329 static V* FromVal(PersistentContainerValue v) { |
| 330 return reinterpret_cast<V*>(v); | 330 return reinterpret_cast<V*>(v); |
| 331 } | 331 } |
| 332 | 332 |
| 333 V8_INLINE static bool SetReturnValueFromVal( | 333 static bool SetReturnValueFromVal( |
| 334 ReturnValue<Value>& returnValue, PersistentContainerValue value) { | 334 ReturnValue<Value>& returnValue, PersistentContainerValue value) { |
| 335 bool hasValue = value != kPersistentContainerNotFound; | 335 bool hasValue = value != kPersistentContainerNotFound; |
| 336 if (hasValue) { | 336 if (hasValue) { |
| 337 returnValue.SetInternal( | 337 returnValue.SetInternal( |
| 338 *reinterpret_cast<internal::Object**>(FromVal(value))); | 338 *reinterpret_cast<internal::Object**>(FromVal(value))); |
| 339 } | 339 } |
| 340 return hasValue; | 340 return hasValue; |
| 341 } | 341 } |
| 342 | 342 |
| 343 V8_INLINE static PersistentContainerValue ClearAndLeak( | 343 static PersistentContainerValue ClearAndLeak( |
| 344 UniquePersistent<V>* persistent) { | 344 UniquePersistent<V>* persistent) { |
| 345 V* v = persistent->val_; | 345 V* v = persistent->val_; |
| 346 persistent->val_ = 0; | 346 persistent->val_ = 0; |
| 347 return reinterpret_cast<PersistentContainerValue>(v); | 347 return reinterpret_cast<PersistentContainerValue>(v); |
| 348 } | 348 } |
| 349 | 349 |
| 350 V8_INLINE static PersistentContainerValue Leak( | 350 static PersistentContainerValue Leak( |
| 351 UniquePersistent<V>* persistent) { | 351 UniquePersistent<V>* persistent) { |
| 352 return reinterpret_cast<PersistentContainerValue>(persistent->val_); | 352 return reinterpret_cast<PersistentContainerValue>(persistent->val_); |
| 353 } | 353 } |
| 354 | 354 |
| 355 /** | 355 /** |
| 356 * Return a container value as UniquePersistent and make sure the weak | 356 * Return a container value as UniquePersistent and make sure the weak |
| 357 * callback is properly disposed of. All remove functionality should go | 357 * callback is properly disposed of. All remove functionality should go |
| 358 * through this. | 358 * through this. |
| 359 */ | 359 */ |
| 360 static UniquePersistent<V> Release(PersistentContainerValue v) { | 360 static UniquePersistent<V> Release(PersistentContainerValue v) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 return reinterpret_cast<V*>(v); | 500 return reinterpret_cast<V*>(v); |
| 501 } | 501 } |
| 502 | 502 |
| 503 Isolate* isolate_; | 503 Isolate* isolate_; |
| 504 typename Traits::Impl impl_; | 504 typename Traits::Impl impl_; |
| 505 }; | 505 }; |
| 506 | 506 |
| 507 } // namespace v8 | 507 } // namespace v8 |
| 508 | 508 |
| 509 #endif // V8_UTIL_H_ | 509 #endif // V8_UTIL_H_ |
| OLD | NEW |