v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
v8-util.h
Go to the documentation of this file.
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_UTIL_H_
29 #define V8_UTIL_H_
30 
31 #include "v8.h"
32 #include <map>
33 
41 namespace v8 {
42 
43 typedef uintptr_t PersistentContainerValue;
44 static const uintptr_t kPersistentContainerNotFound = 0;
45 
46 
53 template<typename K, typename V>
54 class StdMapTraits {
55  public:
56  // STL map & related:
57  typedef std::map<K, PersistentContainerValue> Impl;
58  typedef typename Impl::iterator Iterator;
59 
60  static bool Empty(Impl* impl) { return impl->empty(); }
61  static size_t Size(Impl* impl) { return impl->size(); }
62  static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT
63  static Iterator Begin(Impl* impl) { return impl->begin(); }
64  static Iterator End(Impl* impl) { return impl->end(); }
65  static K Key(Iterator it) { return it->first; }
66  static PersistentContainerValue Value(Iterator it) { return it->second; }
67  static PersistentContainerValue Set(Impl* impl, K key,
69  std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
70  PersistentContainerValue old_value = kPersistentContainerNotFound;
71  if (!res.second) {
72  old_value = res.first->second;
73  res.first->second = value;
74  }
75  return old_value;
76  }
77  static PersistentContainerValue Get(Impl* impl, K key) {
78  Iterator it = impl->find(key);
79  if (it == impl->end()) return kPersistentContainerNotFound;
80  return it->second;
81  }
82  static PersistentContainerValue Remove(Impl* impl, K key) {
83  Iterator it = impl->find(key);
84  if (it == impl->end()) return kPersistentContainerNotFound;
85  PersistentContainerValue value = it->second;
86  impl->erase(it);
87  return value;
88  }
89 };
90 
91 
99 template<typename K, typename V>
100 class StrongMapTraits : public StdMapTraits<K, V> {
101  public:
102  // Weak callback & friends:
103  static const bool kIsWeak = false;
104  typedef typename StdMapTraits<K, V>::Impl Impl;
105  typedef void WeakCallbackDataType;
107  Impl* impl, const K& key, Local<V> value);
110  static K KeyFromWeakCallbackData(
112  static void DisposeCallbackData(WeakCallbackDataType* data);
113 };
114 
115 
121 template<typename K, typename V>
123  public:
125  static void Dispose(Isolate* isolate, UniquePersistent<V> value,
126  Impl* impl, K key) { }
127 };
128 
129 
140 template<typename K, typename V, typename Traits>
142  public:
143  V8_INLINE explicit PersistentValueMap(Isolate* isolate) : isolate_(isolate) {}
144 
146 
147  V8_INLINE Isolate* GetIsolate() { return isolate_; }
148 
152  V8_INLINE size_t Size() { return Traits::Size(&impl_); }
153 
157  V8_INLINE bool IsWeak() { return Traits::kIsWeak; }
158 
162  V8_INLINE Local<V> Get(const K& key) {
163  return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
164  }
165 
169  V8_INLINE bool Contains(const K& key) {
170  return Traits::Get(&impl_, key) != 0;
171  }
172 
177  V8_INLINE bool SetReturnValue(const K& key,
178  ReturnValue<Value>& returnValue) {
179  PersistentContainerValue value = Traits::Get(&impl_, key);
180  bool hasValue = value != 0;
181  if (hasValue) {
182  returnValue.SetInternal(
183  *reinterpret_cast<internal::Object**>(FromVal(value)));
184  }
185  return hasValue;
186  }
187 
191  V8_INLINE void SetReference(const K& key,
192  const Persistent<Object>& parent) {
194  reinterpret_cast<internal::Object**>(parent.val_),
195  reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))));
196  }
197 
203  UniquePersistent<V> Set(const K& key, Local<V> value) {
204  UniquePersistent<V> persistent(isolate_, value);
205  return SetUnique(key, &persistent);
206  }
207 
212  return SetUnique(key, &value);
213  }
214 
219  return Release(Traits::Remove(&impl_, key)).Pass();
220  }
221 
226  void Clear() {
227  typedef typename Traits::Iterator It;
228  HandleScope handle_scope(isolate_);
229  // TODO(dcarney): figure out if this swap and loop is necessary.
230  while (!Traits::Empty(&impl_)) {
231  typename Traits::Impl impl;
232  Traits::Swap(impl_, impl);
233  for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
234  Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(), &impl,
235  Traits::Key(i));
236  }
237  }
238  }
239 
240  private:
242  void operator=(PersistentValueMap&);
243 
248  UniquePersistent<V> SetUnique(const K& key, UniquePersistent<V>* persistent) {
249  if (Traits::kIsWeak) {
250  Local<V> value(Local<V>::New(isolate_, *persistent));
251  persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
252  Traits::WeakCallbackParameter(&impl_, key, value), WeakCallback);
253  }
254  PersistentContainerValue old_value =
255  Traits::Set(&impl_, key, ClearAndLeak(persistent));
256  return Release(old_value).Pass();
257  }
258 
259  static void WeakCallback(
260  const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) {
261  if (Traits::kIsWeak) {
262  typename Traits::Impl* impl = Traits::ImplFromWeakCallbackData(data);
263  K key = Traits::KeyFromWeakCallbackData(data);
264  PersistentContainerValue value = Traits::Remove(impl, key);
265  Traits::Dispose(data.GetIsolate(), Release(value).Pass(), impl, key);
266  }
267  }
268 
269  V8_INLINE static V* FromVal(PersistentContainerValue v) {
270  return reinterpret_cast<V*>(v);
271  }
272 
273  V8_INLINE static PersistentContainerValue ClearAndLeak(
274  UniquePersistent<V>* persistent) {
275  V* v = persistent->val_;
276  persistent->val_ = 0;
277  return reinterpret_cast<PersistentContainerValue>(v);
278  }
279 
285  V8_INLINE static UniquePersistent<V> Release(PersistentContainerValue v) {
286  UniquePersistent<V> p;
287  p.val_ = FromVal(v);
288  if (Traits::kIsWeak && !p.IsEmpty()) {
289  Traits::DisposeCallbackData(
290  p.template ClearWeak<typename Traits::WeakCallbackDataType>());
291  }
292  return p.Pass();
293  }
294 
295  Isolate* isolate_;
296  typename Traits::Impl impl_;
297 };
298 
299 
307 template<typename K, typename V,
308  typename Traits = DefaultPersistentValueMapTraits<K, V> >
309 class StdPersistentValueMap : public PersistentValueMap<K, V, Traits> {
310  public:
311  explicit StdPersistentValueMap(Isolate* isolate)
312  : PersistentValueMap<K, V, Traits>(isolate) {}
313 };
314 
315 
326 template<typename K, typename V>
327 typename StrongMapTraits<K, V>::WeakCallbackDataType*
329  Impl* impl, const K& key, Local<V> value) {
330  return NULL;
331 }
332 
333 
334 template<typename K, typename V>
338  return NULL;
339 }
340 
341 
342 template<typename K, typename V>
345  return K();
346 }
347 
348 
349 template<typename K, typename V>
351 }
352 
353 } // namespace v8
354 
355 #endif // V8_UTIL_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
static V8_INLINE Local< T > New(Isolate *isolate, Handle< T > that)
Definition: v8.h:5713
V8_INLINE bool SetReturnValue(const K &key, ReturnValue< Value > &returnValue)
Definition: v8-util.h:177
UniquePersistent< V > Set(const K &key, UniquePersistent< V > value)
Definition: v8-util.h:211
static PersistentContainerValue Remove(Impl *impl, K key)
Definition: v8-util.h:82
static size_t Size(Impl *impl)
Definition: v8-util.h:61
static WeakCallbackDataType * WeakCallbackParameter(Impl *impl, const K &key, Local< V > value)
Definition: v8-util.h:328
V8_INLINE bool IsWeak()
Definition: v8-util.h:157
V8_INLINE ~PersistentValueMap()
Definition: v8-util.h:145
V8_INLINE Local< V > Get(const K &key)
Definition: v8-util.h:162
V8_INLINE size_t Size()
Definition: v8-util.h:152
static void Dispose(Isolate *isolate, UniquePersistent< V > value, Impl *impl, K key)
Definition: v8-util.h:125
static bool Empty(Impl *impl)
Definition: v8-util.h:60
uintptr_t PersistentContainerValue
Definition: v8-util.h:43
V8_INLINE void SetReference(const K &key, const Persistent< Object > &parent)
Definition: v8-util.h:191
static K Key(Iterator it)
Definition: v8-util.h:65
static Iterator Begin(Impl *impl)
Definition: v8-util.h:63
static Iterator End(Impl *impl)
Definition: v8-util.h:64
V8_INLINE UniquePersistent< V > Remove(const K &key)
Definition: v8-util.h:218
UniquePersistent< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:203
V8_INLINE Isolate * GetIsolate()
Definition: v8-util.h:147
static Impl * ImplFromWeakCallbackData(const WeakCallbackData< V, WeakCallbackDataType > &data)
Definition: v8-util.h:336
void WeakCallbackDataType
Definition: v8-util.h:105
StdPersistentValueMap(Isolate *isolate)
Definition: v8-util.h:311
static K KeyFromWeakCallbackData(const WeakCallbackData< V, WeakCallbackDataType > &data)
Definition: v8-util.h:343
V8_INLINE bool Contains(const K &key)
Definition: v8-util.h:169
void SetReference(const Persistent< T > &parent, const Persistent< S > &child)
Definition: v8.h:6613
StrongMapTraits< K, V >::Impl Impl
Definition: v8-util.h:124
Impl::iterator Iterator
Definition: v8-util.h:58
static PersistentContainerValue Value(Iterator it)
Definition: v8-util.h:66
static const bool kIsWeak
Definition: v8-util.h:103
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
Definition: v8-util.h:67
V8_INLINE PersistentValueMap(Isolate *isolate)
Definition: v8-util.h:143
std::map< K, PersistentContainerValue > Impl
Definition: v8-util.h:57
static void Swap(Impl &a, Impl &b)
Definition: v8-util.h:62
static void DisposeCallbackData(WeakCallbackDataType *data)
Definition: v8-util.h:350
Definition: v8.h:124
#define V8_INLINE
Definition: v8config.h:316
static PersistentContainerValue Get(Impl *impl, K key)
Definition: v8-util.h:77
StdMapTraits< K, V >::Impl Impl
Definition: v8-util.h:104