Shaka Player Embedded
weak_js_ptr.h
Go to the documentation of this file.
1 // Copyright 2016 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_CORE_WEAK_JS_PTR_H_
16 #define SHAKA_EMBEDDED_CORE_WEAK_JS_PTR_H_
17 
18 #include <glog/logging.h>
19 
20 #include <type_traits>
21 
23 #include "src/memory/heap_tracer.h"
24 #ifdef USING_V8
26 #endif
27 
28 namespace shaka {
29 
43 template <typename T>
44 class WeakJsPtr : public memory::Traceable {
45  public:
46  WeakJsPtr() {}
47 
48  WeakJsPtr(std::nullptr_t) {} // NOLINT(runtime/explicit)
49 
50  template <typename U = T>
51  WeakJsPtr(Handle<U> other) { // NOLINT(runtime/explicit)
52  ResetInternal(other);
53  }
54 
55  WeakJsPtr(const WeakJsPtr& other) = default;
56  WeakJsPtr(WeakJsPtr&& other) = default;
57  WeakJsPtr& operator=(const WeakJsPtr& other) = default;
58  WeakJsPtr& operator=(WeakJsPtr&& other) = default;
59 
60  template <typename U = T>
61  bool operator==(const WeakJsPtr<U>& other) const {
62  return ptr_ == other.ptr_;
63  }
64  template <typename U = T>
65  bool operator==(const Handle<U>& other) const {
66  return ptr_ == other;
67  }
68  template <typename U = T>
69  bool operator!=(const WeakJsPtr<U>& other) const {
70  return ptr_ != other.ptr_;
71  }
72  template <typename U = T>
73  bool operator!=(const Handle<U>& other) const {
74  return ptr_ != other;
75  }
76 
78  bool empty() const {
79 #if defined(USING_V8)
80  return ptr_.IsEmpty();
81 #elif defined(USING_JSC)
82  return !ptr_;
83 #endif
84  }
85 
87  Handle<T> handle() const {
88  CHECK(!empty());
89 #if defined(USING_V8)
90  return ptr_.Get(GetIsolate());
91 #elif defined(USING_JSC)
92  return ptr_;
93 #endif
94  }
95 
97  ReturnVal<JsValue> value() const {
98  return RawToJsValue(handle());
99  }
100 
101  void reset() {
102 #if defined(USING_V8)
103  ptr_.Reset();
104 #elif defined(USING_JSC)
105  ptr_ = nullptr;
106 #endif
107  }
108  template <typename U>
109  void reset(Handle<U> other) {
110  ResetInternal(other);
111  }
112 
113 
114  void Trace(memory::HeapTracer* tracer) const override {
115  if (!empty()) {
116 #if defined(USING_V8)
117 # ifndef NDEBUG
118  // Create a handle scope so the handle local() is freed when this returns.
119  v8::HandleScope handles(GetIsolate());
120  CHECK(empty() || !handle()->IsNumber());
121 # endif
122 
123  static_assert(std::is_convertible<T, v8::Data>::value,
124  "Can only store Data types");
125  auto* v8_trace = static_cast<memory::V8HeapTracer*>(tracer);
126  v8_trace->RegisterEmbedderReference(ptr_.template As<v8::Data>());
127 #endif
128  }
129  }
130 
131  private:
132  template <typename>
133  friend class WeakJsPtr;
134 
135  template <typename U>
136  void ResetInternal(const U& other) {
137 #if defined(USING_V8)
138  // Don't do anything if they are both empty. This allows using WeakJsPtr
139  // in a background thread where GetIsolate() would fail a CHECK.
140  if (ptr_.IsEmpty() && other.IsEmpty())
141  return;
142 
143  ptr_.Reset(GetIsolate(), other);
144 #else
145  ptr_ = other;
146 #endif
147  }
148 
149 #if defined(USING_V8)
150  v8::TracedGlobal<T> ptr_;
151 #elif defined(USING_JSC)
152  Handle<T> ptr_;
153 #endif
154 };
155 
156 } // namespace shaka
157 
158 #endif // SHAKA_EMBEDDED_CORE_WEAK_JS_PTR_H_
bool empty() const
Definition: weak_js_ptr.h:78
WeakJsPtr(Handle< U > other)
Definition: weak_js_ptr.h:51
void Trace(memory::HeapTracer *tracer) const override
Definition: weak_js_ptr.h:114
bool operator==(const WeakJsPtr< U > &other) const
Definition: weak_js_ptr.h:61
bool operator!=(const WeakJsPtr< U > &other) const
Definition: weak_js_ptr.h:69
ReturnVal< JsValue > value() const
Definition: weak_js_ptr.h:97
bool operator!=(const Handle< U > &other) const
Definition: weak_js_ptr.h:73
ReturnVal< JsValue > RawToJsValue(Handle< T > source)
Definition: js_wrappers.h:405
bool operator==(const Handle< U > &other) const
Definition: weak_js_ptr.h:65
WeakJsPtr & operator=(const WeakJsPtr &other)=default
WeakJsPtr(std::nullptr_t)
Definition: weak_js_ptr.h:48
Handle< T > handle() const
Definition: weak_js_ptr.h:87
v8::Isolate * GetIsolate()
Definition: v8_utils.cc:27
void reset(Handle< U > other)
Definition: weak_js_ptr.h:109