OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #ifndef V8PersistentValueMap_h | |
32 #define V8PersistentValueMap_h | |
33 | |
34 #include "wtf/HashMap.h" | |
35 #include <v8-util.h> | |
36 #include <v8.h> | |
37 | |
38 namespace WebCore { | |
39 | |
40 /** | |
41 * A Traits class for v8::PersistentValueMap that uses wtf/HashMap as a | |
42 * backing store. | |
43 * | |
44 * The parameter is_weak will determine whether the references are 'weak'. | |
45 * If so, entries will be removed from the map as the weak references are | |
46 * collected. | |
47 */ | |
48 template<class KeyType, class ValueType, bool is_weak> | |
49 class V8PersistentValueMapTraits { | |
50 public: | |
51 // Map traits: | |
52 typedef HashMap<KeyType, v8::PersistentContainerValue> Impl; | |
53 typedef typename Impl::iterator Iterator; | |
54 static size_t Size(const Impl* impl) { return impl->size(); } | |
55 static bool Empty(Impl* impl) { return impl->isEmpty(); } | |
56 static void Swap(Impl& impl, Impl& other) { impl.swap(other); } | |
57 static Iterator Begin(Impl* impl) { return impl->begin(); } | |
58 static Iterator End(Impl* impl) { return impl->end(); } | |
59 static v8::PersistentContainerValue Value(Iterator& iter) | |
60 { | |
61 return iter->value; | |
62 } | |
63 static KeyType Key(Iterator& iter) { return iter->key; } | |
64 static v8::PersistentContainerValue Set( | |
65 Impl* impl, KeyType key, v8::PersistentContainerValue value) | |
66 { | |
67 v8::PersistentContainerValue oldValue = Get(impl, key); | |
68 impl->add(key, value); | |
69 return oldValue; | |
70 } | |
71 static v8::PersistentContainerValue Get(const Impl* impl, KeyType key) | |
72 { | |
73 return impl->get(key); | |
74 } | |
75 | |
76 static v8::PersistentContainerValue Remove(Impl* impl, KeyType key) | |
77 { | |
78 return impl->take(key); | |
79 } | |
80 | |
81 // Weak traits: | |
82 static const v8::PersistentContainerCallbackType kCallbackType = is_weak ? v
8::kWeak : v8::kNotWeak; | |
83 typedef v8::PersistentValueMap<KeyType, ValueType, V8PersistentValueMapTrait
s<KeyType, ValueType, is_weak> > MapType; | |
84 | |
85 typedef void WeakCallbackDataType; | |
86 | |
87 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType key
, const v8::Local<ValueType>& value) | |
88 { | |
89 return 0; | |
90 } | |
91 | |
92 static void DisposeCallbackData(WeakCallbackDataType* callbackData) | |
93 { | |
94 } | |
95 | |
96 static MapType* MapFromWeakCallbackData( | |
97 const v8::WeakCallbackData<ValueType, WeakCallbackDataType>& data) | |
98 { | |
99 return 0; | |
100 } | |
101 | |
102 static KeyType KeyFromWeakCallbackData( | |
103 const v8::WeakCallbackData<ValueType, WeakCallbackDataType>& data) | |
104 { | |
105 return KeyType(); | |
106 } | |
107 | |
108 // Dispose traits: | |
109 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<ValueType> va
lue, KeyType key) { } | |
110 }; | |
111 | |
112 /** | |
113 * A map for safely storing persistent V8 values, based on | |
114 * v8::PersistentValueMap. | |
115 * | |
116 * If is_weak is set, values will be held weakly and map entries will be | |
117 * removed as their values are being collected. | |
118 */ | |
119 template<class KeyType, class ValueType, bool is_weak = true> | |
120 class V8PersistentValueMap : public v8::PersistentValueMap<KeyType, ValueType, V
8PersistentValueMapTraits<KeyType, ValueType, is_weak> > { | |
121 public: | |
122 typedef V8PersistentValueMapTraits<KeyType, ValueType, is_weak> Traits; | |
123 explicit V8PersistentValueMap(v8::Isolate* isolate) : v8::PersistentValueMap
<KeyType, ValueType, Traits>(isolate) { } | |
124 }; | |
125 | |
126 } // namespace WebCore | |
127 | |
128 #endif // V8PersistentValueMap_h | |
OLD | NEW |