Ignore:
Timestamp:
Apr 27, 2012, 8:43:29 PM (13 years ago)
Author:
[email protected]
Message:

Only allow non-null pointers in the WeakSet
https://bugs.webkit.org/show_bug.cgi?id=85119

Reviewed by Darin Adler.

../JavaScriptCore:

This is a step toward more efficient finalization.

No clients put non-pointers (JSValues) into Weak<T> and PassWeak<T>.

Some clients put null pointers into Weak<T> and PassWeak<T>, but this is
more efficient and straight-forward to model with a null in the Weak<T>
or PassWeak<T> instead of allocating a WeakImpl just to hold null.

  • heap/PassWeak.h:

(JSC): Removed the Unknown (JSValue) type of weak pointer because it's
unused now.

(PassWeak): Don't provide a default initializer for our JSCell* argument.
This feature was only used in one place, and it was a bug.

(JSC::::get): Don't check for a null stored inside our WeakImpl: that's
not allowed anymore.

(JSC::PassWeak::PassWeak): Handle null as a null WeakImpl instead of
allocating a WeakImpl and storing null into it.

  • heap/Weak.h:

(Weak):
(JSC::::Weak): Same changes as in PassWeak<T>.

  • heap/WeakBlock.cpp:

(JSC::WeakBlock::visitLiveWeakImpls):
(JSC::WeakBlock::visitDeadWeakImpls): Only non-null cells are valid in
the WeakSet now, so no need to check for non-cells and null cell pointers.

  • heap/WeakImpl.h:

(JSC::WeakImpl::WeakImpl): Only non-null cells are valid in the WeakSet
now, so ASSERT that.

../WebCore:

  • bridge/jsc/BridgeJSC.cpp:

(JSC::Bindings::Instance::Instance): Don't allocate a WeakImpl just to
store null. This was needless, and is now a compile error. Instead,
rely on the default constructor, which will produce a cheap null.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/heap/PassWeak.h

    r113508 r115534  
    5151};
    5252
    53 template<typename Base> class WeakImplAccessor<Base, Unknown> {
    54 public:
    55     typedef JSValue GetType;
    56 
    57     const JSValue* operator->() const;
    58     const JSValue& operator*() const;
    59     GetType get() const;
    60 };
    61 
    6253template<typename T> class PassWeak : public WeakImplAccessor<PassWeak<T>, T> {
    6354public:
     
    6758    PassWeak();
    6859    PassWeak(std::nullptr_t);
    69     PassWeak(JSGlobalData&, GetType = GetType(), WeakHandleOwner* = 0, void* context = 0);
     60    PassWeak(JSGlobalData&, GetType, WeakHandleOwner* = 0, void* context = 0);
    7061
    7162    // It somewhat breaks the type system to allow transfer of ownership out of
     
    10697template<typename Base, typename T> inline typename WeakImplAccessor<Base, T>::GetType WeakImplAccessor<Base, T>::get() const
    10798{
    108     if (!static_cast<const Base*>(this)->m_impl || static_cast<const Base*>(this)->m_impl->state() != WeakImpl::Live || !static_cast<const Base*>(this)->m_impl->jsValue())
     99    if (!static_cast<const Base*>(this)->m_impl || static_cast<const Base*>(this)->m_impl->state() != WeakImpl::Live)
    109100        return GetType();
    110101    return jsCast<T*>(static_cast<const Base*>(this)->m_impl->jsValue().asCell());
     
    118109#endif
    119110
    120 template<typename Base> inline const JSValue* WeakImplAccessor<Base, Unknown>::operator->() const
    121 {
    122     ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
    123     return &static_cast<const Base*>(this)->m_impl->jsValue();
    124 }
    125 
    126 template<typename Base> inline const JSValue& WeakImplAccessor<Base, Unknown>::operator*() const
    127 {
    128     ASSERT(static_cast<const Base*>(this)->m_impl && static_cast<const Base*>(this)->m_impl->state() == WeakImpl::Live);
    129     return static_cast<const Base*>(this)->m_impl->jsValue();
    130 }
    131 
    132 template<typename Base> inline typename WeakImplAccessor<Base, Unknown>::GetType WeakImplAccessor<Base, Unknown>::get() const
    133 {
    134     if (!static_cast<const Base*>(this)->m_impl || static_cast<const Base*>(this)->m_impl->state() != WeakImpl::Live)
    135         return GetType();
    136     return static_cast<const Base*>(this)->m_impl->jsValue();
    137 }
    138 
    139111template<typename T> inline PassWeak<T>::PassWeak()
    140112    : m_impl(0)
     
    148120
    149121template<typename T> inline PassWeak<T>::PassWeak(JSGlobalData& globalData, typename PassWeak<T>::GetType getType, WeakHandleOwner* weakOwner, void* context)
    150     : m_impl(globalData.heap.weakSet()->allocate(getType, weakOwner, context))
     122    : m_impl(getType ? globalData.heap.weakSet()->allocate(getType, weakOwner, context) : 0)
    151123{
    152124}
Note: See TracChangeset for help on using the changeset viewer.