]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - runtime/JSString.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / JSString.cpp
index 86f95e0dbd62e1f2c3339adb627b313215d11da6..10a16d9733d73d5d42e17bd6f26b2fa27d18de21 100644 (file)
 #include "JSString.h"
 
 #include "JSGlobalObject.h"
+#include "JSGlobalObjectFunctions.h"
 #include "JSObject.h"
+#include "JSCInlines.h"
 #include "StringObject.h"
 #include "StringPrototype.h"
+#include "StrongInlines.h"
 
 namespace JSC {
+    
+const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
 
-JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
+void JSRopeString::RopeBuilder::expand()
 {
-    return const_cast<JSString*>(this);
+    ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
+    JSString* jsString = m_jsString;
+    RELEASE_ASSERT(jsString);
+    m_jsString = jsStringBuilder(&m_vm);
+    m_index = 0;
+    append(jsString);
 }
 
-bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue& value)
+void JSString::destroy(JSCell* cell)
 {
-    value = this;
-    number = m_value.toDouble();
-    return false;
+    JSString* thisObject = static_cast<JSString*>(cell);
+    thisObject->JSString::~JSString();
 }
 
-bool JSString::toBoolean(ExecState*) const
+void JSString::dumpToStream(const JSCell* cell, PrintStream& out)
 {
-    return !m_value.isEmpty();
+    const JSString* thisObject = jsCast<const JSString*>(cell);
+    out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(), thisObject->length());
+    if (thisObject->isRope())
+        out.printf("[rope]");
+    else {
+        WTF::StringImpl* ourImpl = thisObject->m_value.impl();
+        if (ourImpl->is8Bit())
+            out.printf("[8 %p]", ourImpl->characters8());
+        else
+            out.printf("[16 %p]", ourImpl->characters16());
+    }
+    out.printf(">");
 }
 
-double JSString::toNumber(ExecState*) const
+void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
 {
-    return m_value.toDouble();
+    JSString* thisObject = jsCast<JSString*>(cell);
+    Base::visitChildren(thisObject, visitor);
+    
+    if (thisObject->isRope())
+        static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
+    else {
+        StringImpl* impl = thisObject->m_value.impl();
+        ASSERT(impl);
+        visitor.reportExtraMemoryUsage(thisObject, impl->costDuringGC());
+    }
 }
 
-UString JSString::toString(ExecState*) const
+void JSRopeString::visitFibers(SlotVisitor& visitor)
 {
-    return m_value;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        visitor.append(&m_fibers[i]);
 }
 
-UString JSString::toThisString(ExecState*) const
+static const unsigned maxLengthForOnStackResolve = 2048;
+
+void JSRopeString::resolveRopeInternal8(LChar* buffer) const
 {
-    return m_value;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        if (m_fibers[i]->isRope()) {
+            resolveRopeSlowCase8(buffer);
+            return;
+        }
+    }
+
+    LChar* position = buffer;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        const StringImpl& fiberString = *m_fibers[i]->m_value.impl();
+        unsigned length = fiberString.length();
+        StringImpl::copyChars(position, fiberString.characters8(), length);
+        position += length;
+    }
+    ASSERT((buffer + m_length) == position);
 }
 
-JSString* JSString::toThisJSString(ExecState*)
+void JSRopeString::resolveRopeInternal16(UChar* buffer) const
 {
-    return this;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        if (m_fibers[i]->isRope()) {
+            resolveRopeSlowCase(buffer);
+            return;
+        }
+    }
+
+    UChar* position = buffer;
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
+        const StringImpl& fiberString = *m_fibers[i]->m_value.impl();
+        unsigned length = fiberString.length();
+        if (fiberString.is8Bit())
+            StringImpl::copyChars(position, fiberString.characters8(), length);
+        else
+            StringImpl::copyChars(position, fiberString.characters16(), length);
+        position += length;
+    }
+    ASSERT((buffer + m_length) == position);
 }
 
-inline StringObject* StringObject::create(ExecState* exec, JSString* string)
+void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const
 {
-    return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
+    if (m_length > maxLengthForOnStackResolve) {
+        resolveRope(exec);
+        m_value = AtomicString(m_value);
+        return;
+    }
+
+    if (is8Bit()) {
+        LChar buffer[maxLengthForOnStackResolve];
+        resolveRopeInternal8(buffer);
+        m_value = AtomicString(buffer, m_length);
+    } else {
+        UChar buffer[maxLengthForOnStackResolve];
+        resolveRopeInternal16(buffer);
+        m_value = AtomicString(buffer, m_length);
+    }
+
+    clearFibers();
+
+    // If we resolved a string that didn't previously exist, notify the heap that we've grown.
+    if (m_value.impl()->hasOneRef())
+        Heap::heap(this)->reportExtraMemoryCost(m_value.impl()->cost());
 }
 
-JSObject* JSString::toObject(ExecState* exec) const
+void JSRopeString::clearFibers() const
 {
-    return StringObject::create(exec, const_cast<JSString*>(this));
+    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
+        m_fibers[i].clear();
 }
 
-JSObject* JSString::toThisObject(ExecState* exec) const
+AtomicStringImpl* JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const
 {
-    return StringObject::create(exec, const_cast<JSString*>(this));
+    if (m_length > maxLengthForOnStackResolve) {
+        resolveRope(exec);
+        if (AtomicStringImpl* existingAtomicString = AtomicString::find(m_value.impl())) {
+            m_value = *existingAtomicString;
+            clearFibers();
+            return existingAtomicString;
+        }
+        return nullptr;
+    }
+
+    if (is8Bit()) {
+        LChar buffer[maxLengthForOnStackResolve];
+        resolveRopeInternal8(buffer);
+        if (AtomicStringImpl* existingAtomicString = AtomicString::find(buffer, m_length)) {
+            m_value = *existingAtomicString;
+            clearFibers();
+            return existingAtomicString;
+        }
+    } else {
+        UChar buffer[maxLengthForOnStackResolve];
+        resolveRopeInternal16(buffer);
+        if (AtomicStringImpl* existingAtomicString = AtomicString::find(buffer, m_length)) {
+            m_value = *existingAtomicString;
+            clearFibers();
+            return existingAtomicString;
+        }
+    }
+
+    return nullptr;
 }
 
-bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
+void JSRopeString::resolveRope(ExecState* exec) const
 {
-    // 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());
-        return true;
+    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;
+        }
+        resolveRopeInternal8(buffer);
+        clearFibers();
+        ASSERT(!isRope());
+        return;
     }
-    slot.setBase(this);
-    JSObject* object;
-    for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
-        object = asObject(prototype);
-        if (object->getOwnPropertySlot(exec, propertyName, slot))
-            return true;
+
+    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;
     }
-    slot.setUndefined();
-    return true;
+
+    resolveRopeInternal16(buffer);
+    clearFibers();
+    ASSERT(!isRope());
 }
 
-bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
+// 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
 {
-    // The semantics here are really getPropertySlot, not getOwnPropertySlot.
-    // This function should only be called by JSValue::get.
-    if (getStringPropertySlot(exec, propertyName, slot))
-        return true;
-    return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
+    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());
+
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
+
+        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;
+        }
+
+        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
+        unsigned length = string->length();
+        position -= length;
+        StringImpl::copyChars(position, string->characters8(), length);
+    }
+
+    ASSERT(buffer == position);
 }
 
-JSString* jsString(JSGlobalData* globalData, const UString& s)
+void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
 {
-    int size = s.size();
-    if (!size)
-        return globalData->smallStrings.emptyString(globalData);
-    if (size == 1) {
-        UChar c = s.data()[0];
-        if (c <= 0xFF)
-            return globalData->smallStrings.singleCharacterString(globalData, c);
+    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());
+
+    while (!workQueue.isEmpty()) {
+        JSString* currentFiber = workQueue.last();
+        workQueue.removeLast();
+
+        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;
+        }
+
+        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);
     }
-    return new (globalData) JSString(globalData, s);
+
+    ASSERT(buffer == position);
 }
-    
-JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length)
+
+void JSRopeString::outOfMemory(ExecState* exec) const
 {
-    ASSERT(offset <= static_cast<unsigned>(s.size()));
-    ASSERT(length <= static_cast<unsigned>(s.size()));
-    ASSERT(offset + length <= static_cast<unsigned>(s.size()));
-    if (!length)
-        return globalData->smallStrings.emptyString(globalData);
-    if (length == 1) {
-        UChar c = s.data()[offset];
-        if (c <= 0xFF)
-            return globalData->smallStrings.singleCharacterString(globalData, c);
-    }
-    return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, length));
+    clearFibers();
+    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 jsEmptyString(exec);
+    ASSERT(!isRope());
+    RELEASE_ASSERT(i < m_value.length());
+    return jsSingleCharacterSubstring(exec, m_value, i);
+}
+
+JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
+{
+    return const_cast<JSString*>(this);
+}
+
+bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
+{
+    result = this;
+    number = jsToNumber(value(exec));
+    return false;
 }
 
-JSString* jsOwnedString(JSGlobalData* globalData, const UString& s)
+bool JSString::toBoolean() const
 {
-    int size = s.size();
-    if (!size)
-        return globalData->smallStrings.emptyString(globalData);
-    if (size == 1) {
-        UChar c = s.data()[0];
-        if (c <= 0xFF)
-            return globalData->smallStrings.singleCharacterString(globalData, c);
+    return m_length;
+}
+
+double JSString::toNumber(ExecState* exec) const
+{
+    return jsToNumber(value(exec));
+}
+
+inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string)
+{
+    StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure());
+    object->finishCreation(vm, string);
+    return object;
+}
+
+JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
+{
+    return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this));
+}
+
+JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode)
+{
+    if (ecmaMode == StrictMode)
+        return cell;
+    return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
+}
+
+bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
+{
+    if (propertyName == exec->propertyNames().length) {
+        descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
+        return true;
     }
-    return new (globalData) JSString(globalData, s, JSString::HasOtherOwner);
+    
+    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;
+    }
+    
+    return false;
+}
+
+JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl)
+{
+    auto addResult = vm.stringCache.add(&stringImpl, nullptr);
+    if (addResult.isNewEntry)
+        addResult.iterator->value = jsString(&vm, String(stringImpl));
+    vm.lastCachedString.set(vm, addResult.iterator->value.get());
+    return addResult.iterator->value.get();
 }
 
 } // namespace JSC