Changeset 11763 in webkit for trunk/JavaScriptCore


Ignore:
Timestamp:
Dec 25, 2005, 1:22:35 AM (19 years ago)
Author:
mjs
Message:

Reviewed by Eric and Dave Hyatt.

This is a somewhat cheesy change. Having to use PassRefPtr_Ref creates ambiguities
in assignment and copy construction. And this makes life way easier and removes
the need for pass(). It is not really correct, but we pretty much never need a real
const PassRefPtr, and this takes care of things for PassRefPtr temporaries.

  • kjs/identifier.cpp: (KJS::Identifier::add): No more need for pass()
  • kjs/property_map.cpp: (KJS::PropertyMap::addSparseArrayPropertiesToReferenceList): No more need for pass()
  • kjs/ustring.cpp: (KJS::UString::Rep::create): Use adoptRef (KJS::UString::UString): No more need for pass (KJS::UString::append): No more need for pass (KJS::UString::substr): No more need for pass
  • kxmlcore/PassRefPtr.h: made m_ptr mutable (ugh) (KXMLCore::PassRefPtr::PassRefPtr): Take a const PassRefPtr reference (KXMLCore::PassRefPtr::release): Made this a const method (ugh) (KXMLCore::PassRefPtr::operator=): clean up appropriately (KXMLCore::adoptRef): Added this to use instead of PassRefPtr<T>::adopt, I think it makes the behavior more clear and it is less verbose. (KXMLCore::static_pointer_cast): use adoptRef (KXMLCore::const_pointer_cast): use adoptRef
  • kxmlcore/RefPtr.h: (KXMLCore::RefPtr::RefPtr): take const PassRefPtr& (KXMLCore::PassRefPtr::operator=): take const PassRefPtr&
Location:
trunk/JavaScriptCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/ChangeLog

    r11762 r11763  
     12005-12-24  Maciej Stachowiak  <[email protected]>
     2
     3        Reviewed by Eric and Dave Hyatt.
     4
     5        - make even const PassRefPtrs give transfer of ownership semantics
     6        http://bugzilla.opendarwin.org/show_bug.cgi?id=6238
     7       
     8        This is a somewhat cheesy change. Having to use PassRefPtr_Ref creates ambiguities
     9        in assignment and copy construction. And this makes life way easier and removes
     10        the need for pass(). It is not really correct, but we pretty much never need a real
     11        const PassRefPtr, and this takes care of things for PassRefPtr temporaries.
     12       
     13        * kjs/identifier.cpp:
     14        (KJS::Identifier::add): No more need for pass()
     15        * kjs/property_map.cpp:
     16        (KJS::PropertyMap::addSparseArrayPropertiesToReferenceList): No more need for pass()
     17        * kjs/ustring.cpp:
     18        (KJS::UString::Rep::create): Use adoptRef
     19        (KJS::UString::UString): No more need for pass
     20        (KJS::UString::append): No more need for pass
     21        (KJS::UString::substr): No more need for pass
     22        * kxmlcore/PassRefPtr.h: made m_ptr mutable (ugh)
     23        (KXMLCore::PassRefPtr::PassRefPtr): Take a const PassRefPtr reference
     24        (KXMLCore::PassRefPtr::release): Made this a const method (ugh)
     25        (KXMLCore::PassRefPtr::operator=): clean up appropriately
     26        (KXMLCore::adoptRef): Added this to use instead of PassRefPtr<T>::adopt, I think
     27        it makes the behavior more clear and it is less verbose.
     28        (KXMLCore::static_pointer_cast): use adoptRef
     29        (KXMLCore::const_pointer_cast): use adoptRef
     30        * kxmlcore/RefPtr.h:
     31        (KXMLCore::RefPtr::RefPtr): take const PassRefPtr&
     32        (KXMLCore::PassRefPtr::operator=): take const PassRefPtr&
     33
    1342005-12-25  Eric Seidel  <[email protected]>
    235
  • trunk/JavaScriptCore/kjs/identifier.cpp

    r11739 r11763  
    130130{
    131131    if (!c)
    132         return pass(&UString::Rep::null);
     132        return &UString::Rep::null;
    133133    int length = strlen(c);
    134134    if (length == 0)
    135         return pass(&UString::Rep::empty);
     135        return &UString::Rep::empty;
    136136   
    137     return pass(*identifierTable().insert<const char *, CStringTranslator>(c).first);
     137    return *identifierTable().insert<const char *, CStringTranslator>(c).first;
    138138}
    139139
     
    173173{
    174174    if (length == 0)
    175         return pass(&UString::Rep::empty);
     175        return &UString::Rep::empty;
    176176   
    177177    UCharBuffer buf = {s, length};
    178     return pass(*identifierTable().insert<UCharBuffer, UCharBufferTranslator>(buf).first);
     178    return *identifierTable().insert<UCharBuffer, UCharBufferTranslator>(buf).first;
    179179}
    180180
     
    182182{
    183183    if (r->isIdentifier)
    184         return pass(r);
     184        return r;
    185185
    186186    if (r->len == 0)
    187         return pass(&UString::Rep::empty);
     187        return &UString::Rep::empty;
    188188
    189189    UString::Rep *result = *identifierTable().insert(r).first;
    190190    if (result == r)
    191191        r->isIdentifier = true;
    192     return pass(result);
     192    return result;
    193193}
    194194
  • trunk/JavaScriptCore/kjs/property_map.cpp

    r11684 r11763  
    615615        UString::Rep *key = _singleEntry.key;
    616616        if (key) {
    617             UString k(pass(key));
     617            UString k(key);
    618618            bool fitsInUInt32;
    619619            k.toUInt32(&fitsInUInt32);
    620620            if (fitsInUInt32)
    621                 list.append(Reference(base, Identifier(pass(key))));
     621                list.append(Reference(base, Identifier(key)));
    622622        }
    623623#endif
     
    630630        UString::Rep *key = entries[i].key;
    631631        if (key && key != &UString::Rep::null) {
    632             UString k(pass(key));
     632            UString k(key);
    633633            bool fitsInUInt32;
    634634            k.toUInt32(&fitsInUInt32);
    635635            if (fitsInUInt32)
    636                 list.append(Reference(base, Identifier(pass(key))));
     636                list.append(Reference(base, Identifier(key)));
    637637        }
    638638    }
  • trunk/JavaScriptCore/kjs/ustring.cpp

    r11684 r11763  
    202202
    203203  // steal the single reference this Rep was created with
    204   return PassRefPtr<Rep>::adopt(r);
     204  return adoptRef(r);
    205205}
    206206
     
    232232
    233233  // steal the single reference this Rep was created with
    234   return PassRefPtr<Rep>::adopt(r);
     234  return adoptRef(r);
    235235}
    236236
     
    460460    x.expandCapacity(aOffset + length);
    461461    memcpy(const_cast<UChar *>(a.data() + aSize), b.data(), bSize * sizeof(UChar));
    462     m_rep = Rep::create(pass(a.m_rep), 0, length);
     462    m_rep = Rep::create(a.m_rep, 0, length);
    463463  } else if (-bOffset == b.usedPreCapacity() && 4 * bSize >= aSize) {
    464464    // - b reaches the beginning of its buffer so it qualifies for shared prepend
     
    468468    y.expandPreCapacity(-bOffset + aSize);
    469469    memcpy(const_cast<UChar *>(b.data() - aSize), a.data(), aSize * sizeof(UChar));
    470     m_rep = Rep::create(pass(b.m_rep), -aSize, length);
     470    m_rep = Rep::create(b.m_rep, -aSize, length);
    471471  } else {
    472472    // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
     
    686686    expandCapacity(thisOffset + length);
    687687    memcpy(const_cast<UChar *>(data() + thisSize), t.data(), tSize * sizeof(UChar));
    688     m_rep = Rep::create(pass(m_rep), 0, length);
     688    m_rep = Rep::create(m_rep, 0, length);
    689689  } else {
    690690    // this is shared with someone using more capacity, gotta make a whole new string
     
    727727    for (int i = 0; i < tSize; ++i)
    728728      d[thisSize+i] = t[i];
    729     m_rep = Rep::create(pass(m_rep), 0, length);
     729    m_rep = Rep::create(m_rep, 0, length);
    730730  } else {
    731731    // this is shared with someone using more capacity, gotta make a whole new string
     
    767767    UChar *d = const_cast<UChar *>(data());
    768768    d[length] = c;
    769     m_rep = Rep::create(pass(m_rep), 0, length + 1);
     769    m_rep = Rep::create(m_rep, 0, length + 1);
    770770  } else {
    771771    // this is shared with someone using more capacity, gotta make a whole new string
     
    11231123    return *this;
    11241124
    1125   return UString(Rep::create(pass(m_rep), pos, len));
     1125  return UString(Rep::create(m_rep, pos, len));
    11261126}
    11271127
  • trunk/JavaScriptCore/kjs/ustring.h

    r11473 r11763  
    463463
    464464    Rep *rep() const { return m_rep.get(); }
    465     UString(PassRefPtr<Rep> r) { m_rep = r; }
     465    UString(PassRefPtr<Rep> r) : m_rep(r) { }
    466466
    467467    void copyForWriting();
  • trunk/JavaScriptCore/kxmlcore/PassRefPtr.h

    r11727 r11763  
    2828    template<typename T> class RefPtr;
    2929    template<typename T> class PassRefPtr;
    30 
    31     // PassRefPtr_Ref class is a helper to allow proper passing of PassRefPtr by value but not by const
    32     // reference
    33     template<typename T>
    34     struct PassRefPtr_Ref
    35     {
    36         T* m_ptr;
    37         explicit PassRefPtr_Ref(T* p) : m_ptr(p) {}
    38     };
     30    template <typename T> PassRefPtr<T> adoptRef(T *p);
    3931
    4032    template<typename T>
     
    4436        PassRefPtr() : m_ptr(0) {}
    4537        PassRefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
    46         PassRefPtr(PassRefPtr& o) : m_ptr(o.release()) {}
    47         template <typename U> PassRefPtr(PassRefPtr<U>& o) : m_ptr(o.release()) { }
     38        // It somewhat breaks the type system to allow transfer of ownership out of
     39        // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
     40        // temporaries, and we don't really have a need to use real const PassRefPtrs
     41        // anyway.
     42        PassRefPtr(const PassRefPtr& o) : m_ptr(o.release()) {}
     43        template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.release()) { }
    4844
    4945        ~PassRefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
     
    5450        T *get() const { return m_ptr; }
    5551
    56         T *release() { T *tmp = m_ptr; m_ptr = 0; return tmp; }
     52        T *release() const { T *tmp = m_ptr; m_ptr = 0; return tmp; }
    5753
    58         template <typename U> static PassRefPtr<T> adopt(U* ptr)
    59         {
    60             PassRefPtr result((PassRefPtr_Ref<T>(ptr)));
    61             return result;
    62         }
    63        
    6454        T& operator*() const { return *m_ptr; }
    6555        T *operator->() const { return m_ptr; }
     
    7262       
    7363        PassRefPtr& operator=(T *);
    74         PassRefPtr& operator=(PassRefPtr&);
    75         template <typename U> PassRefPtr& operator=(PassRefPtr<U>&);
     64        PassRefPtr& operator=(const PassRefPtr&);
     65        template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
    7666        template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
    7767
    78         PassRefPtr(PassRefPtr_Ref<T> ref) : m_ptr(ref.m_ptr) { }
    79      
    80         PassRefPtr& operator=(PassRefPtr_Ref<T> ref)
    81         {
    82             if (m_ptr)
    83                 m_ptr->deref();
    84             m_ptr = ref.m_ptr;
    85             return *this;
    86         }
    87 
    88         template <typename U> operator PassRefPtr_Ref<U>() { return PassRefPtr_Ref<U>(release()); }
    89         template <typename U> operator PassRefPtr<U>() { return PassRefPtr_Ref<U>(release()); }
    90        
     68        friend PassRefPtr adoptRef<T>(T *);
    9169    private:
    92         T *m_ptr;
     70        // adopting constructor
     71        PassRefPtr(T *ptr, bool) : m_ptr(ptr) {}
     72        mutable T *m_ptr;
    9373    };
    9474   
     
    11494    }
    11595
    116     template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(PassRefPtr<T>& ref)
     96    template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
    11797    {
    11898        T *optr = ref.release();
     
    123103    }
    124104   
    125     template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(PassRefPtr<U>& ref)
     105    template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
    126106    {
    127107        T *optr = ref.release();
     
    182162    }
    183163   
     164    template <typename T> inline PassRefPtr<T> adoptRef(T *p)
     165    {
     166        return PassRefPtr<T>(p, true);
     167    }
     168
    184169    template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p)
    185170    {
    186         return PassRefPtr<T>::adopt(static_cast<T *>(p.release()));
     171        return adoptRef(static_cast<T *>(p.release()));
    187172    }
    188173
    189174    template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p)
    190175    {
    191         return PassRefPtr<T>::adopt(const_cast<T *>(p.release()));
    192     }
    193 
    194     template <typename T> inline PassRefPtr<T> pass(T *ptr)
    195     {
    196         return PassRefPtr<T>(ptr);
    197     }
    198 
    199     template <typename T> inline PassRefPtr<T> pass(const RefPtr<T>& ptr)
    200     {
    201         return PassRefPtr<T>(ptr);
     176        return adoptRef(const_cast<T *>(p.release()));
    202177    }
    203178
     
    205180
    206181using KXMLCore::PassRefPtr;
     182using KXMLCore::adoptRef;
    207183using KXMLCore::static_pointer_cast;
    208184using KXMLCore::const_pointer_cast;
    209 using KXMLCore::pass;
    210185
    211186#endif // KXMLCORE_PASS_REF_PTR_H
  • trunk/JavaScriptCore/kxmlcore/RefPtr.h

    r11747 r11763  
    2929
    3030    template <typename T> class PassRefPtr;
    31     template <typename T> class PassRefPtr_Ref;
    3231
    3332    template <typename T> class RefPtr
     
    3736        RefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
    3837        RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
    39         template <typename U> RefPtr(PassRefPtr<U>&);
    40         template <typename U> RefPtr(PassRefPtr_Ref<U>);
     38        // see comment in PassRefPtr.h for why this takes const reference
     39        template <typename U> RefPtr(const PassRefPtr<U>&);
    4140
    4241        ~RefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
     
    5756        RefPtr& operator=(const RefPtr&);
    5857        RefPtr& operator=(T *);
    59         RefPtr& operator=(PassRefPtr<T>&);
    60         RefPtr& operator=(PassRefPtr_Ref<T>);
     58        RefPtr& operator=(const PassRefPtr<T>&);
    6159        template <typename U> RefPtr& operator=(const RefPtr<U>&);
    62         template <typename U> RefPtr& operator=(PassRefPtr<U>&);
    63         template <typename U> RefPtr& operator=(PassRefPtr_Ref<U>);
     60        template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
    6461
    6562        void swap(RefPtr&);
     
    6966    };
    7067   
    71     template <typename T> template <typename U> inline RefPtr<T>::RefPtr(PassRefPtr_Ref<U> ref)
    72         : m_ptr(ref.m_ptr)
    73     {
    74     }
    75    
    76     template <typename T> template <typename U> inline RefPtr<T>::RefPtr(PassRefPtr<U>& o)
     68    template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
    7769        : m_ptr(o.release())
    7870    {
     
    111103    }
    112104
    113     template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr_Ref<U> ref)
    114     {
    115         if (m_ptr)
    116             m_ptr->deref();
    117         m_ptr = ref.m_ptr;
    118         return *this;
    119     }
    120    
    121     template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr<T>& o)
     105    template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
    122106    {
    123107        if (m_ptr)
     
    127111    }
    128112
    129     template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr_Ref<T> ref)
    130     {
    131         if (m_ptr)
    132             m_ptr->deref();
    133         m_ptr = ref.m_ptr;
    134         return *this;
    135     }
    136    
    137     template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(PassRefPtr<U>& o)
     113    template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
    138114    {
    139115        if (m_ptr)
Note: See TracChangeset for help on using the changeset viewer.