v8  6.2.414(node8.16.0)
V8 is Google's open source JavaScript engine
v8-util.h
Go to the documentation of this file.
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.
4 
5 #ifndef V8_UTIL_H_
6 #define V8_UTIL_H_
7 
8 #include "v8.h" // NOLINT(build/include)
9 #include <assert.h>
10 #include <map>
11 #include <vector>
12 
20 namespace v8 {
21 
22 typedef uintptr_t PersistentContainerValue;
23 static const uintptr_t kPersistentContainerNotFound = 0;
26  // These correspond to v8::WeakCallbackType
29  kWeak = kWeakWithParameter // For backwards compatibility. Deprecate.
30 };
31 
32 
39 template<typename K, typename V>
40 class StdMapTraits {
41  public:
42  // STL map & related:
43  typedef std::map<K, PersistentContainerValue> Impl;
44  typedef typename Impl::iterator Iterator;
45 
46  static bool Empty(Impl* impl) { return impl->empty(); }
47  static size_t Size(Impl* impl) { return impl->size(); }
48  static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT
49  static Iterator Begin(Impl* impl) { return impl->begin(); }
50  static Iterator End(Impl* impl) { return impl->end(); }
51  static K Key(Iterator it) { return it->first; }
52  static PersistentContainerValue Value(Iterator it) { return it->second; }
53  static PersistentContainerValue Set(Impl* impl, K key,
55  std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value));
56  PersistentContainerValue old_value = kPersistentContainerNotFound;
57  if (!res.second) {
58  old_value = res.first->second;
59  res.first->second = value;
60  }
61  return old_value;
62  }
63  static PersistentContainerValue Get(Impl* impl, K key) {
64  Iterator it = impl->find(key);
65  if (it == impl->end()) return kPersistentContainerNotFound;
66  return it->second;
67  }
68  static PersistentContainerValue Remove(Impl* impl, K key) {
69  Iterator it = impl->find(key);
70  if (it == impl->end()) return kPersistentContainerNotFound;
71  PersistentContainerValue value = it->second;
72  impl->erase(it);
73  return value;
74  }
75 };
76 
77 
86 template<typename K, typename V>
88  public:
89  // Weak callback & friends:
93  typedef void WeakCallbackDataType;
94 
96  MapType* map, const K& key, Local<V> value) {
97  return NULL;
98  }
101  return NULL;
102  }
105  return K();
106  }
108  static void Dispose(Isolate* isolate, Global<V> value, K key) {}
109 };
110 
111 
112 template <typename K, typename V>
113 class DefaultGlobalMapTraits : public StdMapTraits<K, V> {
114  private:
115  template <typename T>
116  struct RemovePointer;
117 
118  public:
119  // Weak callback & friends:
122  typedef void WeakCallbackDataType;
123 
125  Local<V> value) {
126  return nullptr;
127  }
130  return nullptr;
131  }
134  return K();
135  }
137  static void OnWeakCallback(
139  static void Dispose(Isolate* isolate, Global<V> value, K key) {}
140  // This is a second pass callback, so SetSecondPassCallback cannot be called.
142 
143  private:
144  template <typename T>
145  struct RemovePointer<T*> {
146  typedef T Type;
147  };
148 };
149 
150 
161 template <typename K, typename V, typename Traits>
163  public:
164  Isolate* GetIsolate() { return isolate_; }
165 
169  size_t Size() { return Traits::Size(&impl_); }
170 
174  bool IsWeak() { return Traits::kCallbackType != kNotWeak; }
175 
179  Local<V> Get(const K& key) {
180  return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
181  }
182 
186  bool Contains(const K& key) {
187  return Traits::Get(&impl_, key) != kPersistentContainerNotFound;
188  }
189 
194  bool SetReturnValue(const K& key,
195  ReturnValue<Value> returnValue) {
196  return SetReturnValueFromVal(&returnValue, Traits::Get(&impl_, key));
197  }
198 
202  void SetReference(const K& key,
203  const Persistent<Object>& parent) {
204  GetIsolate()->SetReference(
205  reinterpret_cast<internal::Object**>(parent.val_),
206  reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))));
207  }
208 
214  assert(Contains(key));
215  V8::RegisterExternallyReferencedObject(
216  reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key))),
217  reinterpret_cast<internal::Isolate*>(GetIsolate()));
218  }
219 
223  Global<V> Remove(const K& key) {
224  return Release(Traits::Remove(&impl_, key)).Pass();
225  }
226 
231  void Clear() {
232  typedef typename Traits::Iterator It;
233  HandleScope handle_scope(isolate_);
234  // TODO(dcarney): figure out if this swap and loop is necessary.
235  while (!Traits::Empty(&impl_)) {
236  typename Traits::Impl impl;
237  Traits::Swap(impl_, impl);
238  for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
239  Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(),
240  Traits::Key(i));
241  }
242  }
243  }
244 
250  public:
251  PersistentValueReference() : value_(kPersistentContainerNotFound) { }
253  : value_(other.value_) { }
254 
256  return Local<V>::New(isolate, FromVal(value_));
257  }
258  bool IsEmpty() const {
259  return value_ == kPersistentContainerNotFound;
260  }
261  template<typename T>
262  bool SetReturnValue(ReturnValue<T> returnValue) {
263  return SetReturnValueFromVal(&returnValue, value_);
264  }
265  void Reset() {
266  value_ = kPersistentContainerNotFound;
267  }
268  void operator=(const PersistentValueReference& other) {
269  value_ = other.value_;
270  }
271 
272  private:
274  friend class PersistentValueMap<K, V, Traits>;
275  friend class GlobalValueMap<K, V, Traits>;
276 
278  : value_(value) { }
279 
280  void operator=(PersistentContainerValue value) {
281  value_ = value;
282  }
283 
285  };
286 
298  return PersistentValueReference(Traits::Get(&impl_, key));
299  }
300 
301  protected:
302  explicit PersistentValueMapBase(Isolate* isolate) : isolate_(isolate) {}
303 
305 
306  Isolate* isolate() { return isolate_; }
307  typename Traits::Impl* impl() { return &impl_; }
308 
310  return reinterpret_cast<V*>(v);
311  }
312 
314  V* v = persistent->val_;
315  persistent->val_ = 0;
316  return reinterpret_cast<PersistentContainerValue>(v);
317  }
318 
320  return reinterpret_cast<PersistentContainerValue>(persistent->val_);
321  }
322 
329  Global<V> p;
330  p.val_ = FromVal(v);
331  if (Traits::kCallbackType != kNotWeak && p.IsWeak()) {
332  Traits::DisposeCallbackData(
333  p.template ClearWeak<typename Traits::WeakCallbackDataType>());
334  }
335  return p.Pass();
336  }
337 
338  void RemoveWeak(const K& key) {
339  Global<V> p;
340  p.val_ = FromVal(Traits::Remove(&impl_, key));
341  p.Reset();
342  }
343 
344  private:
346  void operator=(PersistentValueMapBase&);
347 
348  static bool SetReturnValueFromVal(ReturnValue<Value>* returnValue,
349  PersistentContainerValue value) {
350  bool hasValue = value != kPersistentContainerNotFound;
351  if (hasValue) {
352  returnValue->SetInternal(
353  *reinterpret_cast<internal::Object**>(FromVal(value)));
354  }
355  return hasValue;
356  }
357 
358  Isolate* isolate_;
359  typename Traits::Impl impl_;
360 };
361 
362 
363 template <typename K, typename V, typename Traits>
364 class PersistentValueMap : public PersistentValueMapBase<K, V, Traits> {
365  public:
367  : PersistentValueMapBase<K, V, Traits>(isolate) {}
368 
369  typedef
372 
378  Global<V> Set(const K& key, Local<V> value) {
379  Global<V> persistent(this->isolate(), value);
380  return SetUnique(key, &persistent);
381  }
382 
386  Global<V> Set(const K& key, Global<V> value) {
387  return SetUnique(key, &value);
388  }
389 
394  Global<V> SetUnique(const K& key, Global<V>* persistent) {
395  if (Traits::kCallbackType != kNotWeak) {
396  Local<V> value(Local<V>::New(this->isolate(), *persistent));
397  persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
398  Traits::WeakCallbackParameter(this, key, value), WeakCallback);
399  }
400  PersistentContainerValue old_value =
401  Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
402  return this->Release(old_value).Pass();
403  }
404 
409  Global<V> Set(const K& key, Global<V> value,
410  PersistentValueReference* reference) {
411  *reference = this->Leak(&value);
412  return SetUnique(key, &value);
413  }
414 
415  private:
416  static void WeakCallback(
418  if (Traits::kCallbackType != kNotWeak) {
419  PersistentValueMap<K, V, Traits>* persistentValueMap =
420  Traits::MapFromWeakCallbackInfo(data);
421  K key = Traits::KeyFromWeakCallbackInfo(data);
422  Traits::Dispose(data.GetIsolate(),
423  persistentValueMap->Remove(key).Pass(), key);
424  Traits::DisposeCallbackData(data.GetParameter());
425  }
426  }
427 };
428 
429 
430 template <typename K, typename V, typename Traits>
431 class GlobalValueMap : public PersistentValueMapBase<K, V, Traits> {
432  public:
434  : PersistentValueMapBase<K, V, Traits>(isolate) {}
435 
436  typedef
439 
445  Global<V> Set(const K& key, Local<V> value) {
446  Global<V> persistent(this->isolate(), value);
447  return SetUnique(key, &persistent);
448  }
449 
453  Global<V> Set(const K& key, Global<V> value) {
454  return SetUnique(key, &value);
455  }
456 
461  Global<V> SetUnique(const K& key, Global<V>* persistent) {
462  if (Traits::kCallbackType != kNotWeak) {
463  WeakCallbackType callback_type =
464  Traits::kCallbackType == kWeakWithInternalFields
467  Local<V> value(Local<V>::New(this->isolate(), *persistent));
468  persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
469  Traits::WeakCallbackParameter(this, key, value), OnWeakCallback,
470  callback_type);
471  }
472  PersistentContainerValue old_value =
473  Traits::Set(this->impl(), key, this->ClearAndLeak(persistent));
474  return this->Release(old_value).Pass();
475  }
476 
481  Global<V> Set(const K& key, Global<V> value,
482  PersistentValueReference* reference) {
483  *reference = this->Leak(&value);
484  return SetUnique(key, &value);
485  }
486 
487  private:
488  static void OnWeakCallback(
490  if (Traits::kCallbackType != kNotWeak) {
491  auto map = Traits::MapFromWeakCallbackInfo(data);
492  K key = Traits::KeyFromWeakCallbackInfo(data);
493  map->RemoveWeak(key);
494  Traits::OnWeakCallback(data);
495  data.SetSecondPassCallback(SecondWeakCallback);
496  }
497  }
498 
499  static void SecondWeakCallback(
500  const WeakCallbackInfo<typename Traits::WeakCallbackDataType>& data) {
501  Traits::DisposeWeak(data);
502  }
503 };
504 
505 
513 template<typename K, typename V,
514  typename Traits = DefaultPersistentValueMapTraits<K, V> >
515 class StdPersistentValueMap : public PersistentValueMap<K, V, Traits> {
516  public:
518  : PersistentValueMap<K, V, Traits>(isolate) {}
519 };
520 
521 
529 template <typename K, typename V,
530  typename Traits = DefaultGlobalMapTraits<K, V> >
531 class StdGlobalValueMap : public GlobalValueMap<K, V, Traits> {
532  public:
534  : GlobalValueMap<K, V, Traits>(isolate) {}
535 };
536 
537 
539  public:
540  typedef std::vector<PersistentContainerValue> Impl;
541 
542  static void Append(Impl* impl, PersistentContainerValue value) {
543  impl->push_back(value);
544  }
545  static bool IsEmpty(const Impl* impl) {
546  return impl->empty();
547  }
548  static size_t Size(const Impl* impl) {
549  return impl->size();
550  }
551  static PersistentContainerValue Get(const Impl* impl, size_t i) {
552  return (i < impl->size()) ? impl->at(i) : kPersistentContainerNotFound;
553  }
554  static void ReserveCapacity(Impl* impl, size_t capacity) {
555  impl->reserve(capacity);
556  }
557  static void Clear(Impl* impl) {
558  impl->clear();
559  }
560 };
561 
562 
573 template<typename V, typename Traits = DefaultPersistentValueVectorTraits>
575  public:
576  explicit PersistentValueVector(Isolate* isolate) : isolate_(isolate) { }
577 
579  Clear();
580  }
581 
585  void Append(Local<V> value) {
586  Global<V> persistent(isolate_, value);
587  Traits::Append(&impl_, ClearAndLeak(&persistent));
588  }
589 
593  void Append(Global<V> persistent) {
594  Traits::Append(&impl_, ClearAndLeak(&persistent));
595  }
596 
600  bool IsEmpty() const {
601  return Traits::IsEmpty(&impl_);
602  }
603 
607  size_t Size() const {
608  return Traits::Size(&impl_);
609  }
610 
614  Local<V> Get(size_t index) const {
615  return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, index)));
616  }
617 
621  void Clear() {
622  size_t length = Traits::Size(&impl_);
623  for (size_t i = 0; i < length; i++) {
624  Global<V> p;
625  p.val_ = FromVal(Traits::Get(&impl_, i));
626  }
627  Traits::Clear(&impl_);
628  }
629 
634  void ReserveCapacity(size_t capacity) {
635  Traits::ReserveCapacity(&impl_, capacity);
636  }
637 
638  private:
639  static PersistentContainerValue ClearAndLeak(Global<V>* persistent) {
640  V* v = persistent->val_;
641  persistent->val_ = 0;
642  return reinterpret_cast<PersistentContainerValue>(v);
643  }
644 
645  static V* FromVal(PersistentContainerValue v) {
646  return reinterpret_cast<V*>(v);
647  }
648 
649  Isolate* isolate_;
650  typename Traits::Impl impl_;
651 };
652 
653 } // namespace v8
654 
655 #endif // V8_UTIL_H
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:99
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:386
Definition: v8.h:121
static void Clear(Impl *impl)
Definition: v8-util.h:557
PersistentValueVector(Isolate *isolate)
Definition: v8-util.h:576
static MapType * MapFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:128
static size_t Size(const Impl *impl)
Definition: v8-util.h:548
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:394
static void OnWeakCallback(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:137
V8_INLINE T * GetParameter() const
Definition: v8.h:428
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:378
StdGlobalValueMap(Isolate *isolate)
Definition: v8-util.h:533
static PersistentContainerValue Remove(Impl *impl, K key)
Definition: v8-util.h:68
static PersistentContainerValue Leak(Global< V > *persistent)
Definition: v8-util.h:319
static PersistentContainerValue ClearAndLeak(Global< V > *persistent)
Definition: v8-util.h:313
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:481
Global< V > SetUnique(const K &key, Global< V > *persistent)
Definition: v8-util.h:461
PersistentValueReference GetReference(const K &key)
Definition: v8-util.h:297
static size_t Size(Impl *impl)
Definition: v8-util.h:47
static bool IsEmpty(const Impl *impl)
Definition: v8-util.h:545
static const PersistentContainerCallbackType kCallbackType
Definition: v8-util.h:90
static void Append(Impl *impl, PersistentContainerValue value)
Definition: v8-util.h:542
void ReserveCapacity(size_t capacity)
Definition: v8-util.h:634
std::vector< PersistentContainerValue > Impl
Definition: v8-util.h:540
static void ReserveCapacity(Impl *impl, size_t capacity)
Definition: v8-util.h:554
static void DisposeWeak(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:141
static V * FromVal(PersistentContainerValue v)
Definition: v8-util.h:309
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Definition: v8-util.h:438
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
Definition: v8-util.h:124
static void Dispose(Isolate *isolate, Global< V > value, K key)
Definition: v8-util.h:108
Global< V > Remove(const K &key)
Definition: v8-util.h:223
static Global< V > Release(PersistentContainerValue v)
Definition: v8-util.h:328
bool Contains(const K &key)
Definition: v8-util.h:186
PersistentValueMap(Isolate *isolate)
Definition: v8-util.h:366
GlobalValueMap< K, V, DefaultGlobalMapTraits< K, V > > MapType
Definition: v8-util.h:121
PersistentContainerCallbackType
Definition: v8-util.h:24
static bool Empty(Impl *impl)
Definition: v8-util.h:46
static PersistentContainerValue Get(const Impl *impl, size_t i)
Definition: v8-util.h:551
uintptr_t PersistentContainerValue
Definition: v8-util.h:22
void RegisterExternallyReferencedObject(K &key)
Definition: v8-util.h:213
Definition: libplatform.h:12
static const PersistentContainerCallbackType kCallbackType
Definition: v8-util.h:120
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:103
static K Key(Iterator it)
Definition: v8-util.h:51
bool SetReturnValue(ReturnValue< T > returnValue)
Definition: v8-util.h:262
static Iterator Begin(Impl *impl)
Definition: v8-util.h:49
V8_INLINE Isolate * GetIsolate() const
Definition: v8.h:427
void SetReference(const K &key, const Persistent< Object > &parent)
Definition: v8-util.h:202
static Iterator End(Impl *impl)
Definition: v8-util.h:50
bool SetReturnValue(const K &key, ReturnValue< Value > returnValue)
Definition: v8-util.h:194
static void Dispose(Isolate *isolate, Global< V > value, K key)
Definition: v8-util.h:139
Local< V > Get(size_t index) const
Definition: v8-util.h:614
PersistentValueMapBase< K, V, Traits >::PersistentValueReference PersistentValueReference
Definition: v8-util.h:371
Global Pass()
Definition: v8.h:820
V8_INLINE bool IsWeak() const
Definition: v8.h:9229
PersistentValueReference(const PersistentValueReference &other)
Definition: v8-util.h:252
StdPersistentValueMap(Isolate *isolate)
Definition: v8-util.h:517
Local< V > Get(const K &key)
Definition: v8-util.h:179
bool IsEmpty() const
Definition: v8-util.h:600
Global< V > Set(const K &key, Global< V > value, PersistentValueReference *reference)
Definition: v8-util.h:409
Isolate * GetIsolate()
Definition: v8-util.h:164
void SetSecondPassCallback(Callback callback) const
Definition: v8.h:451
Global< V > Set(const K &key, Local< V > value)
Definition: v8-util.h:445
static void DisposeCallbackData(WeakCallbackDataType *data)
Definition: v8-util.h:107
Impl::iterator Iterator
Definition: v8-util.h:44
V8_INLINE void Reset()
Definition: v8.h:9238
static PersistentContainerValue Value(Iterator it)
Definition: v8-util.h:52
size_t Size() const
Definition: v8-util.h:607
Traits::Impl * impl()
Definition: v8-util.h:307
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8.h:9132
void operator=(const PersistentValueReference &other)
Definition: v8-util.h:268
Local< V > NewLocal(Isolate *isolate) const
Definition: v8-util.h:255
void RemoveWeak(const K &key)
Definition: v8-util.h:338
static PersistentContainerValue Set(Impl *impl, K key, PersistentContainerValue value)
Definition: v8-util.h:53
static K KeyFromWeakCallbackInfo(const WeakCallbackInfo< WeakCallbackDataType > &data)
Definition: v8-util.h:132
PersistentValueMapBase(Isolate *isolate)
Definition: v8-util.h:302
std::map< K, PersistentContainerValue > Impl
Definition: v8-util.h:43
static void Swap(Impl &a, Impl &b)
Definition: v8-util.h:48
void Append(Local< V > value)
Definition: v8-util.h:585
GlobalValueMap(Isolate *isolate)
Definition: v8-util.h:433
Definition: v8.h:112
WeakCallbackType
Definition: v8.h:466
PersistentValueMap< K, V, DefaultPersistentValueMapTraits< K, V > > MapType
Definition: v8-util.h:92
void Append(Global< V > persistent)
Definition: v8-util.h:593
Global< V > Set(const K &key, Global< V > value)
Definition: v8-util.h:453
static PersistentContainerValue Get(Impl *impl, K key)
Definition: v8-util.h:63
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, const K &key, Local< V > value)
Definition: v8-util.h:95
static void DisposeCallbackData(WeakCallbackDataType *data)
Definition: v8-util.h:136