X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/ba379fdc102753d6be2c4d937058fe40257329fe..b80e619319b1def83d1e8b4f84042b661be1be7f:/runtime/JSString.cpp?ds=sidebyside diff --git a/runtime/JSString.cpp b/runtime/JSString.cpp index 86f95e0..6128790 100644 --- a/runtime/JSString.cpp +++ b/runtime/JSString.cpp @@ -25,46 +25,230 @@ #include "JSGlobalObject.h" #include "JSObject.h" +#include "Operations.h" #include "StringObject.h" #include "StringPrototype.h" namespace JSC { + +static const unsigned resolveRopeForSubstringCutoff = 4; -JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const +// 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 { - return const_cast(this); + ASSERT(isRope()); + + // Allocate the buffer to hold the final string, position initially points to the end. + UChar* buffer; + if (PassRefPtr 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; + } + m_fiberCount = 0; + ASSERT(!isRope()); + ASSERT(m_value == UString()); + if (exec) + throwOutOfMemoryError(exec); + return; + } + 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_other.m_fibers[i]); + currentFiber = m_other.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 { + UStringImpl* string = static_cast(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(); + } + } +} + +// 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()); + + JSGlobalData* globalData = &exec->globalData(); + + UString substringFibers[3]; + + unsigned fiberCount = 0; + unsigned substringFiberCount = 0; + unsigned substringEnd = substringStart + substringLength; + unsigned fiberEnd = 0; + + RopeIterator end; + for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) { + ++fiberCount; + UStringImpl* 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(UStringImpl::create(fiberString, copyStart - fiberStart, copyEnd - copyStart)); + if (fiberEnd >= substringEnd) + break; + if (fiberCount > resolveRopeForSubstringCutoff || 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].data()[0]; + if (c <= 0xFF) + 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]); } -bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue& value) +JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement) { - value = this; - number = m_value.toDouble(); - return false; + 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)); + } + + 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; + + UStringImpl* string = *it; + matchPosition = string->find(character); + if (matchPosition == -1) + continue; + matchString = string; + } + + if (!matchString) + return this; + + RopeBuilder builder(replacement.size() ? fiberCount + 2 : fiberCount + 1); + if (UNLIKELY(builder.isOutOfMemory())) + return throwOutOfMemoryError(exec); + + for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) { + UStringImpl* string = *it; + if (string != matchString) { + builder.append(UString(string)); + continue; + } + + builder.append(UString(string).substr(0, matchPosition)); + if (replacement.size()) + builder.append(replacement); + builder.append(UString(string).substr(matchPosition + 1)); + matchString = 0; + } + + JSGlobalData* globalData = &exec->globalData(); + return JSValue(new (globalData) JSString(globalData, builder.release())); } -bool JSString::toBoolean(ExecState*) const +JSString* JSString::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, ""); + ASSERT(!isRope()); + ASSERT(i < m_value.size()); + return jsSingleCharacterSubstring(exec, m_value, i); +} + +JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const { - return !m_value.isEmpty(); + return const_cast(this); } -double JSString::toNumber(ExecState*) const +bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) { - return m_value.toDouble(); + result = this; + number = value(exec).toDouble(); + return false; } -UString JSString::toString(ExecState*) const +bool JSString::toBoolean(ExecState*) const { - return m_value; + return m_length; } -UString JSString::toThisString(ExecState*) const +double JSString::toNumber(ExecState* exec) const { - return m_value; + return value(exec).toDouble(); } -JSString* JSString::toThisJSString(ExecState*) +UString JSString::toString(ExecState* exec) const { - return this; + return value(exec); } inline StringObject* StringObject::create(ExecState* exec, JSString* string) @@ -103,54 +287,40 @@ bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyNam return true; } -bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) +bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) { - // The semantics here are really getPropertySlot, not getOwnPropertySlot. - // This function should only be called by JSValue::get. - if (getStringPropertySlot(exec, propertyName, slot)) + if (propertyName == exec->propertyNames().length) { + descriptor.setDescriptor(jsNumber(exec, m_length), DontEnum | DontDelete | ReadOnly); return true; - return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot); -} - -JSString* jsString(JSGlobalData* globalData, const UString& s) -{ - 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 new (globalData) JSString(globalData, s); -} -JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length) -{ - ASSERT(offset <= static_cast(s.size())); - ASSERT(length <= static_cast(s.size())); - ASSERT(offset + length <= static_cast(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); + bool isStrictUInt32; + unsigned i = propertyName.toStrictUInt32(&isStrictUInt32); + if (isStrictUInt32 && i < m_length) { + descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly); + return true; } - return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, length)); + + return false; } -JSString* jsOwnedString(JSGlobalData* globalData, const UString& s) +bool JSString::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) { - 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 new (globalData) JSString(globalData, s, JSString::HasOtherOwner); + 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) +{ + // 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); } } // namespace JSC