]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/JSString.cpp
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / runtime / JSString.cpp
index 1d5e639714f5f47811390be4d7bdeb8030936b8e..86704d715805cba8e6f12faeae66cc35173451dc 100644 (file)
 #include "JSString.h"
 
 #include "JSGlobalObject.h"
+#include "JSGlobalObjectFunctions.h"
 #include "JSObject.h"
 #include "Operations.h"
 #include "StringObject.h"
 #include "StringPrototype.h"
 
 namespace JSC {
+    
+static const unsigned substringFromRopeCutoff = 4;
 
-// Overview: this methods converts a JSString from holding a string in rope form
-// down to a simple UString representation.  It does so by building up the string
-// backwards, since we want to avoid recursion, we expect that the tree structure
-// representing the rope is likely imbalanced with more nodes down the left side
-// (since appending to the string is likely more common) - and as such resolving
-// in this fashion should minimize work queue size.  (If we built the queue forwards
-// we would likely have to place all of the constituent UStringImpls into the
-// Vector before performing any concatenation, but by working backwards we likely
-// only fill the queue with the number of substrings at any given level in a
-// rope-of-ropes.)
-void JSString::resolveRope(ExecState* exec) const
+const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
+
+void JSRopeString::RopeBuilder::expand()
+{
+    ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
+    JSString* jsString = m_jsString;
+    m_jsString = jsStringBuilder(&m_vm);
+    m_index = 0;
+    append(jsString);
+}
+
+void JSString::destroy(JSCell* cell)
+{
+    JSString* thisObject = static_cast<JSString*>(cell);
+    thisObject->JSString::~JSString();
+}
+
+void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
+{
+    JSString* thisObject = jsCast<JSString*>(cell);
+    Base::visitChildren(thisObject, visitor);
+    
+    MARK_LOG_MESSAGE1("[%u]: ", thisObject->length());
+
+#if ENABLE(OBJECT_MARK_LOGGING)
+    if (!thisObject->isRope()) {
+        WTF::StringImpl* ourImpl = thisObject->m_value.impl();
+        if (ourImpl->is8Bit())
+            MARK_LOG_MESSAGE1("[8 %p]", ourImpl->characters8());
+        else
+            MARK_LOG_MESSAGE1("[16 %p]", ourImpl->characters16());
+    } else
+        MARK_LOG_MESSAGE0("[rope]: ");
+#endif
+
+    if (thisObject->isRope())
+        static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
+}
+
+void JSRopeString::visitFibers(SlotVisitor& visitor)
+{
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        visitor.append(&m_fibers[i]);
+}
+
+void JSRopeString::resolveRope(ExecState* exec) const
 {
     ASSERT(isRope());
 
-    // Allocate the buffer to hold the final string, position initially points to the end.
-    UChar* buffer;
-    if (PassRefPtr<UStringImpl> newImpl = UStringImpl::tryCreateUninitialized(m_length, buffer))
-        m_value = newImpl;
-    else {
-        for (unsigned i = 0; i < m_fiberCount; ++i) {
-            RopeImpl::deref(m_other.m_fibers[i]);
-            m_other.m_fibers[i] = 0;
+    if (is8Bit()) {
+        LChar* buffer;
+        if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
+            Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
+            m_value = newImpl.release();
+        } else {
+            outOfMemory(exec);
+            return;
+        }
+
+        for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+            if (m_fibers[i]->isRope())
+                return resolveRopeSlowCase8(buffer);
+        }
+
+        LChar* position = buffer;
+        for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+            StringImpl* string = m_fibers[i]->m_value.impl();
+            unsigned length = string->length();
+            StringImpl::copyChars(position, string->characters8(), length);
+            position += length;
+            m_fibers[i].clear();
         }
-        m_fiberCount = 0;
+        ASSERT((buffer + m_length) == position);
         ASSERT(!isRope());
-        ASSERT(m_value == UString());
-        if (exec)
-            throwOutOfMemoryError(exec);
+
         return;
     }
-    UChar* position = buffer + m_length;
-
-    // Start with the current RopeImpl.
-    Vector<RopeImpl::Fiber, 32> workQueue;
-    RopeImpl::Fiber currentFiber;
-    for (unsigned i = 0; i < (m_fiberCount - 1); ++i)
-        workQueue.append(m_other.m_fibers[i]);
-    currentFiber = m_other.m_fibers[m_fiberCount - 1];
-    while (true) {
-        if (RopeImpl::isRope(currentFiber)) {
-            RopeImpl* rope = static_cast<RopeImpl*>(currentFiber);
-            // Copy the contents of the current rope into the workQueue, with the last item in 'currentFiber'
-            // (we will be working backwards over the rope).
-            unsigned fiberCountMinusOne = rope->fiberCount() - 1;
-            for (unsigned i = 0; i < fiberCountMinusOne; ++i)
-                workQueue.append(rope->fibers()[i]);
-            currentFiber = rope->fibers()[fiberCountMinusOne];
-        } else {
-            UStringImpl* string = static_cast<UStringImpl*>(currentFiber);
-            unsigned length = string->length();
-            position -= length;
-            UStringImpl::copyChars(position, string->characters(), length);
-
-            // Was this the last item in the work queue?
-            if (workQueue.isEmpty()) {
-                // Create a string from the UChar buffer, clear the rope RefPtr.
-                ASSERT(buffer == position);
-                for (unsigned i = 0; i < m_fiberCount; ++i) {
-                    RopeImpl::deref(m_other.m_fibers[i]);
-                    m_other.m_fibers[i] = 0;
-                }
-                m_fiberCount = 0;
-
-                ASSERT(!isRope());
-                return;
-            }
-
-            // No! - set the next item up to process.
-            currentFiber = workQueue.last();
-            workQueue.removeLast();
-        }
+
+    UChar* buffer;
+    if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
+        Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
+        m_value = newImpl.release();
+    } else {
+        outOfMemory(exec);
+        return;
     }
+
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        if (m_fibers[i]->isRope())
+            return resolveRopeSlowCase(buffer);
+    }
+
+    UChar* position = buffer;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        StringImpl* string = m_fibers[i]->m_value.impl();
+        unsigned length = string->length();
+        if (string->is8Bit())
+            StringImpl::copyChars(position, string->characters8(), length);
+        else
+            StringImpl::copyChars(position, string->characters16(), length);
+        position += length;
+        m_fibers[i].clear();
+    }
+    ASSERT((buffer + m_length) == position);
+    ASSERT(!isRope());
 }
 
-JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement)
+// Overview: These functions convert a JSString from holding a string in rope form
+// down to a simple String representation. It does so by building up the string
+// backwards, since we want to avoid recursion, we expect that the tree structure
+// representing the rope is likely imbalanced with more nodes down the left side
+// (since appending to the string is likely more common) - and as such resolving
+// in this fashion should minimize work queue size.  (If we built the queue forwards
+// we would likely have to place all of the constituent StringImpls into the
+// Vector before performing any concatenation, but by working backwards we likely
+// only fill the queue with the number of substrings at any given level in a
+// rope-of-ropes.)    
+void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
 {
-    if (!isRope()) {
-        unsigned matchPosition = m_value.find(character);
-        if (matchPosition == UString::NotFound)
-            return JSValue(this);
-        return jsString(exec, m_value.substr(0, matchPosition), replacement, m_value.substr(matchPosition + 1));
+    LChar* position = buffer + m_length; // We will be working backwards over the rope.
+    Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
+    
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        workQueue.append(m_fibers[i].get());
+        // Clearing here works only because there are no GC points in this method.
+        m_fibers[i].clear();
     }
 
-    RopeIterator end;
-    
-    // Count total fibers and find matching string.
-    size_t fiberCount = 0;
-    UStringImpl* matchString = 0;
-    int matchPosition = -1;
-    for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) {
-        ++fiberCount;
-        if (matchString)
-            continue;
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
 
-        UStringImpl* string = *it;
-        matchPosition = string->find(character);
-        if (matchPosition == -1)
+        if (currentFiber->isRope()) {
+            JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
+            for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
+                workQueue.append(currentFiberAsRope->m_fibers[i].get());
             continue;
-        matchString = string;
+        }
+
+        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
+        unsigned length = string->length();
+        position -= length;
+        StringImpl::copyChars(position, string->characters8(), length);
     }
 
-    if (!matchString)
-        return this;
+    ASSERT(buffer == position);
+    ASSERT(!isRope());
+}
+
+void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
+{
+    UChar* position = buffer + m_length; // We will be working backwards over the rope.
+    Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
+
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        workQueue.append(m_fibers[i].get());
 
-    RopeBuilder builder(replacement.size() ? fiberCount + 2 : fiberCount + 1);
-    if (UNLIKELY(builder.isOutOfMemory()))
-        return throwOutOfMemoryError(exec);
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
 
-    for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) {
-        UStringImpl* string = *it;
-        if (string != matchString) {
-            builder.append(UString(string));
+        if (currentFiber->isRope()) {
+            JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
+            for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
+                workQueue.append(currentFiberAsRope->m_fibers[i].get());
             continue;
         }
 
-        builder.append(UString(string).substr(0, matchPosition));
-        if (replacement.size())
-            builder.append(replacement);
-        builder.append(UString(string).substr(matchPosition + 1));
-        matchString = 0;
+        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
+        unsigned length = string->length();
+        position -= length;
+        if (string->is8Bit())
+            StringImpl::copyChars(position, string->characters8(), length);
+        else
+            StringImpl::copyChars(position, string->characters16(), length);
     }
 
-    JSGlobalData* globalData = &exec->globalData();
-    return JSValue(new (globalData) JSString(globalData, builder.release()));
+    ASSERT(buffer == position);
+    ASSERT(!isRope());
 }
 
-JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i)
+void JSRopeString::outOfMemory(ExecState* exec) const
+{
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        m_fibers[i].clear();
+    ASSERT(isRope());
+    ASSERT(m_value.isNull());
+    if (exec)
+        throwOutOfMemoryError(exec);
+}
+
+JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i)
 {
     ASSERT(isRope());
     resolveRope(exec);
     // Return a safe no-value result, this should never be used, since the excetion will be thrown.
     if (exec->exception())
-        return jsString(exec, "");
+        return jsEmptyString(exec);
     ASSERT(!isRope());
-    ASSERT(i < m_value.size());
+    RELEASE_ASSERT(i < m_value.length());
     return jsSingleCharacterSubstring(exec, m_value, i);
 }
 
@@ -174,74 +242,68 @@ JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
     return const_cast<JSString*>(this);
 }
 
-bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result)
+bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
 {
     result = this;
-    number = value(exec).toDouble();
+    number = jsToNumber(value(exec));
     return false;
 }
 
-bool JSString::toBoolean(ExecState*) const
+bool JSString::toBoolean() const
 {
     return m_length;
 }
 
 double JSString::toNumber(ExecState* exec) const
 {
-    return value(exec).toDouble();
-}
-
-UString JSString::toString(ExecState* exec) const
-{
-    return value(exec);
+    return jsToNumber(value(exec));
 }
 
-inline StringObject* StringObject::create(ExecState* exec, JSString* string)
+inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
 {
-    return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
+    StringObject* object = new (NotNull, allocateCell<StringObject>(*exec->heap())) StringObject(exec->vm(), globalObject->stringObjectStructure());
+    object->finishCreation(exec->vm(), string);
+    return object;
 }
 
-JSObject* JSString::toObject(ExecState* exec) const
+JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
 {
-    return StringObject::create(exec, const_cast<JSString*>(this));
+    return StringObject::create(exec, globalObject, const_cast<JSString*>(this));
 }
 
-JSObject* JSString::toThisObject(ExecState* exec) const
+JSObject* JSString::toThisObject(JSCell* cell, ExecState* exec)
 {
-    return StringObject::create(exec, const_cast<JSString*>(this));
+    return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
 }
 
-bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
+bool JSString::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
 {
+    JSString* thisObject = jsCast<JSString*>(cell);
     // The semantics here are really getPropertySlot, not getOwnPropertySlot.
     // This function should only be called by JSValue::get.
-    if (getStringPropertySlot(exec, propertyName, slot))
-        return true;
-    if (propertyName == exec->propertyNames().underscoreProto) {
-        slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
+    if (thisObject->getStringPropertySlot(exec, propertyName, slot))
         return true;
-    }
-    slot.setBase(this);
+    slot.setBase(thisObject);
     JSObject* object;
     for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
         object = asObject(prototype);
-        if (object->getOwnPropertySlot(exec, propertyName, slot))
+        if (object->methodTable()->getOwnPropertySlot(object, exec, propertyName, slot))
             return true;
     }
     slot.setUndefined();
     return true;
 }
 
-bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
+bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
 {
     if (propertyName == exec->propertyNames().length) {
-        descriptor.setDescriptor(jsNumber(exec, m_length), DontEnum | DontDelete | ReadOnly);
+        descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
         return true;
     }
     
-    bool isStrictUInt32;
-    unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
-    if (isStrictUInt32 && i < m_length) {
+    unsigned i = propertyName.asIndex();
+    if (i < m_length) {
+        ASSERT(i != PropertyName::NotAnIndex); // No need for an explicit check, the above test would always fail!
         descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
         return true;
     }
@@ -249,23 +311,14 @@ bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& pr
     return false;
 }
 
-bool JSString::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
-{
-    if (getStringPropertyDescriptor(exec, propertyName, descriptor))
-        return true;
-    if (propertyName != exec->propertyNames().underscoreProto)
-        return false;
-    descriptor.setDescriptor(exec->lexicalGlobalObject()->stringPrototype(), DontEnum);
-    return true;
-}
-
-bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
+bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
 {
+    JSString* thisObject = jsCast<JSString*>(cell);
     // The semantics here are really getPropertySlot, not getOwnPropertySlot.
     // This function should only be called by JSValue::get.
-    if (getStringPropertySlot(exec, propertyName, slot))
+    if (thisObject->getStringPropertySlot(exec, propertyName, slot))
         return true;
-    return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
+    return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
 }
 
 } // namespace JSC