OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 ASSERT(value->IsExternal()); | 44 ASSERT(value->IsExternal()); |
45 T* result = static_cast<T*>(value.As<v8::External>()->Value()); | 45 T* result = static_cast<T*>(value.As<v8::External>()->Value()); |
46 RELEASE_ASSERT(result->m_handle == value); | 46 RELEASE_ASSERT(result->m_handle == value); |
47 return result; | 47 return result; |
48 } | 48 } |
49 | 49 |
50 protected: | 50 protected: |
51 V8GarbageCollected(v8::Isolate* isolate) | 51 V8GarbageCollected(v8::Isolate* isolate) |
52 : m_isolate(isolate) | 52 : m_isolate(isolate) |
53 , m_handle(isolate, v8::External::New(isolate, static_cast<T*>(this))) | 53 , m_handle(isolate, v8::External::New(isolate, static_cast<T*>(this))) |
54 , m_releasedToV8GarbageCollector(false) | |
54 { | 55 { |
55 } | 56 } |
56 | 57 |
57 v8::Handle<v8::External> releaseToV8GarbageCollector() | 58 v8::Handle<v8::External> releaseToV8GarbageCollector() |
58 { | 59 { |
59 ASSERT(!m_handle.isWeak()); // Call this exactly once. | 60 ASSERT(!m_handle.isWeak()); // Call this exactly once. |
61 m_releasedToV8GarbageCollector = true; | |
60 v8::Handle<v8::External> result = m_handle.newLocal(m_isolate); | 62 v8::Handle<v8::External> result = m_handle.newLocal(m_isolate); |
61 m_handle.setWeak(static_cast<T*>(this), &weakCallback); | 63 m_handle.setWeak(static_cast<T*>(this), &weakCallback); |
62 return result; | 64 return result; |
63 } | 65 } |
64 | 66 |
65 ~V8GarbageCollected() | 67 ~V8GarbageCollected() |
66 { | 68 { |
67 ASSERT(m_handle.isEmpty()); | 69 ASSERT(!m_releasedToV8GarbageCollector || m_handle.isEmpty()); |
haraken
2014/06/17 02:46:24
Can we use m_handle.IsWeak() instead of introducin
falken
2014/06/17 03:43:04
No, m_handle.IsWeak() is false until releaseToV8Ga
| |
68 } | 70 } |
69 | 71 |
70 v8::Isolate* isolate() { return m_isolate; } | 72 v8::Isolate* isolate() { return m_isolate; } |
71 | 73 |
72 private: | 74 private: |
73 static void weakCallback(const v8::WeakCallbackData<v8::External, T>& data) | 75 static void weakCallback(const v8::WeakCallbackData<v8::External, T>& data) |
74 { | 76 { |
75 T* self = data.GetParameter(); | 77 T* self = data.GetParameter(); |
76 self->m_handle.clear(); | 78 self->m_handle.clear(); |
77 delete self; | 79 delete self; |
78 } | 80 } |
79 | 81 |
80 v8::Isolate* m_isolate; | 82 v8::Isolate* m_isolate; |
81 ScopedPersistent<v8::External> m_handle; | 83 ScopedPersistent<v8::External> m_handle; |
84 bool m_releasedToV8GarbageCollector; | |
82 }; | 85 }; |
83 | 86 |
84 } // namespace WebCore | 87 } // namespace WebCore |
85 | 88 |
86 #endif // V8GarbageCollected_h | 89 #endif // V8GarbageCollected_h |
OLD | NEW |