Changeset 54545 in webkit


Ignore:
Timestamp:
Feb 9, 2010 3:55:39 AM (14 years ago)
Author:
barraclough@apple.com
Message:

Three small string fixes:
(1) StringBuilder::release should CRASH if the buffer allocation failed.
(2) Remove weird, dead code from JSString::tryGetValue, replace with an ASSERT.
(3) Move UString::createFromUTF8 out to the API, as tryCreateStringFromUTF8.

This is only used from the API, and (now) unlike other UString::create
methods may return UString::null() to indicate failure cases. Better
handle these in the API.

Reviewed by Oliver Hunt.

  • API/JSClassRef.cpp:

(tryCreateStringFromUTF8):
(OpaqueJSClass::OpaqueJSClass):
(OpaqueJSClassContextData::OpaqueJSClassContextData):

  • runtime/JSString.h:

(JSC::Fiber::tryGetValue):

  • runtime/StringBuilder.h:

(JSC::StringBuilder::release):

  • runtime/UString.cpp:

(JSC::UString::UString):
(JSC::UString::from):
(JSC::UString::find):

  • runtime/UString.h:
Location:
trunk/JavaScriptCore
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/API/JSClassRef.cpp

    r54428 r54545  
    3434#include <runtime/ObjectPrototype.h>
    3535#include <runtime/Identifier.h>
     36#include <wtf/unicode/UTF8.h>
    3637
    3738using namespace std;
    3839using namespace JSC;
     40using namespace WTF::Unicode;
    3941
    4042const JSClassDefinition kJSClassDefinitionEmpty = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
     43
     44UString tryCreateStringFromUTF8(const char* string)
     45{
     46    if (!string)
     47        return UString::null();
     48
     49    size_t length = strlen(string);
     50    Vector<UChar, 1024> buffer(length);
     51    UChar* p = buffer.data();
     52    if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
     53        return UString::null();
     54
     55    return UString(buffer.data(), p - buffer.data());
     56}
    4157
    4258OpaqueJSClass::OpaqueJSClass(const JSClassDefinition* definition, OpaqueJSClass* protoClass)
     
    5470    , hasInstance(definition->hasInstance)
    5571    , convertToType(definition->convertToType)
    56     , m_className(UString::createFromUTF8(definition->className).rep()->ref())
     72    , m_className(tryCreateStringFromUTF8(definition->className))
    5773    , m_staticValues(0)
    5874    , m_staticFunctions(0)
     
    6379        m_staticValues = new OpaqueJSClassStaticValuesTable();
    6480        while (staticValue->name) {
    65             // Use a local variable here to sidestep an RVCT compiler bug.
    66             StaticValueEntry* entry = new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes);
    67             m_staticValues->add(UString::createFromUTF8(staticValue->name).rep()->ref(), entry);
     81            UString valueName = tryCreateStringFromUTF8(staticValue->name);
     82            if (!valueName.isNull()) {
     83                // Use a local variable here to sidestep an RVCT compiler bug.
     84                StaticValueEntry* entry = new StaticValueEntry(staticValue->getProperty, staticValue->setProperty, staticValue->attributes);
     85                m_staticValues->add(valueName.rep()->ref(), entry);
     86            }
    6887            ++staticValue;
    6988        }
     
    7392        m_staticFunctions = new OpaqueJSClassStaticFunctionsTable();
    7493        while (staticFunction->name) {
    75             // Use a local variable here to sidestep an RVCT compiler bug.
    76             StaticFunctionEntry* entry = new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes);
    77             m_staticFunctions->add(UString::createFromUTF8(staticFunction->name).rep()->ref(), entry);
     94            UString functionName = tryCreateStringFromUTF8(staticFunction->name);
     95            if (!functionName.isNull()) {
     96                // Use a local variable here to sidestep an RVCT compiler bug.
     97                StaticFunctionEntry* entry = new StaticFunctionEntry(staticFunction->callAsFunction, staticFunction->attributes);
     98                m_staticFunctions->add(functionName.rep()->ref(), entry);
     99            }
    78100            ++staticFunction;
    79101        }
     
    147169            StaticValueEntry* entry = new StaticValueEntry(it->second->getProperty, it->second->setProperty, it->second->attributes);
    148170            staticValues->add(UString::Rep::create(it->first->data(), it->first->size()), entry);
    149 
    150         }
    151            
     171        }
    152172    } else
    153173        staticValues = 0;
    154        
    155174
    156175    if (jsClass->m_staticFunctions) {
  • trunk/JavaScriptCore/ChangeLog

    r54539 r54545  
     12010-02-09  Gavin Barraclough  <barraclough@apple.com>
     2
     3        Reviewed by Oliver Hunt.
     4
     5        Three small string fixes:
     6        (1) StringBuilder::release should CRASH if the buffer allocation failed.
     7        (2) Remove weird, dead code from JSString::tryGetValue, replace with an ASSERT.
     8        (3) Move UString::createFromUTF8 out to the API, as tryCreateStringFromUTF8.
     9            This is only used from the API, and (now) unlike other UString::create
     10            methods may return UString::null() to indicate failure cases.  Better
     11            handle these in the API.
     12
     13        * API/JSClassRef.cpp:
     14        (tryCreateStringFromUTF8):
     15        (OpaqueJSClass::OpaqueJSClass):
     16        (OpaqueJSClassContextData::OpaqueJSClassContextData):
     17        * runtime/JSString.h:
     18        (JSC::Fiber::tryGetValue):
     19        * runtime/StringBuilder.h:
     20        (JSC::StringBuilder::release):
     21        * runtime/UString.cpp:
     22        (JSC::UString::UString):
     23        (JSC::UString::from):
     24        (JSC::UString::find):
     25        * runtime/UString.h:
     26
    1272010-02-09  Janne Koskinen  <janne.p.koskinen@digia.com>
    228
  • trunk/JavaScriptCore/runtime/JSString.h

    r54022 r54545  
    284284        const UString tryGetValue() const
    285285        {
    286             if (isRope())
    287                 UString();
     286            // If this is a rope, m_value should be null -
     287            // if this is not a rope, m_value should be non-null.
     288            ASSERT(isRope() == m_value.isNull());
    288289            return m_value;
    289290        }
  • trunk/JavaScriptCore/runtime/StringBuilder.h

    r54394 r54545  
    7070    {
    7171        buffer.shrinkToFit();
     72        if (!buffer.data())
     73            CRASH();
    7274        return UString::adopt(buffer);
    7375    }
  • trunk/JavaScriptCore/runtime/UString.cpp

    r54531 r54545  
    5454
    5555namespace JSC {
    56  
     56
    5757extern const double NaN;
    5858extern const double Inf;
     
    147147}
    148148
    149 // These static strings are immutable, except for rc, whose initial value is chosen to 
     149// These static strings are immutable, except for rc, whose initial value is chosen to
    150150// reduce the possibility of it becoming zero due to ref/deref not being thread-safe.
    151151static UChar sharedEmptyChar;
     
    175175UString::UString(const UChar* c, int length)
    176176{
    177     if (length == 0) 
     177    if (length == 0)
    178178        m_rep = &Rep::empty();
    179179    else
     
    181181}
    182182
    183 UString UString::createFromUTF8(const char* string)
    184 {
    185     if (!string)
    186         return null();
    187 
    188     size_t length = strlen(string);
    189     Vector<UChar, 1024> buffer(length);
    190     UChar* p = buffer.data();
    191     if (conversionOK != convertUTF8ToUTF16(&string, string + length, &p, p + length))
    192         return null();
    193 
    194     return UString(buffer.data(), p - buffer.data());
    195 }
    196 
    197183UString UString::from(int i)
    198184{
     
    200186    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    201187    UChar* p = end;
    202  
     188
    203189    if (i == 0)
    204190        *--p = '0';
     
    262248    UChar* end = buf + sizeof(buf) / sizeof(UChar);
    263249    UChar* p = end;
    264    
     250
    265251    if (u == 0)
    266252        *--p = '0';
     
    271257        }
    272258    }
    273    
     259
    274260    return UString(p, static_cast<int>(end - p));
    275261}
     
    598584            return static_cast<int>(c - data());
    599585    }
    600    
     586
    601587    return -1;
    602588}
  • trunk/JavaScriptCore/runtime/UString.h

    r54531 r54545  
    8282   
    8383    public:
    84         // UString constructors passed char*s assume ISO Latin-1 encoding; for UTF8 use 'createFromUTF8', below.
    8584        UString();
    8685        UString(const char*); // Constructor for null-terminated string.
     
    109108            return Rep::adopt(vector);
    110109        }
    111 
    112         static UString createFromUTF8(const char*);
    113110
    114111        static UString from(int);
  • trunk/JavaScriptCore/runtime/UStringImpl.h

    r54464 r54545  
    8888    static PassRefPtr<UStringImpl> adopt(Vector<UChar, inlineCapacity>& vector)
    8989    {
     90        ASSERT(vector.data());
    9091        if (unsigned length = vector.size())
    9192            return adoptRef(new UStringImpl(vector.releaseBuffer(), length, BufferOwned));
Note: See TracChangeset for help on using the changeset viewer.