Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef ScopedPersistent_h | 31 #ifndef ScopedPersistent_h |
| 32 #define ScopedPersistent_h | 32 #define ScopedPersistent_h |
| 33 | 33 |
| 34 #include "bindings/core/v8/ScriptWrappableVisitor.h" | |
| 34 #include "wtf/Allocator.h" | 35 #include "wtf/Allocator.h" |
| 35 #include "wtf/Noncopyable.h" | 36 #include "wtf/Noncopyable.h" |
| 36 #include <memory> | 37 #include <memory> |
| 37 #include <v8.h> | 38 #include <v8.h> |
| 38 | 39 |
| 39 namespace blink { | 40 namespace blink { |
| 40 | 41 |
| 41 template <typename T> | 42 template <typename T> |
| 42 class ScopedPersistent { | 43 class ScopedPersistent { |
| 43 USING_FAST_MALLOC(ScopedPersistent); | 44 USING_FAST_MALLOC(ScopedPersistent); |
| 44 WTF_MAKE_NONCOPYABLE(ScopedPersistent); | 45 WTF_MAKE_NONCOPYABLE(ScopedPersistent); |
| 45 | 46 |
| 46 public: | 47 public: |
| 47 ScopedPersistent() {} | 48 ScopedPersistent() {} |
| 48 | 49 |
| 49 ScopedPersistent(v8::Isolate* isolate, v8::Local<T> handle) | 50 ScopedPersistent(v8::Isolate* isolate, v8::Local<T> handle) |
| 50 : m_handle(isolate, handle) {} | 51 : m_handle(isolate, handle) {} |
| 51 | 52 |
| 52 ScopedPersistent(v8::Isolate* isolate, v8::MaybeLocal<T> maybe) { | 53 ScopedPersistent(v8::Isolate* isolate, v8::MaybeLocal<T> maybe) { |
| 53 v8::Local<T> local; | 54 v8::Local<T> local; |
| 54 if (maybe.ToLocal(&local)) | 55 if (maybe.ToLocal(&local)) |
| 55 m_handle.Reset(isolate, local); | 56 m_handle.Reset(isolate, local); |
| 56 } | 57 } |
| 57 | 58 |
| 58 ~ScopedPersistent() { clear(); } | 59 virtual ~ScopedPersistent() { clear(); } |
| 59 | 60 |
| 60 ALWAYS_INLINE v8::Local<T> newLocal(v8::Isolate* isolate) const { | 61 ALWAYS_INLINE v8::Local<T> newLocal(v8::Isolate* isolate) const { |
| 61 return v8::Local<T>::New(isolate, m_handle); | 62 return v8::Local<T>::New(isolate, m_handle); |
| 62 } | 63 } |
| 63 | 64 |
| 64 // If you don't need to get weak callback, use setPhantom instead. | 65 // If you don't need to get weak callback, use setPhantom instead. |
| 65 // setPhantom is faster than setWeak. | 66 // setPhantom is faster than setWeak. |
| 66 template <typename P> | 67 template <typename P> |
| 67 void setWeak(P* parameters, | 68 void setWeak(P* parameters, |
| 68 void (*callback)(const v8::WeakCallbackInfo<P>&), | 69 void (*callback)(const v8::WeakCallbackInfo<P>&), |
| 69 v8::WeakCallbackType type = v8::WeakCallbackType::kParameter) { | 70 v8::WeakCallbackType type = v8::WeakCallbackType::kParameter) { |
| 70 m_handle.SetWeak(parameters, callback, type); | 71 m_handle.SetWeak(parameters, callback, type); |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Turns this handle into a weak phantom handle without | 74 // Turns this handle into a weak phantom handle without |
| 74 // finalization callback. | 75 // finalization callback. |
| 75 void setPhantom() { m_handle.SetWeak(); } | 76 void setPhantom() { m_handle.SetWeak(); } |
| 76 | 77 |
| 77 void clearWeak() { m_handle.template ClearWeak<void>(); } | 78 void clearWeak() { m_handle.template ClearWeak<void>(); } |
| 78 | 79 |
| 79 bool isEmpty() const { return m_handle.IsEmpty(); } | 80 bool isEmpty() const { return m_handle.IsEmpty(); } |
| 80 bool isWeak() const { return m_handle.IsWeak(); } | 81 bool isWeak() const { return m_handle.IsWeak(); } |
| 81 | 82 |
| 82 void set(v8::Isolate* isolate, v8::Local<T> handle) { | 83 virtual void set(v8::Isolate* isolate, v8::Local<T> handle) { |
| 83 m_handle.Reset(isolate, handle); | 84 m_handle.Reset(isolate, handle); |
| 84 } | 85 } |
| 85 | 86 |
| 86 // Note: This is clear in the std::unique_ptr sense, not the v8::Handle sense. | 87 // Note: This is clear in the std::unique_ptr sense, not the v8::Handle sense. |
| 87 void clear() { m_handle.Reset(); } | 88 void clear() { m_handle.Reset(); } |
| 88 | 89 |
| 89 void setReference(const v8::Persistent<v8::Object>& parent, | 90 void setReference(const v8::Persistent<v8::Object>& parent, |
| 90 v8::Isolate* isolate) { | 91 v8::Isolate* isolate) { |
| 91 isolate->SetReference(parent, m_handle); | 92 isolate->SetReference(parent, m_handle); |
| 92 } | 93 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 105 template <typename S> | 106 template <typename S> |
| 106 const ScopedPersistent<S>& cast() const { | 107 const ScopedPersistent<S>& cast() const { |
| 107 return reinterpret_cast<const ScopedPersistent<v8::Object>&>( | 108 return reinterpret_cast<const ScopedPersistent<v8::Object>&>( |
| 108 const_cast<const ScopedPersistent<T>&>(*this)); | 109 const_cast<const ScopedPersistent<T>&>(*this)); |
| 109 } | 110 } |
| 110 | 111 |
| 111 private: | 112 private: |
| 112 v8::Persistent<T> m_handle; | 113 v8::Persistent<T> m_handle; |
| 113 }; | 114 }; |
| 114 | 115 |
| 116 /** | |
| 117 * TraceWrapperScopedPersistent is used to trace from Blink to V8. | |
| 118 * | |
| 119 * TODO(mlippautz): Once shipped, create a separate type with a more | |
| 120 * appropriate handle type than v8::Persistent. The handle should have regular | |
|
haraken
2016/11/03 05:55:25
I'm just curious but how hard would it be to imple
Michael Lippautz
2016/11/03 08:59:53
What I have in mind is a new handle type on the V8
| |
| 121 * tracing semantics, i.e., only hold strongly on to an object if reached | |
| 122 * trough tracing. | |
| 123 */ | |
| 124 template <typename T> | |
| 125 class TraceWrapperScopedPersistent : public ScopedPersistent<T> { | |
|
Michael Lippautz
2016/11/02 16:00:03
The name doesn't really fit. I am all open for any
Marcel Hlopko
2016/11/02 16:44:51
TracedV8Reference? It's neither scoped nor persist
Michael Lippautz
2016/11/02 17:07:55
+1 for TracedV8Reference. haraken?
haraken
2016/11/03 05:55:25
I want to keep a "TraceWrapper" prefix at least. T
Michael Lippautz
2016/11/03 08:59:53
Done.
| |
| 126 public: | |
| 127 TraceWrapperScopedPersistent(void* parent) | |
| 128 : ScopedPersistent<T>(), m_parent(parent) {} | |
| 129 | |
| 130 TraceWrapperScopedPersistent(void* parent, | |
| 131 v8::Isolate* isolate, | |
| 132 v8::Local<T> handle) | |
| 133 : ScopedPersistent<T>(isolate, handle), m_parent(parent) {} | |
| 134 | |
| 135 TraceWrapperScopedPersistent(void* parent, | |
| 136 v8::Isolate* isolate, | |
| 137 v8::MaybeLocal<T> maybe) | |
| 138 : ScopedPersistent<T>(isolate, maybe), m_parent(parent) {} | |
| 139 | |
| 140 void set(v8::Isolate* isolate, v8::Local<T> handle) override { | |
| 141 ScriptWrappableVisitor::writeBarrier(m_parent, this); | |
| 142 ScopedPersistent<T>::set(isolate, handle); | |
| 143 } | |
| 144 | |
| 145 template <typename S> | |
| 146 const TraceWrapperScopedPersistent<S>& cast() const { | |
| 147 return reinterpret_cast<const TraceWrapperScopedPersistent<S>&>( | |
| 148 const_cast<const TraceWrapperScopedPersistent<T>&>(*this)); | |
| 149 } | |
| 150 | |
| 151 private: | |
| 152 void* m_parent; | |
| 153 }; | |
| 154 | |
| 115 } // namespace blink | 155 } // namespace blink |
| 116 | 156 |
| 117 #endif // ScopedPersistent_h | 157 #endif // ScopedPersistent_h |
| OLD | NEW |