diff options
Diffstat (limited to 'Source/JavaScriptCore/heap/Weak.h')
-rw-r--r-- | Source/JavaScriptCore/heap/Weak.h | 100 |
1 files changed, 46 insertions, 54 deletions
diff --git a/Source/JavaScriptCore/heap/Weak.h b/Source/JavaScriptCore/heap/Weak.h index f0c028d71..96fe1b58c 100644 --- a/Source/JavaScriptCore/heap/Weak.h +++ b/Source/JavaScriptCore/heap/Weak.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Apple Inc. All rights reserved. + * Copyright (C) 2009, 2012 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -30,11 +30,14 @@ #include "Handle.h" #include "HandleHeap.h" #include "JSGlobalData.h" +#include "PassWeak.h" namespace JSC { // A weakly referenced handle that becomes 0 when the value it points to is garbage collected. template <typename T> class Weak : public Handle<T> { + WTF_MAKE_NONCOPYABLE(Weak); + using Handle<T>::slot; using Handle<T>::setSlot; @@ -46,11 +49,18 @@ public: { } - Weak(JSGlobalData& globalData, ExternalType value = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) + Weak(std::nullptr_t) + : Handle<T>() + { + } + + Weak(JSGlobalData& globalData, ExternalType externalType = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) : Handle<T>(globalData.heap.handleHeap()->allocate()) { HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); - set(value); + JSValue value = HandleTypes<T>::toJSValue(externalType); + HandleHeap::heapFor(slot())->writeBarrier(slot(), value); + *slot() = value; } enum AdoptTag { Adopt }; @@ -59,23 +69,7 @@ public: { validateCell(get()); } - - Weak(const Weak& other) - : Handle<T>() - { - if (!other.slot()) - return; - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - } - template <typename U> Weak(const Weak<U>& other) - : Handle<T>() - { - if (!other.slot()) - return; - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - } - enum HashTableDeletedValueTag { HashTableDeletedValue }; bool isHashTableDeletedValue() const { return slot() == hashTableDeletedValue(); } Weak(HashTableDeletedValueTag) @@ -83,6 +77,11 @@ public: { } + template<typename U> Weak(const PassWeak<U>& other) + : Handle<T>(other.leakHandle()) + { + } + ~Weak() { clear(); @@ -93,8 +92,12 @@ public: Handle<T>::swap(other); } + Weak& operator=(const PassWeak<T>&); + ExternalType get() const { return HandleTypes<T>::getFromSlot(slot()); } + PassWeak<T> release() { PassWeak<T> tmp = adoptWeak<T>(slot()); setSlot(0); return tmp; } + void clear() { if (!slot()) @@ -103,32 +106,6 @@ public: setSlot(0); } - void set(JSGlobalData& globalData, ExternalType value, WeakHandleOwner* weakOwner = 0, void* context = 0) - { - if (!slot()) { - setSlot(globalData.heap.handleHeap()->allocate()); - HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); - } - ASSERT(HandleHeap::heapFor(slot())->hasWeakOwner(slot(), weakOwner)); - set(value); - } - - template <typename U> Weak& operator=(const Weak<U>& other) - { - clear(); - if (other.slot()) - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - return *this; - } - - Weak& operator=(const Weak& other) - { - clear(); - if (other.slot()) - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - return *this; - } - HandleSlot leakHandle() { ASSERT(HandleHeap::heapFor(slot())->hasFinalizer(slot())); @@ -139,14 +116,6 @@ public: private: static HandleSlot hashTableDeletedValue() { return reinterpret_cast<HandleSlot>(-1); } - - void set(ExternalType externalType) - { - ASSERT(slot()); - JSValue value = HandleTypes<T>::toJSValue(externalType); - HandleHeap::heapFor(slot())->writeBarrier(slot(), value); - *slot() = value; - } }; template<class T> inline void swap(Weak<T>& a, Weak<T>& b) @@ -154,6 +123,13 @@ template<class T> inline void swap(Weak<T>& a, Weak<T>& b) a.swap(b); } +template<typename T> inline Weak<T>& Weak<T>::operator=(const PassWeak<T>& o) +{ + clear(); + setSlot(o.leakHandle()); + return *this; +} + } // namespace JSC namespace WTF { @@ -162,7 +138,23 @@ template<typename T> struct VectorTraits<JSC::Weak<T> > : SimpleClassVectorTrait static const bool canCompareWithMemcmp = false; }; -template<typename P> struct HashTraits<JSC::Weak<P> > : SimpleClassHashTraits<JSC::Weak<P> > { }; +template<typename T> struct HashTraits<JSC::Weak<T> > : SimpleClassHashTraits<JSC::Weak<T> > { + typedef JSC::Weak<T> StorageType; + + typedef std::nullptr_t EmptyValueType; + static EmptyValueType emptyValue() { return nullptr; } + + typedef JSC::PassWeak<T> PassInType; + static void store(PassInType value, StorageType& storage) { storage = value; } + + typedef JSC::PassWeak<T> PassOutType; + static PassOutType passOut(StorageType& value) { return value.release(); } + static PassOutType passOut(EmptyValueType) { return PassOutType(); } + + typedef typename StorageType::ExternalType PeekType; + static PeekType peek(const StorageType& value) { return value.get(); } + static PeekType peek(EmptyValueType) { return PeekType(); } +}; } |