From ad0d549d4cc13433f77c1ac8f0ab379c83d93f28 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 24 Feb 2012 16:36:50 +0100 Subject: Imported WebKit commit bb52bf3c0119e8a128cd93afe5572413a8617de9 (http://svn.webkit.org/repository/webkit/trunk@108790) --- Source/JavaScriptCore/heap/Weak.h | 100 ++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 54 deletions(-) (limited to 'Source/JavaScriptCore/heap/Weak.h') 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 class Weak : public Handle { + WTF_MAKE_NONCOPYABLE(Weak); + using Handle::slot; using Handle::setSlot; @@ -46,11 +49,18 @@ public: { } - Weak(JSGlobalData& globalData, ExternalType value = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) + Weak(std::nullptr_t) + : Handle() + { + } + + Weak(JSGlobalData& globalData, ExternalType externalType = ExternalType(), WeakHandleOwner* weakOwner = 0, void* context = 0) : Handle(globalData.heap.handleHeap()->allocate()) { HandleHeap::heapFor(slot())->makeWeak(slot(), weakOwner, context); - set(value); + JSValue value = HandleTypes::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() - { - if (!other.slot()) - return; - setSlot(HandleHeap::heapFor(other.slot())->copyWeak(other.slot())); - } - template Weak(const Weak& other) - : Handle() - { - 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 Weak(const PassWeak& other) + : Handle(other.leakHandle()) + { + } + ~Weak() { clear(); @@ -93,8 +92,12 @@ public: Handle::swap(other); } + Weak& operator=(const PassWeak&); + ExternalType get() const { return HandleTypes::getFromSlot(slot()); } + PassWeak release() { PassWeak tmp = adoptWeak(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 Weak& operator=(const Weak& 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(-1); } - - void set(ExternalType externalType) - { - ASSERT(slot()); - JSValue value = HandleTypes::toJSValue(externalType); - HandleHeap::heapFor(slot())->writeBarrier(slot(), value); - *slot() = value; - } }; template inline void swap(Weak& a, Weak& b) @@ -154,6 +123,13 @@ template inline void swap(Weak& a, Weak& b) a.swap(b); } +template inline Weak& Weak::operator=(const PassWeak& o) +{ + clear(); + setSlot(o.leakHandle()); + return *this; +} + } // namespace JSC namespace WTF { @@ -162,7 +138,23 @@ template struct VectorTraits > : SimpleClassVectorTrait static const bool canCompareWithMemcmp = false; }; -template struct HashTraits > : SimpleClassHashTraits > { }; +template struct HashTraits > : SimpleClassHashTraits > { + typedef JSC::Weak StorageType; + + typedef std::nullptr_t EmptyValueType; + static EmptyValueType emptyValue() { return nullptr; } + + typedef JSC::PassWeak PassInType; + static void store(PassInType value, StorageType& storage) { storage = value; } + + typedef JSC::PassWeak 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(); } +}; } -- cgit v1.2.1