X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b80e619319b1def83d1e8b4f84042b661be1be7f..14957cd040308e3eeec43d26bae5d76da13fcd85:/runtime/JSString.cpp diff --git a/runtime/JSString.cpp b/runtime/JSString.cpp index 6128790..da15997 100644 --- a/runtime/JSString.cpp +++ b/runtime/JSString.cpp @@ -24,6 +24,7 @@ #include "JSString.h" #include "JSGlobalObject.h" +#include "JSGlobalObjectFunctions.h" #include "JSObject.h" #include "Operations.h" #include "StringObject.h" @@ -31,7 +32,53 @@ namespace JSC { -static const unsigned resolveRopeForSubstringCutoff = 4; +static const unsigned substringFromRopeCutoff = 4; + +const ClassInfo JSString::s_info = { "string", 0, 0, 0 }; + +void JSString::resolveRope(ExecState* exec) const +{ + ASSERT(isRope()); + + UChar* buffer; + if (PassRefPtr newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) + m_value = newImpl; + else { + 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; + } + + UChar* position = buffer; + StringImpl* string = static_cast(currentFiber); + unsigned length = string->length(); + StringImpl::copyChars(position, string->characters(), length); + + 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; + } + + 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()); +} // 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 @@ -39,38 +86,22 @@ static const unsigned resolveRopeForSubstringCutoff = 4; // 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 +// 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::resolveRope(ExecState* exec) const +// rope-of-ropes.) +void JSString::resolveRopeSlowCase(ExecState* exec, UChar* buffer) const { - ASSERT(isRope()); + UNUSED_PARAM(exec); - // 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]; + workQueue.append(m_fibers[i]); + currentFiber = m_fibers[m_fiberCount - 1]; while (true) { if (RopeImpl::isRope(currentFiber)) { RopeImpl* rope = static_cast(currentFiber); @@ -81,21 +112,21 @@ void JSString::resolveRope(ExecState* exec) const workQueue.append(rope->fibers()[i]); currentFiber = rope->fibers()[fiberCountMinusOne]; } else { - UStringImpl* string = static_cast(currentFiber); + StringImpl* string = static_cast(currentFiber); unsigned length = string->length(); position -= length; - UStringImpl::copyChars(position, string->characters(), 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_other.m_fibers[i]); - m_other.m_fibers[i] = 0; + RopeImpl::deref(m_fibers[i]); + m_fibers[i] = 0; } m_fiberCount = 0; - + ASSERT(!isRope()); return; } @@ -106,14 +137,28 @@ void JSString::resolveRope(ExecState* exec) const } } } - + +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(); UString substringFibers[3]; @@ -124,9 +169,9 @@ JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, unsigned fiberEnd = 0; RopeIterator end; - for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) { + for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) { ++fiberCount; - UStringImpl* fiberString = *it; + StringImpl* fiberString = *it; unsigned fiberStart = fiberEnd; fiberEnd = fiberStart + fiberString->length(); if (fiberEnd <= substringStart) @@ -136,10 +181,10 @@ JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, if (copyStart == fiberStart && copyEnd == fiberEnd) substringFibers[substringFiberCount++] = UString(fiberString); else - substringFibers[substringFiberCount++] = UString(UStringImpl::create(fiberString, copyStart - fiberStart, copyEnd - copyStart)); + substringFibers[substringFiberCount++] = UString(StringImpl::create(fiberString, copyStart - fiberStart, copyEnd - copyStart)); if (fiberEnd >= substringEnd) break; - if (fiberCount > resolveRopeForSubstringCutoff || substringFiberCount >= 3) { + 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); @@ -149,8 +194,8 @@ JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, if (substringLength == 1) { ASSERT(substringFiberCount == 1); - UChar c = substringFibers[0].data()[0]; - if (c <= 0xFF) + UChar c = substringFibers[0].characters()[0]; + if (c <= maxSingleCharacterString) return globalData->smallStrings.singleCharacterString(globalData, c); } if (substringFiberCount == 1) @@ -163,26 +208,26 @@ JSString* JSString::substringFromRope(ExecState* exec, unsigned substringStart, JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement) { if (!isRope()) { - unsigned matchPosition = m_value.find(character); - if (matchPosition == UString::NotFound) + size_t matchPosition = m_value.find(character); + if (matchPosition == notFound) return JSValue(this); - return jsString(exec, m_value.substr(0, matchPosition), replacement, m_value.substr(matchPosition + 1)); + 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; - UStringImpl* matchString = 0; - int matchPosition = -1; - for (RopeIterator it(m_other.m_fibers, m_fiberCount); it != end; ++it) { + StringImpl* matchString = 0; + size_t matchPosition = notFound; + for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) { ++fiberCount; if (matchString) continue; - UStringImpl* string = *it; + StringImpl* string = *it; matchPosition = string->find(character); - if (matchPosition == -1) + if (matchPosition == notFound) continue; matchString = string; } @@ -190,21 +235,21 @@ JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UStri if (!matchString) return this; - RopeBuilder builder(replacement.size() ? fiberCount + 2 : fiberCount + 1); + RopeBuilder builder(replacement.length() ? 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; + for (RopeIterator it(m_fibers.data(), m_fiberCount); it != end; ++it) { + StringImpl* string = *it; if (string != matchString) { builder.append(UString(string)); continue; } - builder.append(UString(string).substr(0, matchPosition)); - if (replacement.size()) + builder.append(UString(string).substringSharingImpl(0, matchPosition)); + if (replacement.length()) builder.append(replacement); - builder.append(UString(string).substr(matchPosition + 1)); + builder.append(UString(string).substringSharingImpl(matchPosition + 1)); matchString = 0; } @@ -220,7 +265,7 @@ JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i) if (exec->exception()) return jsString(exec, ""); ASSERT(!isRope()); - ASSERT(i < m_value.size()); + ASSERT(i < m_value.length()); return jsSingleCharacterSubstring(exec, m_value, i); } @@ -232,7 +277,7 @@ JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) { result = this; - number = value(exec).toDouble(); + number = jsToNumber(value(exec)); return false; } @@ -243,7 +288,7 @@ bool JSString::toBoolean(ExecState*) const double JSString::toNumber(ExecState* exec) const { - return value(exec).toDouble(); + return jsToNumber(value(exec)); } UString JSString::toString(ExecState* exec) const @@ -251,19 +296,19 @@ UString JSString::toString(ExecState* exec) const return 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); + return new (exec) StringObject(exec->globalData(), globalObject->stringObjectStructure(), string); } -JSObject* JSString::toObject(ExecState* exec) const +JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const { - return StringObject::create(exec, const_cast(this)); + return StringObject::create(exec, globalObject, const_cast(this)); } JSObject* JSString::toThisObject(ExecState* exec) const { - return StringObject::create(exec, const_cast(this)); + return StringObject::create(exec, exec->lexicalGlobalObject(), const_cast(this)); } bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) @@ -290,12 +335,12 @@ bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyNam bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& 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); + unsigned i = propertyName.toUInt32(isStrictUInt32); if (isStrictUInt32 && i < m_length) { descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly); return true;