X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..6fe7ccc865dc7d7541b93c5bcaf6368d2c98a174:/runtime/JSString.cpp diff --git a/runtime/JSString.cpp b/runtime/JSString.cpp index 1e23a15..904cc4d 100644 --- a/runtime/JSString.cpp +++ b/runtime/JSString.cpp @@ -24,188 +24,251 @@ #include "JSString.h" #include "JSGlobalObject.h" +#include "JSGlobalObjectFunctions.h" #include "JSObject.h" #include "Operations.h" #include "StringObject.h" #include "StringPrototype.h" namespace JSC { + +static const unsigned substringFromRopeCutoff = 4; -void JSString::Rope::destructNonRecursive() -{ - Vector workQueue; - Rope* rope = this; - - while (true) { - unsigned length = rope->ropeLength(); - for (unsigned i = 0; i < length; ++i) { - Fiber& fiber = rope->fibers(i); - if (fiber.isString()) - fiber.string()->deref(); - else { - Rope* nextRope = fiber.rope(); - if (nextRope->hasOneRef()) - workQueue.append(nextRope); - else - nextRope->deref(); - } - } - if (rope != this) - fastFree(rope); +const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) }; - if (workQueue.isEmpty()) - return; +void JSRopeString::RopeBuilder::expand() +{ + ASSERT(m_index == JSRopeString::s_maxInternalRopeLength); + JSString* jsString = m_jsString; + m_jsString = jsStringBuilder(&m_globalData); + m_index = 0; + append(jsString); +} - rope = workQueue.last(); - workQueue.removeLast(); - } +void JSString::destroy(JSCell* cell) +{ + JSString* thisObject = jsCast(cell); + thisObject->JSString::~JSString(); +} + +void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor) +{ + JSString* thisObject = jsCast(cell); + Base::visitChildren(thisObject, visitor); + + if (thisObject->isRope()) + static_cast(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]); } -JSString::Rope::~Rope() +void JSRopeString::resolveRope(ExecState* exec) const { - destructNonRecursive(); + ASSERT(isRope()); + + if (is8Bit()) { + LChar* buffer; + if (RefPtr 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; + if (RefPtr 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 resolveRopeSlowCase(buffer); + } + + UChar* 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->characters(), length); + position += length; + m_fibers[i].clear(); + } + ASSERT((buffer + m_length) == position); + 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 // (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 UString::Reps 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 JSRopeString::resolveRopeSlowCase8(LChar* buffer) const { - ASSERT(isRope()); - - // Allocate the buffer to hold the final string, position initially points to the end. - UChar* buffer; - if (PassRefPtr newImpl = UStringImpl::tryCreateUninitialized(m_stringLength, buffer)) - m_value = newImpl; - else { - for (unsigned i = 0; i < m_ropeLength; ++i) { - m_fibers[i].deref(); - m_fibers[i] = static_cast(0); - } - m_ropeLength = 0; - ASSERT(!isRope()); - ASSERT(m_value == UString()); - throwOutOfMemoryError(exec); - return; + 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 && 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(); } - UChar* position = buffer + m_stringLength; - - // Start with the current Rope. - Vector workQueue; - Rope::Fiber currentFiber; - for (unsigned i = 0; i < (m_ropeLength - 1); ++i) - workQueue.append(m_fibers[i]); - currentFiber = m_fibers[m_ropeLength - 1]; - while (true) { - if (currentFiber.isRope()) { - Rope* rope = currentFiber.rope(); - // 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 ropeLengthMinusOne = rope->ropeLength() - 1; - for (unsigned i = 0; i < ropeLengthMinusOne; ++i) - workQueue.append(rope->fibers(i)); - currentFiber = rope->fibers(ropeLengthMinusOne); - } else { - UString::Rep* string = currentFiber.string(); - unsigned length = string->size(); - position -= length; - UStringImpl::copyChars(position, string->data(), 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_ropeLength; ++i) { - m_fibers[i].deref(); - m_fibers[i] = static_cast(0); - } - m_ropeLength = 0; - - ASSERT(!isRope()); - return; - } - - // No! - set the next item up to process. - currentFiber = workQueue.last(); - workQueue.removeLast(); + + while (!workQueue.isEmpty()) { + JSString* currentFiber = workQueue.last(); + workQueue.removeLast(); + + if (currentFiber->isRope()) { + JSRopeString* currentFiberAsRope = static_cast(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(currentFiber->m_value.impl()); + unsigned length = string->length(); + position -= length; + StringImpl::copyChars(position, string->characters8(), length); } + + ASSERT(buffer == position); + ASSERT(!isRope()); } -JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const +void JSRopeString::resolveRopeSlowCase(UChar* buffer) const { - return const_cast(this); + 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 && 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(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(currentFiber->m_value.impl()); + unsigned length = string->length(); + position -= length; + StringImpl::copyChars(position, string->characters(), length); + } + + ASSERT(buffer == position); + ASSERT(!isRope()); } -bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) +void JSRopeString::outOfMemory(ExecState* exec) const { - result = this; - number = value(exec).toDouble(); - return false; + 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); } -bool JSString::toBoolean(ExecState*) const +JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i) { - return m_stringLength; + 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); } -double JSString::toNumber(ExecState* exec) const +JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const { - return value(exec).toDouble(); + return const_cast(this); } -UString JSString::toString(ExecState* exec) const +bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const { - return value(exec); + result = this; + number = jsToNumber(value(exec)); + return false; } -UString JSString::toThisString(ExecState* exec) const +bool JSString::toBoolean(ExecState*) const { - return value(exec); + return m_length; } -JSString* JSString::toThisJSString(ExecState*) +double JSString::toNumber(ExecState* exec) const { - return this; + return jsToNumber(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); + StringObject* object = new (NotNull, allocateCell(*exec->heap())) StringObject(exec->globalData(), globalObject->stringObjectStructure()); + object->finishCreation(exec->globalData(), string); + return object; } -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 +JSObject* JSString::toThisObject(JSCell* cell, ExecState* exec) { - return StringObject::create(exec, const_cast(this)); + return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast(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(cell); // 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; - if (propertyName == exec->propertyNames().underscoreProto) { - slot.setValue(exec->lexicalGlobalObject()->stringPrototype()); - return true; - } - slot.setBase(this); + slot.setBase(thisObject); 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(); @@ -215,37 +278,28 @@ 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_stringLength), DontEnum | DontDelete | ReadOnly); + descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly); return true; } bool isStrictUInt32; - unsigned i = propertyName.toStrictUInt32(&isStrictUInt32); - if (isStrictUInt32 && i < m_stringLength) { - descriptor.setDescriptor(jsSingleCharacterSubstring(exec, value(exec), i), DontDelete | ReadOnly); + unsigned i = propertyName.toUInt32(isStrictUInt32); + if (isStrictUInt32 && i < m_length) { + descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly); return true; } 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(cell); // 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 JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot); + return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot); } } // namespace JSC