Ignore:
Timestamp:
Feb 22, 2010, 11:12:01 AM (15 years ago)
Author:
[email protected]
Message:

JSStringBuilder should not CRASH if allocation fails, it should throw a JSException.

Reviewed by Oliver Hunt.

  • runtime/JSGlobalObjectFunctions.cpp:
  • runtime/JSStringBuilder.h:

(JSC::JSStringBuilder::JSStringBuilder):
(JSC::JSStringBuilder::append):
(JSC::JSStringBuilder::build):

  • runtime/StringBuilder.h:

(JSC::StringBuilder::build):

  • wtf/Vector.h:

(WTF::VectorBufferBase::tryAllocateBuffer):
(WTF::):
(WTF::VectorBuffer::tryAllocateBuffer):
(WTF::::tryExpandCapacity):
(WTF::::tryReserveCapacity):
(WTF::::tryAppend):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/JSStringBuilder.h

    r54571 r55093  
    2929#include "ExceptionHelpers.h"
    3030#include "JSString.h"
    31 #include "StringBuilder.h"
     31#include "Vector.h"
    3232
    3333namespace JSC {
    3434
    35 class JSStringBuilder : public StringBuilder {
     35class JSStringBuilder {
    3636public:
     37    JSStringBuilder()
     38        : m_okay(true)
     39    {
     40    }
     41
     42    void append(const UChar u)
     43    {
     44        m_okay &= buffer.tryAppend(&u, 1);
     45    }
     46
     47    void append(const char* str)
     48    {
     49        append(str, strlen(str));
     50    }
     51
     52    void append(const char* str, size_t len)
     53    {
     54        m_okay &= buffer.tryReserveCapacity(buffer.size() + len);
     55        for (size_t i = 0; i < len; i++) {
     56            UChar u = static_cast<unsigned char>(str[i]);
     57            m_okay &= buffer.tryAppend(&u, 1);
     58        }
     59    }
     60
     61    void append(const UChar* str, size_t len)
     62    {
     63        m_okay &= buffer.tryAppend(str, len);
     64    }
     65
     66    void append(const UString& str)
     67    {
     68        m_okay &= buffer.tryAppend(str.data(), str.size());
     69    }
     70
    3771    JSValue build(ExecState* exec)
    3872    {
     73        if (!m_okay)
     74            return throwOutOfMemoryError(exec);
    3975        buffer.shrinkToFit();
    4076        if (!buffer.data())
     
    4379    }
    4480
    45 private:
    46     // Make attempts to call this compile error - if you only wanted a UString,
    47     // Why didn't you just use a StringBuilder?!  (This may change, maybe at some
    48     // point in the future we'll need to start building a string not knowing whether
    49     // we'll want a UString or a JSValue - but until we have this requirement,
    50     // block this).
    51     UString build()
    52     {
    53         ASSERT_NOT_REACHED();
    54         return StringBuilder::build();
    55     }
     81protected:
     82    Vector<UChar, 64> buffer;
     83    bool m_okay;
    5684};
    5785
Note: See TracChangeset for help on using the changeset viewer.