]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/JSString.cpp
JavaScriptCore-1097.3.tar.gz
[apple/javascriptcore.git] / runtime / JSString.cpp
index da15997d32db78aed7b57f07f0cc4b529331f010..904cc4d3e51fa4d91f5681a8d02de042b78d1d73 100644 (file)
@@ -34,53 +34,98 @@ namespace JSC {
     
 static const unsigned substringFromRopeCutoff = 4;
 
     
 static const unsigned substringFromRopeCutoff = 4;
 
-const ClassInfo JSString::s_info = { "string", 0, 0, 0 };
+const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
 
 
-void JSString::resolveRope(ExecState* exec) const
+void JSRopeString::RopeBuilder::expand()
+{
+    ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
+    JSString* jsString = m_jsString;
+    m_jsString = jsStringBuilder(&m_globalData);
+    m_index = 0;
+    append(jsString);
+}
+
+void JSString::destroy(JSCell* cell)
+{
+    JSString* thisObject = jsCast<JSString*>(cell);
+    thisObject->JSString::~JSString();
+}
+
+void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
+{
+    JSString* thisObject = jsCast<JSString*>(cell);
+    Base::visitChildren(thisObject, visitor);
+    
+    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());
 
 {
     ASSERT(isRope());
 
+    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();
+        }
+        ASSERT((buffer + m_length) == position);
+        ASSERT(!isRope());
+
+        return;
+    }
+
     UChar* buffer;
     UChar* buffer;
-    if (PassRefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer))
-        m_value = newImpl;
-    else {
+    if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
+        Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
+        m_value = newImpl.release();
+    } else {
         outOfMemory(exec);
         return;
     }
 
         outOfMemory(exec);
         return;
     }
 
-    RopeImpl::Fiber currentFiber = m_fibers[0];
-
-    if ((m_fiberCount > 2) || (RopeImpl::isRope(currentFiber)) 
-        || ((m_fiberCount == 2) && (RopeImpl::isRope(m_fibers[1])))) {
-        resolveRopeSlowCase(exec, buffer);
-        return;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        if (m_fibers[i]->isRope())
+            return resolveRopeSlowCase(buffer);
     }
 
     UChar* position = buffer;
     }
 
     UChar* position = buffer;
-    StringImpl* string = static_cast<StringImpl*>(currentFiber);
-    unsigned length = string->length();
-    StringImpl::copyChars(position, string->characters(), length);
-
-    if (m_fiberCount > 1) {
-        position += length;
-        currentFiber = m_fibers[1];
-        string = static_cast<StringImpl*>(currentFiber);
-        length = string->length();
+    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->characters(), length);
         position += length;
         StringImpl::copyChars(position, string->characters(), length);
         position += length;
+        m_fibers[i].clear();
     }
     }
-
     ASSERT((buffer + m_length) == position);
     ASSERT((buffer + m_length) == position);
-    for (unsigned i = 0; i < m_fiberCount; ++i) {
-        RopeImpl::deref(m_fibers[i]);
-        m_fibers[i] = 0;
-    }
-    m_fiberCount = 0;
-
     ASSERT(!isRope());
 }
 
     ASSERT(!isRope());
 }
 
-// Overview: this methods converts a JSString from holding a string in rope form
+// Overview: These functions convert 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
 // 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
@@ -90,174 +135,78 @@ void JSString::resolveRope(ExecState* exec) const
 // 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.)    
 // 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::resolveRopeSlowCase(ExecState* exec, UChar* buffer) const
+void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
 {
 {
-    UNUSED_PARAM(exec);
-
-    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_fibers[i]);
-    currentFiber = 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 {
-            StringImpl* string = static_cast<StringImpl*>(currentFiber);
-            unsigned length = string->length();
-            position -= length;
-            StringImpl::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_fibers[i]);
-                    m_fibers[i] = 0;
-                }
-                m_fiberCount = 0;
-                
-                ASSERT(!isRope());
-                return;
-            }
-
-            // No! - set the next item up to process.
-            currentFiber = workQueue.last();
-            workQueue.removeLast();
-        }
+    LChar* position = buffer + m_length; // We will be working backwards over the rope.
+    Vector<JSString*, 32> 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();
     }
     }
-}
 
 
-void JSString::outOfMemory(ExecState* exec) const
-{
-    for (unsigned i = 0; i < m_fiberCount; ++i) {
-        RopeImpl::deref(m_fibers[i]);
-        m_fibers[i] = 0;
-    }
-    m_fiberCount = 0;
-    ASSERT(!isRope());
-    ASSERT(m_value == UString());
-    if (exec)
-        throwOutOfMemoryError(exec);
-}
-
-// This function construsts a substring out of a rope without flattening by reusing the existing fibers.
-// This can reduce memory usage substantially. Since traversing ropes is slow the function will revert 
-// back to flattening if the rope turns out to be long.
-JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, unsigned substringLength)
-{
-    ASSERT(isRope());
-    ASSERT(substringLength);
-    
-    JSGlobalData* globalData = &exec->globalData();
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
 
 
-    UString substringFibers[3];
-    
-    unsigned fiberCount = 0;
-    unsigned substringFiberCount = 0;
-    unsigned substringEnd = substringStart + substringLength;
-    unsigned fiberEnd = 0;
-
-    RopeIterator end;
-    for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) {
-        ++fiberCount;
-        StringImpl* fiberString = *it;
-        unsigned fiberStart = fiberEnd;
-        fiberEnd = fiberStart + fiberString->length();
-        if (fiberEnd <= substringStart)
+        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;
             continue;
-        unsigned copyStart = std::max(substringStart, fiberStart);
-        unsigned copyEnd = std::min(substringEnd, fiberEnd);
-        if (copyStart == fiberStart && copyEnd == fiberEnd)
-            substringFibers[substringFiberCount++] = UString(fiberString);
-        else
-            substringFibers[substringFiberCount++] = UString(StringImpl::create(fiberString, copyStart - fiberStart, copyEnd - copyStart));
-        if (fiberEnd >= substringEnd)
-            break;
-        if (fiberCount > substringFromRopeCutoff || substringFiberCount >= 3) {
-            // This turned out to be a really inefficient rope. Just flatten it.
-            resolveRope(exec);
-            return jsSubstring(&exec->globalData(), m_value, substringStart, substringLength);
         }
         }
-    }
-    ASSERT(substringFiberCount && substringFiberCount <= 3);
 
 
-    if (substringLength == 1) {
-        ASSERT(substringFiberCount == 1);
-        UChar c = substringFibers[0].characters()[0];
-        if (c <= maxSingleCharacterString)
-            return globalData->smallStrings.singleCharacterString(globalData, c);
+        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
+        unsigned length = string->length();
+        position -= length;
+        StringImpl::copyChars(position, string->characters8(), length);
     }
     }
-    if (substringFiberCount == 1)
-        return new (globalData) JSString(globalData, substringFibers[0]);
-    if (substringFiberCount == 2)
-        return new (globalData) JSString(globalData, substringFibers[0], substringFibers[1]);
-    return new (globalData) JSString(globalData, substringFibers[0], substringFibers[1], substringFibers[2]);
+
+    ASSERT(buffer == position);
+    ASSERT(!isRope());
 }
 
 }
 
-JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement)
+void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
 {
 {
-    if (!isRope()) {
-        size_t matchPosition = m_value.find(character);
-        if (matchPosition == notFound)
-            return JSValue(this);
-        return jsString(exec, m_value.substringSharingImpl(0, matchPosition), replacement, m_value.substringSharingImpl(matchPosition + 1));
-    }
-
-    RopeIterator end;
-    
-    // Count total fibers and find matching string.
-    size_t fiberCount = 0;
-    StringImpl* matchString = 0;
-    size_t matchPosition = notFound;
-    for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) {
-        ++fiberCount;
-        if (matchString)
-            continue;
-
-        StringImpl* string = *it;
-        matchPosition = string->find(character);
-        if (matchPosition == notFound)
-            continue;
-        matchString = string;
-    }
+    UChar* position = buffer + m_length; // We will be working backwards over the rope.
+    Vector<JSString*, 32> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
 
 
-    if (!matchString)
-        return this;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        workQueue.append(m_fibers[i].get());
 
 
-    RopeBuilder builder(replacement.length() ? fiberCount + 2 : fiberCount + 1);
-    if (UNLIKELY(builder.isOutOfMemory()))
-        return throwOutOfMemoryError(exec);
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
 
 
-    for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) {
-        StringImpl* 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;
         }
 
             continue;
         }
 
-        builder.append(UString(string).substringSharingImpl(0, matchPosition));
-        if (replacement.length())
-            builder.append(replacement);
-        builder.append(UString(string).substringSharingImpl(matchPosition + 1));
-        matchString = 0;
+        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
+        unsigned length = string->length();
+        position -= length;
+        StringImpl::copyChars(position, string->characters(), length);
     }
 
     }
 
-    JSGlobalData* globalData = &exec->globalData();
-    return JSValue(new (globalData) JSString(globalData, builder.release()));
+    ASSERT(buffer == position);
+    ASSERT(!isRope());
+}
+
+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 == UString());
+    if (exec)
+        throwOutOfMemoryError(exec);
 }
 
 }
 
-JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i)
+JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i)
 {
     ASSERT(isRope());
     resolveRope(exec);
 {
     ASSERT(isRope());
     resolveRope(exec);
@@ -274,7 +223,7 @@ JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
     return const_cast<JSString*>(this);
 }
 
     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 = jsToNumber(value(exec));
 {
     result = this;
     number = jsToNumber(value(exec));
@@ -291,14 +240,11 @@ double JSString::toNumber(ExecState* exec) const
     return jsToNumber(value(exec));
 }
 
     return jsToNumber(value(exec));
 }
 
-UString JSString::toString(ExecState* exec) const
-{
-    return value(exec);
-}
-
 inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
 {
 inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
 {
-    return new (exec) StringObject(exec->globalData(), globalObject->stringObjectStructure(), string);
+    StringObject* object = new (NotNull, allocateCell<StringObject>(*exec->heap())) StringObject(exec->globalData(), globalObject->stringObjectStructure());
+    object->finishCreation(exec->globalData(), string);
+    return object;
 }
 
 JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
 }
 
 JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
@@ -306,26 +252,23 @@ JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) cons
     return StringObject::create(exec, globalObject, 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, exec->lexicalGlobalObject(), 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, const Identifier& 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.
     // 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;
         return true;
-    }
-    slot.setBase(this);
+    slot.setBase(thisObject);
     JSObject* object;
     for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
         object = asObject(prototype);
     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;
     }
     slot.setUndefined();
@@ -349,23 +292,14 @@ bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& pr
     return false;
 }
 
     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.
     // 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 true;
-    return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
+    return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
 }
 
 } // namespace JSC
 }
 
 } // namespace JSC