Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1322)

Side by Side Diff: Source/bindings/v8/GarbageCollected.h

Issue 22470008: Implement ScriptPromise and ScriptFunction (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Protect the ScriptFunction with a strong ref Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 10 matching lines...) Expand all
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
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 GarbageCollected_h
32 #define ScopedPersistent_h 32 #define GarbageCollected_h
33 33
34 #include "wtf/Noncopyable.h"
34 #include <v8.h> 35 #include <v8.h>
35 #include "wtf/Noncopyable.h"
36 36
37 namespace WebCore { 37 namespace WebCore {
38 38
39 template<typename T> 39 template<typename T>
40 class ScopedPersistent { 40 class GarbageCollected {
abarth-chromium 2013/08/15 00:22:11 Oh, sorry. I meant that GarbageCollected would ha
41 WTF_MAKE_NONCOPYABLE(ScopedPersistent); 41 WTF_MAKE_NONCOPYABLE(GarbageCollected);
42 public: 42 public:
43 ScopedPersistent() { } 43 GarbageCollected() { }
44 44
45 explicit ScopedPersistent(v8::Handle<T> handle) 45 explicit GarbageCollected(v8::Handle<T> handle)
46 : m_handle(v8::Isolate::GetCurrent(), handle) 46 : m_handle(v8::Isolate::GetCurrent(), handle)
47 { 47 {
48 } 48 }
49 49
50 ~ScopedPersistent() 50 ~GarbageCollected()
51 { 51 {
52 clear(); 52 clear();
53 } 53 }
54 54
55 ALWAYS_INLINE v8::Local<T> newLocal(v8::Isolate* isolate) const 55 ALWAYS_INLINE v8::Local<T> newLocal(v8::Isolate* isolate) const
56 { 56 {
57 return v8::Local<T>::New(isolate, m_handle); 57 return v8::Local<T>::New(isolate, m_handle);
58 } 58 }
59 59
60 template<typename P>
61 void makeWeak(P* parameters, void (*callback)(v8::Isolate*, v8::Persistent<T >*, P*))
62 {
63 m_handle.MakeWeak(parameters, callback);
64 }
65
66 bool isEmpty() const { return m_handle.IsEmpty(); } 60 bool isEmpty() const { return m_handle.IsEmpty(); }
67 61
68 void set(v8::Isolate* isolate, v8::Handle<T> handle) 62 void set(v8::Isolate* isolate, v8::Handle<T> handle)
69 { 63 {
70 m_handle.Reset(isolate, handle); 64 m_handle.Reset(isolate, handle);
65 m_handle.MakeWeak(this, &weakCallback);
71 } 66 }
72
73 // Note: This is clear in the OwnPtr sense, not the v8::Handle sense. 67 // Note: This is clear in the OwnPtr sense, not the v8::Handle sense.
74 void clear() 68 void clear()
75 { 69 {
76 if (m_handle.IsEmpty()) 70 if (m_handle.IsEmpty())
77 return; 71 return;
78 m_handle.Dispose(); 72 m_handle.Dispose();
79 m_handle.Clear(); 73 m_handle.Clear();
80 } 74 }
81 75
82 bool operator==(const ScopedPersistent<T>& other) 76 bool operator==(const GarbageCollected<T>& other)
83 { 77 {
84 return m_handle == other.m_handle; 78 return m_handle == other.m_handle;
85 } 79 }
86 80
87 private: 81 private:
88 // FIXME: This function does an unsafe handle access. Remove it. 82 static void weakCallback(v8::Isolate*, v8::Persistent<T>*, GarbageCollected< T>* self)
89 friend class V8AbstractEventListener;
90 friend class V8PerIsolateData;
91 ALWAYS_INLINE v8::Persistent<T>& getUnsafe()
92 { 83 {
93 return m_handle; 84 self->clear();
94 } 85 }
95 86
96 v8::Persistent<T> m_handle; 87 v8::Persistent<T> m_handle;
97 }; 88 };
98 89
99 } // namespace WebCore 90 } // namespace WebCore
100 91
101 #endif // ScopedPersistent_h 92 #endif // GarbageCollected_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698