X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/runtime/JSString.cpp diff --git a/runtime/JSString.cpp b/runtime/JSString.cpp index da15997..eb046ed 100644 --- a/runtime/JSString.cpp +++ b/runtime/JSString.cpp @@ -26,247 +26,343 @@ #include "JSGlobalObject.h" #include "JSGlobalObjectFunctions.h" #include "JSObject.h" -#include "Operations.h" +#include "JSCInlines.h" #include "StringObject.h" #include "StringPrototype.h" +#include "StrongInlines.h" namespace JSC { -static const unsigned substringFromRopeCutoff = 4; +const ClassInfo JSString::s_info = { "string", 0, 0, CREATE_METHOD_TABLE(JSString) }; -const ClassInfo JSString::s_info = { "string", 0, 0, 0 }; +void JSRopeString::RopeBuilder::expand() +{ + ASSERT(m_index == JSRopeString::s_maxInternalRopeLength); + JSString* jsString = m_jsString; + RELEASE_ASSERT(jsString); + m_jsString = jsStringBuilder(&m_vm); + m_index = 0; + append(jsString); +} -void JSString::resolveRope(ExecState* exec) const +void JSString::destroy(JSCell* cell) { - ASSERT(isRope()); + JSString* thisObject = static_cast(cell); + thisObject->JSString::~JSString(); +} - UChar* buffer; - if (PassRefPtr newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) - m_value = newImpl; +void JSString::dumpToStream(const JSCell* cell, PrintStream& out) +{ + const JSString* thisObject = jsCast(cell); + out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(), thisObject->length()); + if (thisObject->isRope()) + out.printf("[rope]"); else { - outOfMemory(exec); - return; + 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(">"); +} - RopeImpl::Fiber currentFiber = m_fibers[0]; +void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor) +{ + JSString* thisObject = jsCast(cell); + Base::visitChildren(thisObject, visitor); + + if (thisObject->isRope()) + static_cast(thisObject)->visitFibers(visitor); + else { + StringImpl* impl = thisObject->m_value.impl(); + ASSERT(impl); + visitor.reportExtraMemoryVisited(thisObject, impl->costDuringGC()); + } +} - if ((m_fiberCount > 2) || (RopeImpl::isRope(currentFiber)) - || ((m_fiberCount == 2) && (RopeImpl::isRope(m_fibers[1])))) { - resolveRopeSlowCase(exec, buffer); +void JSRopeString::visitFibers(SlotVisitor& visitor) +{ + if (isSubstring()) { + visitor.append(&substringBase()); return; } + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) + visitor.append(&fiber(i)); +} - UChar* position = buffer; - StringImpl* string = static_cast(currentFiber); - unsigned length = string->length(); - StringImpl::copyChars(position, string->characters(), length); +static const unsigned maxLengthForOnStackResolve = 2048; - if (m_fiberCount > 1) { - position += length; - currentFiber = m_fibers[1]; - string = static_cast(currentFiber); - length = string->length(); - StringImpl::copyChars(position, string->characters(), length); - position += length; +void JSRopeString::resolveRopeInternal8(LChar* buffer) const +{ + if (isSubstring()) { + StringImpl::copyChars( + buffer, substringBase()->m_value.characters8() + substringOffset(), m_length); + return; } + + resolveRopeInternal8NoSubstring(buffer); +} - ASSERT((buffer + m_length) == position); - for (unsigned i = 0; i < m_fiberCount; ++i) { - RopeImpl::deref(m_fibers[i]); - m_fibers[i] = 0; +void JSRopeString::resolveRopeInternal8NoSubstring(LChar* buffer) const +{ + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { + if (fiber(i)->isRope()) { + resolveRopeSlowCase8(buffer); + return; + } } - m_fiberCount = 0; - ASSERT(!isRope()); + LChar* position = buffer; + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { + const StringImpl& fiberString = *fiber(i)->m_value.impl(); + unsigned length = fiberString.length(); + StringImpl::copyChars(position, fiberString.characters8(), length); + position += length; + } + ASSERT((buffer + m_length) == position); } -// 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 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 JSString::resolveRopeSlowCase(ExecState* exec, UChar* buffer) const +void JSRopeString::resolveRopeInternal16(UChar* buffer) const { - UNUSED_PARAM(exec); - - UChar* position = buffer + m_length; - - // Start with the current RopeImpl. - Vector 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(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(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; - } + if (isSubstring()) { + StringImpl::copyChars( + buffer, substringBase()->m_value.characters16() + substringOffset(), m_length); + return; + } + + resolveRopeInternal16NoSubstring(buffer); +} - // No! - set the next item up to process. - currentFiber = workQueue.last(); - workQueue.removeLast(); +void JSRopeString::resolveRopeInternal16NoSubstring(UChar* buffer) const +{ + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { + if (fiber(i)->isRope()) { + resolveRopeSlowCase(buffer); + return; } } + + UChar* position = buffer; + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) { + const StringImpl& fiberString = *fiber(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); } -void JSString::outOfMemory(ExecState* exec) const +void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const { - for (unsigned i = 0; i < m_fiberCount; ++i) { - RopeImpl::deref(m_fibers[i]); - m_fibers[i] = 0; + if (m_length > maxLengthForOnStackResolve) { + resolveRope(exec); + m_value = AtomicString(m_value); + setIs8Bit(m_value.impl()->is8Bit()); + return; } - m_fiberCount = 0; - ASSERT(!isRope()); - ASSERT(m_value == UString()); - if (exec) - throwOutOfMemoryError(exec); + + if (is8Bit()) { + LChar buffer[maxLengthForOnStackResolve]; + resolveRopeInternal8(buffer); + m_value = AtomicString(buffer, m_length); + setIs8Bit(m_value.impl()->is8Bit()); + } else { + UChar buffer[maxLengthForOnStackResolve]; + resolveRopeInternal16(buffer); + m_value = AtomicString(buffer, m_length); + setIs8Bit(m_value.impl()->is8Bit()); + } + + 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)->reportExtraMemoryAllocated(m_value.impl()->cost()); } -// 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) +void JSRopeString::clearFibers() const { - ASSERT(isRope()); - ASSERT(substringLength); - - JSGlobalData* globalData = &exec->globalData(); + for (size_t i = 0; i < s_maxInternalRopeLength; ++i) + u[i].number = 0; +} - UString substringFibers[3]; +RefPtr JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const +{ + if (m_length > maxLengthForOnStackResolve) { + resolveRope(exec); + if (RefPtr existingAtomicString = AtomicStringImpl::lookUp(m_value.impl())) { + m_value = *existingAtomicString; + setIs8Bit(m_value.impl()->is8Bit()); + clearFibers(); + return existingAtomicString; + } + return nullptr; + } - 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) - 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); + if (is8Bit()) { + LChar buffer[maxLengthForOnStackResolve]; + resolveRopeInternal8(buffer); + if (RefPtr existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { + m_value = *existingAtomicString; + setIs8Bit(m_value.impl()->is8Bit()); + clearFibers(); + return existingAtomicString; + } + } else { + UChar buffer[maxLengthForOnStackResolve]; + resolveRopeInternal16(buffer); + if (RefPtr existingAtomicString = AtomicStringImpl::lookUp(buffer, m_length)) { + m_value = *existingAtomicString; + setIs8Bit(m_value.impl()->is8Bit()); + clearFibers(); + return existingAtomicString; } } - 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); - } - 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]); + return nullptr; } -JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement) +void JSRopeString::resolveRope(ExecState* exec) 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)); + ASSERT(isRope()); + + if (isSubstring()) { + ASSERT(!substringBase()->isRope()); + m_value = substringBase()->m_value.substring(substringOffset(), m_length); + substringBase().clear(); + return; } - - 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; + if (is8Bit()) { + LChar* buffer; + if (RefPtr newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { + Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); + m_value = newImpl.release(); + } else { + outOfMemory(exec); + return; + } + resolveRopeInternal8NoSubstring(buffer); + clearFibers(); + ASSERT(!isRope()); + return; + } - StringImpl* string = *it; - matchPosition = string->find(character); - if (matchPosition == notFound) - continue; - matchString = string; + UChar* buffer; + if (RefPtr newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) { + Heap::heap(this)->reportExtraMemoryAllocated(newImpl->cost()); + m_value = newImpl.release(); + } else { + outOfMemory(exec); + return; } - if (!matchString) - return this; + resolveRopeInternal16NoSubstring(buffer); + clearFibers(); + ASSERT(!isRope()); +} - RopeBuilder builder(replacement.length() ? fiberCount + 2 : fiberCount + 1); - if (UNLIKELY(builder.isOutOfMemory())) - return throwOutOfMemoryError(exec); +// 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 +{ + LChar* position = buffer + m_length; // We will be working backwards over the rope. + Vector 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 && fiber(i); ++i) + workQueue.append(fiber(i).get()); + + while (!workQueue.isEmpty()) { + JSString* currentFiber = workQueue.last(); + workQueue.removeLast(); + + const LChar* characters; + + if (currentFiber->isRope()) { + JSRopeString* currentFiberAsRope = static_cast(currentFiber); + if (!currentFiberAsRope->isSubstring()) { + for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) + workQueue.append(currentFiberAsRope->fiber(i).get()); + continue; + } + ASSERT(!currentFiberAsRope->substringBase()->isRope()); + characters = + currentFiberAsRope->substringBase()->m_value.characters8() + + currentFiberAsRope->substringOffset(); + } else + characters = currentFiber->m_value.characters8(); + + unsigned length = currentFiber->length(); + position -= length; + StringImpl::copyChars(position, characters, length); + } + + ASSERT(buffer == position); +} - for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) { - StringImpl* string = *it; - if (string != matchString) { - builder.append(UString(string)); +void JSRopeString::resolveRopeSlowCase(UChar* buffer) const +{ + UChar* position = buffer + m_length; // We will be working backwards over the rope. + Vector workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK. + + for (size_t i = 0; i < s_maxInternalRopeLength && fiber(i); ++i) + workQueue.append(fiber(i).get()); + + while (!workQueue.isEmpty()) { + JSString* currentFiber = workQueue.last(); + workQueue.removeLast(); + + if (currentFiber->isRope()) { + JSRopeString* currentFiberAsRope = static_cast(currentFiber); + if (currentFiberAsRope->isSubstring()) { + ASSERT(!currentFiberAsRope->substringBase()->isRope()); + StringImpl* string = static_cast( + currentFiberAsRope->substringBase()->m_value.impl()); + unsigned offset = currentFiberAsRope->substringOffset(); + unsigned length = currentFiberAsRope->length(); + position -= length; + if (string->is8Bit()) + StringImpl::copyChars(position, string->characters8() + offset, length); + else + StringImpl::copyChars(position, string->characters16() + offset, length); + continue; + } + for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->fiber(i); ++i) + workQueue.append(currentFiberAsRope->fiber(i).get()); 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(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); } -JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i) +void JSRopeString::outOfMemory(ExecState* exec) const { + clearFibers(); 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, ""); - ASSERT(!isRope()); - ASSERT(i < m_value.length()); - return jsSingleCharacterSubstring(exec, m_value, i); + ASSERT(m_value.isNull()); + if (exec) + throwOutOfMemoryError(exec); } JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const @@ -274,98 +370,61 @@ JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const return const_cast(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)); + number = jsToNumber(view(exec)); return false; } -bool JSString::toBoolean(ExecState*) const -{ - return m_length; -} - double JSString::toNumber(ExecState* exec) const { - return jsToNumber(value(exec)); -} - -UString JSString::toString(ExecState* exec) const -{ - return value(exec); + return jsToNumber(view(exec)); } -inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string) +inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string) { - return new (exec) StringObject(exec->globalData(), globalObject->stringObjectStructure(), string); + StringObject* object = new (NotNull, allocateCell(vm.heap)) StringObject(vm, globalObject->stringObjectStructure()); + object->finishCreation(vm, string); + return object; } JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const { - return StringObject::create(exec, globalObject, const_cast(this)); + return StringObject::create(exec->vm(), globalObject, const_cast(this)); } -JSObject* JSString::toThisObject(ExecState* exec) const +JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode) { - return StringObject::create(exec, exec->lexicalGlobalObject(), const_cast(this)); + if (ecmaMode == StrictMode) + return cell; + return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast(cell)); } -bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) -{ - // 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; - } - 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; - } - 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(m_length), DontEnum | DontDelete | ReadOnly); return true; } - bool isStrictUInt32; - unsigned i = propertyName.toUInt32(isStrictUInt32); - if (isStrictUInt32 && i < m_length) { - descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly); + Optional index = parseIndex(propertyName); + if (index && index.value() < m_length) { + descriptor.setDescriptor(getIndex(exec, index.value()), DontDelete | ReadOnly); return true; } return false; } -bool JSString::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) +JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl) { - if (getStringPropertyDescriptor(exec, propertyName, descriptor)) - return true; - if (propertyName != exec->propertyNames().underscoreProto) - return false; - descriptor.setDescriptor(exec->lexicalGlobalObject()->stringPrototype(), DontEnum); - return true; -} + if (JSString* string = vm.stringCache.get(&stringImpl)) + return string; -bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) -{ - // 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); + JSString* string = jsString(&vm, String(stringImpl)); + vm.lastCachedString.set(vm, string); + return string; } } // namespace JSC