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 10 matching lines...) Expand all Loading... |
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 SharedPersistent_h | 31 #ifndef GarbageCollected_h |
32 #define SharedPersistent_h | 32 #define GarbageCollected_h |
33 | 33 |
34 #include "bindings/v8/ScopedPersistent.h" | 34 #include "bindings/v8/ScopedPersistent.h" |
35 #include <v8.h> | |
36 #include "wtf/PassRefPtr.h" | |
37 #include "wtf/RefCounted.h" | |
38 | 35 |
39 namespace WebCore { | 36 namespace WebCore { |
40 | 37 |
41 template <typename T> | 38 template<typename T> |
42 class SharedPersistent : public RefCounted<SharedPersistent<T> > { | 39 class GarbageCollected { |
43 WTF_MAKE_NONCOPYABLE(SharedPersistent); | 40 WTF_MAKE_NONCOPYABLE(GarbageCollected); |
44 public: | 41 public: |
45 static PassRefPtr<SharedPersistent<T> > create(v8::Handle<T> value) | 42 static T* Cast(v8::Handle<v8::Value> value) |
46 { | 43 { |
47 return adoptRef(new SharedPersistent<T>(value)); | 44 ASSERT(value->IsExternal()); |
48 } | 45 T* result = static_cast<T*>(value.As<v8::External>()->Value()); |
| 46 RELEASE_ASSERT(result->m_handle == value); |
| 47 return result; |
| 48 } |
49 | 49 |
50 v8::Local<T> newLocal(v8::Isolate* isolate) const | 50 protected: |
51 { | 51 GarbageCollected() : m_handle(v8::External::New(static_cast<T*>(this))) { } |
52 return m_value.newLocal(isolate); | |
53 } | |
54 | 52 |
55 bool isEmpty() { return m_value.isEmpty(); } | 53 v8::Handle<v8::External> releaseToGarbageCollector() |
| 54 { |
| 55 ASSERT(!m_handle.isWeak()); // Call releaseToGarbageCollector exactly on
ce. |
| 56 v8::Handle<v8::External> result = m_handle.newLocal(v8::Isolate::GetCurr
ent()); |
| 57 m_handle.makeWeak(static_cast<T*>(this), weakCallback); |
| 58 return result; |
| 59 } |
56 | 60 |
57 bool operator==(const SharedPersistent<T>& other) | 61 ~GarbageCollected() |
58 { | 62 { |
59 return m_value == other.m_value; | 63 ASSERT(m_handle.isEmpty()); |
60 } | 64 } |
61 | 65 |
62 private: | 66 private: |
63 explicit SharedPersistent(v8::Handle<T> value) : m_value(value) { } | 67 static void weakCallback(v8::Isolate* isolate, v8::Persistent<v8::External>*
handle, T* self) |
64 ScopedPersistent<T> m_value; | 68 { |
65 }; | 69 self->m_handle.clear(); |
| 70 delete self; |
| 71 } |
| 72 |
| 73 ScopedPersistent<v8::External> m_handle; |
| 74 }; |
66 | 75 |
67 } // namespace WebCore | 76 } // namespace WebCore |
68 | 77 |
69 #endif // SharedPersistent_h | 78 #endif // GarbageCollected_h |
OLD | NEW |