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

Unified Diff: Source/bindings/v8/GarbageCollected.h

Issue 23254004: Implement ScriptPromise and ScriptFunction (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Control shouldn't reach the end of a non-void function 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 side-by-side diff with in-line comments
Download patch
Index: Source/bindings/v8/GarbageCollected.h
diff --git a/Source/bindings/v8/SharedPersistent.h b/Source/bindings/v8/GarbageCollected.h
similarity index 59%
copy from Source/bindings/v8/SharedPersistent.h
copy to Source/bindings/v8/GarbageCollected.h
index 52cc1196f348e14168c6fbaf9332d475d76e5055..c21f421fac2b3ce52c0818d9b71de753bce0188d 100644
--- a/Source/bindings/v8/SharedPersistent.h
+++ b/Source/bindings/v8/GarbageCollected.h
@@ -28,42 +28,51 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#ifndef SharedPersistent_h
-#define SharedPersistent_h
+#ifndef GarbageCollected_h
+#define GarbageCollected_h
#include "bindings/v8/ScopedPersistent.h"
-#include <v8.h>
-#include "wtf/PassRefPtr.h"
-#include "wtf/RefCounted.h"
namespace WebCore {
- template <typename T>
- class SharedPersistent : public RefCounted<SharedPersistent<T> > {
- WTF_MAKE_NONCOPYABLE(SharedPersistent);
- public:
- static PassRefPtr<SharedPersistent<T> > create(v8::Handle<T> value)
- {
- return adoptRef(new SharedPersistent<T>(value));
- }
+template<typename T>
+class GarbageCollected {
+ WTF_MAKE_NONCOPYABLE(GarbageCollected);
+public:
+ static T* Cast(v8::Handle<v8::Value> value)
+ {
+ ASSERT(value->IsExternal());
+ T* result = static_cast<T*>(value.As<v8::External>()->Value());
+ RELEASE_ASSERT(result->m_handle == value);
+ return result;
+ }
- v8::Local<T> newLocal(v8::Isolate* isolate) const
- {
- return m_value.newLocal(isolate);
- }
+protected:
+ GarbageCollected() : m_handle(v8::External::New(static_cast<T*>(this))) { }
- bool isEmpty() { return m_value.isEmpty(); }
+ v8::Handle<v8::External> releaseToGarbageCollector()
+ {
+ ASSERT(!m_handle.isWeak()); // Call releaseToGarbageCollector exactly once.
+ v8::Handle<v8::External> result = m_handle.newLocal(v8::Isolate::GetCurrent());
+ m_handle.makeWeak(static_cast<T*>(this), weakCallback);
+ return result;
+ }
- bool operator==(const SharedPersistent<T>& other)
- {
- return m_value == other.m_value;
- }
+ ~GarbageCollected()
+ {
+ ASSERT(m_handle.isEmpty());
+ }
- private:
- explicit SharedPersistent(v8::Handle<T> value) : m_value(value) { }
- ScopedPersistent<T> m_value;
- };
+private:
+ static void weakCallback(v8::Isolate* isolate, v8::Persistent<v8::External>* handle, T* self)
+ {
+ self->m_handle.clear();
+ delete self;
+ }
+
+ ScopedPersistent<v8::External> m_handle;
+};
} // namespace WebCore
-#endif // SharedPersistent_h
+#endif // GarbageCollected_h

Powered by Google App Engine
This is Rietveld 408576698