X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/4be4e30906bcb8ee30b4d189205cb70bad6707ce..81345200c95645a1b0d2635520f96ad55dfde63f:/runtime/JSString.cpp diff --git a/runtime/JSString.cpp b/runtime/JSString.cpp index 6f0b09d..10a16d9 100644 --- a/runtime/JSString.cpp +++ b/runtime/JSString.cpp @@ -26,14 +26,13 @@ #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, 0, CREATE_METHOD_TABLE(JSString) }; void JSRopeString::RopeBuilder::expand() @@ -52,26 +51,34 @@ void JSString::destroy(JSCell* cell) thisObject->JSString::~JSString(); } -void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor) +void JSString::dumpToStream(const JSCell* cell, PrintStream& out) { - JSString* thisObject = jsCast(cell); - Base::visitChildren(thisObject, visitor); - - MARK_LOG_MESSAGE1("[%u]: ", thisObject->length()); - -#if ENABLE(OBJECT_MARK_LOGGING) - if (!thisObject->isRope()) { + const JSString* thisObject = jsCast(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()) - MARK_LOG_MESSAGE1("[8 %p]", ourImpl->characters8()); + out.printf("[8 %p]", ourImpl->characters8()); else - MARK_LOG_MESSAGE1("[16 %p]", ourImpl->characters16()); - } else - MARK_LOG_MESSAGE0("[rope]: "); -#endif + out.printf("[16 %p]", ourImpl->characters16()); + } + out.printf(">"); +} +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.reportExtraMemoryUsage(thisObject, impl->costDuringGC()); + } } void JSRopeString::visitFibers(SlotVisitor& visitor) @@ -80,6 +87,113 @@ void JSRopeString::visitFibers(SlotVisitor& visitor) visitor.append(&m_fibers[i]); } +static const unsigned maxLengthForOnStackResolve = 2048; + +void JSRopeString::resolveRopeInternal8(LChar* buffer) const +{ + 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); +} + +void JSRopeString::resolveRopeInternal16(UChar* buffer) const +{ + 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); +} + +void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const +{ + 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()); +} + +void JSRopeString::clearFibers() const +{ + for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) + m_fibers[i].clear(); +} + +AtomicStringImpl* JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const +{ + 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; +} + void JSRopeString::resolveRope(ExecState* exec) const { ASSERT(isRope()); @@ -93,23 +207,9 @@ void JSRopeString::resolveRope(ExecState* exec) const 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); + resolveRopeInternal8(buffer); + clearFibers(); ASSERT(!isRope()); - return; } @@ -122,23 +222,8 @@ void JSRopeString::resolveRope(ExecState* exec) const 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(); - if (string->is8Bit()) - StringImpl::copyChars(position, string->characters8(), length); - else - StringImpl::copyChars(position, string->characters16(), length); - position += length; - m_fibers[i].clear(); - } - ASSERT((buffer + m_length) == position); + resolveRopeInternal16(buffer); + clearFibers(); ASSERT(!isRope()); } @@ -157,11 +242,8 @@ 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 && m_fibers[i]; ++i) { + 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(); - } while (!workQueue.isEmpty()) { JSString* currentFiber = workQueue.last(); @@ -181,7 +263,6 @@ void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const } ASSERT(buffer == position); - ASSERT(!isRope()); } void JSRopeString::resolveRopeSlowCase(UChar* buffer) const @@ -213,13 +294,11 @@ void JSRopeString::resolveRopeSlowCase(UChar* buffer) const } ASSERT(buffer == position); - ASSERT(!isRope()); } void JSRopeString::outOfMemory(ExecState* exec) const { - for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) - m_fibers[i].clear(); + clearFibers(); ASSERT(isRope()); ASSERT(m_value.isNull()); if (exec) @@ -260,39 +339,23 @@ double JSString::toNumber(ExecState* exec) const return jsToNumber(value(exec)); } -inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string) +inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string) { - StringObject* object = new (NotNull, allocateCell(*exec->heap())) StringObject(exec->vm(), globalObject->stringObjectStructure()); - object->finishCreation(exec->vm(), 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(JSCell* cell, ExecState* exec) +JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode) { - return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast(cell)); -} - -bool JSString::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName 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 (thisObject->getStringPropertySlot(exec, propertyName, slot)) - return true; - slot.setBase(thisObject); - JSObject* object; - for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) { - object = asObject(prototype); - if (object->methodTable()->getOwnPropertySlot(object, exec, propertyName, slot)) - return true; - } - slot.setUndefined(); - return true; + if (ecmaMode == StrictMode) + return cell; + return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast(cell)); } bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor) @@ -312,14 +375,13 @@ bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propert return false; } -bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot) +JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl) { - JSString* thisObject = jsCast(cell); - // The semantics here are really getPropertySlot, not getOwnPropertySlot. - // This function should only be called by JSValue::get. - if (thisObject->getStringPropertySlot(exec, propertyName, slot)) - return true; - return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot); + 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