X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..14957cd040308e3eeec43d26bae5d76da13fcd85:/runtime/JSString.h?ds=inline diff --git a/runtime/JSString.h b/runtime/JSString.h index e4baa73..668064f 100644 --- a/runtime/JSString.h +++ b/runtime/JSString.h @@ -23,11 +23,13 @@ #ifndef JSString_h #define JSString_h -#include "CommonIdentifiers.h" #include "CallFrame.h" +#include "CommonIdentifiers.h" #include "Identifier.h" -#include "JSNumberCell.h" +#include "PropertyDescriptor.h" #include "PropertySlot.h" +#include "RopeImpl.h" +#include "Structure.h" namespace JSC { @@ -40,7 +42,6 @@ namespace JSC { JSString* jsSingleCharacterString(JSGlobalData*, UChar); JSString* jsSingleCharacterString(ExecState*, UChar); - JSString* jsSingleCharacterSubstring(JSGlobalData*, const UString&, unsigned offset); JSString* jsSingleCharacterSubstring(ExecState*, const UString&, unsigned offset); JSString* jsSubstring(JSGlobalData*, const UString&, unsigned offset, unsigned length); JSString* jsSubstring(ExecState*, const UString&, unsigned offset, unsigned length); @@ -58,71 +59,378 @@ namespace JSC { JSString* jsOwnedString(JSGlobalData*, const UString&); JSString* jsOwnedString(ExecState*, const UString&); - class JSString : public JSCell { - friend class JIT; - friend class Interpreter; - + class JS_EXPORTCLASS JSString : public JSCell { public: - JSString(JSGlobalData* globalData, const UString& value) - : JSCell(globalData->stringStructure.get()) + friend class JIT; + friend class JSGlobalData; + friend class SpecializedThunkJIT; + friend struct ThunkHelpers; + + class RopeBuilder { + public: + RopeBuilder(unsigned fiberCount) + : m_index(0) + , m_rope(RopeImpl::tryCreateUninitialized(fiberCount)) + { + } + + bool isOutOfMemory() { return !m_rope; } + + void append(RopeImpl::Fiber& fiber) + { + ASSERT(m_rope); + m_rope->initializeFiber(m_index, fiber); + } + void append(const UString& string) + { + ASSERT(m_rope); + m_rope->initializeFiber(m_index, string.impl()); + } + void append(JSString* jsString) + { + if (jsString->isRope()) { + for (unsigned i = 0; i < jsString->m_fiberCount; ++i) + append(jsString->m_fibers[i]); + } else + append(jsString->string()); + } + + PassRefPtr release() + { + ASSERT(m_index == m_rope->fiberCount()); + return m_rope.release(); + } + + unsigned length() { return m_rope->length(); } + + private: + unsigned m_index; + RefPtr m_rope; + }; + + class RopeIterator { + public: + RopeIterator() { } + + RopeIterator(RopeImpl::Fiber* fibers, size_t fiberCount) + { + ASSERT(fiberCount); + m_workQueue.append(WorkItem(fibers, fiberCount)); + skipRopes(); + } + + RopeIterator& operator++() + { + WorkItem& item = m_workQueue.last(); + ASSERT(!RopeImpl::isRope(item.fibers[item.i])); + if (++item.i == item.fiberCount) + m_workQueue.removeLast(); + skipRopes(); + return *this; + } + + StringImpl* operator*() + { + WorkItem& item = m_workQueue.last(); + RopeImpl::Fiber fiber = item.fibers[item.i]; + ASSERT(!RopeImpl::isRope(fiber)); + return static_cast(fiber); + } + + bool operator!=(const RopeIterator& other) const + { + return m_workQueue != other.m_workQueue; + } + + private: + struct WorkItem { + WorkItem(RopeImpl::Fiber* fibers, size_t fiberCount) + : fibers(fibers) + , fiberCount(fiberCount) + , i(0) + { + } + + bool operator!=(const WorkItem& other) const + { + return fibers != other.fibers || fiberCount != other.fiberCount || i != other.i; + } + + RopeImpl::Fiber* fibers; + size_t fiberCount; + size_t i; + }; + + void skipRopes() + { + if (m_workQueue.isEmpty()) + return; + + while (1) { + WorkItem& item = m_workQueue.last(); + RopeImpl::Fiber fiber = item.fibers[item.i]; + if (!RopeImpl::isRope(fiber)) + break; + RopeImpl* rope = static_cast(fiber); + if (++item.i == item.fiberCount) + m_workQueue.removeLast(); + m_workQueue.append(WorkItem(rope->fibers(), rope->fiberCount())); + } + } + + Vector m_workQueue; + }; + + ALWAYS_INLINE JSString(JSGlobalData* globalData, const UString& value) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(value.length()) , m_value(value) + , m_fiberCount(0) { - Heap::heap(this)->reportExtraMemoryCost(value.cost()); + ASSERT(!m_value.isNull()); + Heap::heap(this)->reportExtraMemoryCost(value.impl()->cost()); } enum HasOtherOwnerType { HasOtherOwner }; JSString(JSGlobalData* globalData, const UString& value, HasOtherOwnerType) - : JSCell(globalData->stringStructure.get()) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(value.length()) , m_value(value) + , m_fiberCount(0) { + ASSERT(!m_value.isNull()); } - JSString(JSGlobalData* globalData, PassRefPtr value, HasOtherOwnerType) - : JSCell(globalData->stringStructure.get()) + JSString(JSGlobalData* globalData, PassRefPtr value, HasOtherOwnerType) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(value->length()) , m_value(value) + , m_fiberCount(0) { + ASSERT(!m_value.isNull()); + } + JSString(JSGlobalData* globalData, PassRefPtr rope) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(rope->length()) + , m_fiberCount(1) + { + m_fibers[0] = rope.leakRef(); + } + // This constructor constructs a new string by concatenating s1 & s2. + // This should only be called with fiberCount <= 3. + JSString(JSGlobalData* globalData, unsigned fiberCount, JSString* s1, JSString* s2) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(s1->length() + s2->length()) + , m_fiberCount(fiberCount) + { + ASSERT(fiberCount <= s_maxInternalRopeLength); + unsigned index = 0; + appendStringInConstruct(index, s1); + appendStringInConstruct(index, s2); + ASSERT(fiberCount == index); + } + // This constructor constructs a new string by concatenating s1 & s2. + // This should only be called with fiberCount <= 3. + JSString(JSGlobalData* globalData, unsigned fiberCount, JSString* s1, const UString& u2) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(s1->length() + u2.length()) + , m_fiberCount(fiberCount) + { + ASSERT(fiberCount <= s_maxInternalRopeLength); + unsigned index = 0; + appendStringInConstruct(index, s1); + appendStringInConstruct(index, u2); + ASSERT(fiberCount == index); + } + // This constructor constructs a new string by concatenating s1 & s2. + // This should only be called with fiberCount <= 3. + JSString(JSGlobalData* globalData, unsigned fiberCount, const UString& u1, JSString* s2) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(u1.length() + s2->length()) + , m_fiberCount(fiberCount) + { + ASSERT(fiberCount <= s_maxInternalRopeLength); + unsigned index = 0; + appendStringInConstruct(index, u1); + appendStringInConstruct(index, s2); + ASSERT(fiberCount == index); + } + // This constructor constructs a new string by concatenating v1, v2 & v3. + // This should only be called with fiberCount <= 3 ... which since every + // value must require a fiberCount of at least one implies that the length + // for each value must be exactly 1! + JSString(ExecState* exec, JSValue v1, JSValue v2, JSValue v3) + : JSCell(exec->globalData(), exec->globalData().stringStructure.get()) + , m_length(0) + , m_fiberCount(s_maxInternalRopeLength) + { + unsigned index = 0; + appendValueInConstructAndIncrementLength(exec, index, v1); + appendValueInConstructAndIncrementLength(exec, index, v2); + appendValueInConstructAndIncrementLength(exec, index, v3); + ASSERT(index == s_maxInternalRopeLength); } - - const UString& value() const { return m_value; } + + // This constructor constructs a new string by concatenating u1 & u2. + JSString(JSGlobalData* globalData, const UString& u1, const UString& u2) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(u1.length() + u2.length()) + , m_fiberCount(2) + { + unsigned index = 0; + appendStringInConstruct(index, u1); + appendStringInConstruct(index, u2); + ASSERT(index <= s_maxInternalRopeLength); + } + + // This constructor constructs a new string by concatenating u1, u2 & u3. + JSString(JSGlobalData* globalData, const UString& u1, const UString& u2, const UString& u3) + : JSCell(*globalData, globalData->stringStructure.get()) + , m_length(u1.length() + u2.length() + u3.length()) + , m_fiberCount(s_maxInternalRopeLength) + { + unsigned index = 0; + appendStringInConstruct(index, u1); + appendStringInConstruct(index, u2); + appendStringInConstruct(index, u3); + ASSERT(index <= s_maxInternalRopeLength); + } + + ~JSString() + { + ASSERT(vptr() == JSGlobalData::jsStringVPtr); + for (unsigned i = 0; i < m_fiberCount; ++i) + RopeImpl::deref(m_fibers[i]); + } + + const UString& value(ExecState* exec) const + { + if (isRope()) + resolveRope(exec); + return m_value; + } + const UString& tryGetValue() const + { + if (isRope()) + resolveRope(0); + return m_value; + } + unsigned length() { return m_length; } bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); + bool getStringPropertyDescriptor(ExecState*, const Identifier& propertyName, PropertyDescriptor&); - bool canGetIndex(unsigned i) { return i < static_cast(m_value.size()); } - JSString* getIndex(JSGlobalData*, unsigned); + bool canGetIndex(unsigned i) { return i < m_length; } + JSString* getIndex(ExecState*, unsigned); + JSString* getIndexSlowCase(ExecState*, unsigned); - static PassRefPtr createStructure(JSValuePtr proto) { return Structure::create(proto, TypeInfo(StringType, NeedsThisConversion)); } + JSValue replaceCharacter(ExecState*, UChar, const UString& replacement); + + static Structure* createStructure(JSGlobalData& globalData, JSValue proto) + { + return Structure::create(globalData, proto, TypeInfo(StringType, OverridesGetOwnPropertySlot | NeedsThisConversion), AnonymousSlotCount, &s_info); + } + + static const ClassInfo s_info; private: - enum VPtrStealingHackType { VPtrStealingHack }; JSString(VPtrStealingHackType) - : JSCell(0) + : JSCell(VPtrStealingHack) + , m_fiberCount(0) + { + } + + void resolveRope(ExecState*) const; + void resolveRopeSlowCase(ExecState*, UChar*) const; + void outOfMemory(ExecState*) const; + JSString* substringFromRope(ExecState*, unsigned offset, unsigned length); + + void appendStringInConstruct(unsigned& index, const UString& string) + { + StringImpl* impl = string.impl(); + impl->ref(); + m_fibers[index++] = impl; + } + + void appendStringInConstruct(unsigned& index, JSString* jsString) { + if (jsString->isRope()) { + for (unsigned i = 0; i < jsString->m_fiberCount; ++i) { + RopeImpl::Fiber fiber = jsString->m_fibers[i]; + fiber->ref(); + m_fibers[index++] = fiber; + } + } else + appendStringInConstruct(index, jsString->string()); } - virtual JSValuePtr toPrimitive(ExecState*, PreferredPrimitiveType) const; - virtual bool getPrimitiveNumber(ExecState*, double& number, JSValuePtr& value); + void appendValueInConstructAndIncrementLength(ExecState* exec, unsigned& index, JSValue v) + { + if (v.isString()) { + ASSERT(v.asCell()->isString()); + JSString* s = static_cast(v.asCell()); + ASSERT(s->fiberCount() == 1); + appendStringInConstruct(index, s); + m_length += s->length(); + } else { + UString u(v.toString(exec)); + StringImpl* impl = u.impl(); + impl->ref(); + m_fibers[index++] = impl; + m_length += u.length(); + } + } + + virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const; + virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue& value); virtual bool toBoolean(ExecState*) const; virtual double toNumber(ExecState*) const; - virtual JSObject* toObject(ExecState*) const; + virtual JSObject* toObject(ExecState*, JSGlobalObject*) const; virtual UString toString(ExecState*) const; virtual JSObject* toThisObject(ExecState*) const; - virtual UString toThisString(ExecState*) const; - virtual JSString* toThisJSString(ExecState*); // Actually getPropertySlot, not getOwnPropertySlot (see JSCell). virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); - - UString m_value; + virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&); + + static const unsigned s_maxInternalRopeLength = 3; + + // A string is represented either by a UString or a RopeImpl. + unsigned m_length; + mutable UString m_value; + mutable unsigned m_fiberCount; + mutable FixedArray m_fibers; + + bool isRope() const { return m_fiberCount; } + UString& string() { ASSERT(!isRope()); return m_value; } + unsigned fiberCount() { return m_fiberCount ? m_fiberCount : 1; } + + friend JSValue jsString(ExecState* exec, JSString* s1, JSString* s2); + friend JSValue jsString(ExecState* exec, const UString& u1, JSString* s2); + friend JSValue jsString(ExecState* exec, JSString* s1, const UString& u2); + friend JSValue jsString(ExecState* exec, Register* strings, unsigned count); + friend JSValue jsString(ExecState* exec, JSValue thisValue); + friend JSString* jsSubstring(ExecState* exec, JSString* s, unsigned offset, unsigned length); }; - JSString* asString(JSValuePtr); + JSString* asString(JSValue); - inline JSString* asString(JSValuePtr value) + // When an object is created from a different DLL, MSVC changes vptr to a "local" one right after invoking a constructor, + // see . + // This breaks isJSString(), and we don't need that hack anyway, so we change vptr back to primary one. + // The below function must be called by any inline function that invokes a JSString constructor. +#if COMPILER(MSVC) && !defined(BUILDING_JavaScriptCore) + inline JSString* fixupVPtr(JSGlobalData* globalData, JSString* string) { string->setVPtr(globalData->jsStringVPtr); return string; } +#else + inline JSString* fixupVPtr(JSGlobalData*, JSString* string) { return string; } +#endif + + inline JSString* asString(JSValue value) { - ASSERT(asCell(value)->isString()); - return static_cast(asCell(value)); + ASSERT(value.asCell()->isString()); + return static_cast(value.asCell()); } inline JSString* jsEmptyString(JSGlobalData* globalData) @@ -132,18 +440,19 @@ namespace JSC { inline JSString* jsSingleCharacterString(JSGlobalData* globalData, UChar c) { - if (c <= 0xFF) + if (c <= maxSingleCharacterString) return globalData->smallStrings.singleCharacterString(globalData, c); - return new (globalData) JSString(globalData, UString(&c, 1)); + return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(&c, 1))); } - inline JSString* jsSingleCharacterSubstring(JSGlobalData* globalData, const UString& s, unsigned offset) + inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset) { - ASSERT(offset < static_cast(s.size())); - UChar c = s.data()[offset]; - if (c <= 0xFF) + JSGlobalData* globalData = &exec->globalData(); + ASSERT(offset < static_cast(s.length())); + UChar c = s.characters()[offset]; + if (c <= maxSingleCharacterString) return globalData->smallStrings.singleCharacterString(globalData, c); - return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, 1)); + return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(StringImpl::create(s.impl(), offset, 1)))); } inline JSString* jsNontrivialString(JSGlobalData* globalData, const char* s) @@ -151,25 +460,81 @@ namespace JSC { ASSERT(s); ASSERT(s[0]); ASSERT(s[1]); - return new (globalData) JSString(globalData, s); + return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); } inline JSString* jsNontrivialString(JSGlobalData* globalData, const UString& s) { - ASSERT(s.size() > 1); - return new (globalData) JSString(globalData, s); + ASSERT(s.length() > 1); + return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); } - inline JSString* JSString::getIndex(JSGlobalData* globalData, unsigned i) + inline JSString* JSString::getIndex(ExecState* exec, unsigned i) { ASSERT(canGetIndex(i)); - return jsSingleCharacterSubstring(globalData, m_value, i); + if (isRope()) + return getIndexSlowCase(exec, i); + ASSERT(i < m_value.length()); + return jsSingleCharacterSubstring(exec, m_value, i); + } + + inline JSString* jsString(JSGlobalData* globalData, const UString& s) + { + int size = s.length(); + if (!size) + return globalData->smallStrings.emptyString(globalData); + if (size == 1) { + UChar c = s.characters()[0]; + if (c <= maxSingleCharacterString) + return globalData->smallStrings.singleCharacterString(globalData, c); + } + return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); + } + + inline JSString* jsSubstring(ExecState* exec, JSString* s, unsigned offset, unsigned length) + { + ASSERT(offset <= static_cast(s->length())); + ASSERT(length <= static_cast(s->length())); + ASSERT(offset + length <= static_cast(s->length())); + JSGlobalData* globalData = &exec->globalData(); + if (!length) + return globalData->smallStrings.emptyString(globalData); + if (s->isRope()) + return s->substringFromRope(exec, offset, length); + return jsSubstring(globalData, s->m_value, offset, length); + } + + inline JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length) + { + ASSERT(offset <= static_cast(s.length())); + ASSERT(length <= static_cast(s.length())); + ASSERT(offset + length <= static_cast(s.length())); + if (!length) + return globalData->smallStrings.emptyString(globalData); + if (length == 1) { + UChar c = s.characters()[offset]; + if (c <= maxSingleCharacterString) + return globalData->smallStrings.singleCharacterString(globalData, c); + } + return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(StringImpl::create(s.impl(), offset, length)), JSString::HasOtherOwner)); + } + + inline JSString* jsOwnedString(JSGlobalData* globalData, const UString& s) + { + int size = s.length(); + if (!size) + return globalData->smallStrings.emptyString(globalData); + if (size == 1) { + UChar c = s.characters()[0]; + if (c <= maxSingleCharacterString) + return globalData->smallStrings.singleCharacterString(globalData, c); + } + return fixupVPtr(globalData, new (globalData) JSString(globalData, s, JSString::HasOtherOwner)); } inline JSString* jsEmptyString(ExecState* exec) { return jsEmptyString(&exec->globalData()); } inline JSString* jsString(ExecState* exec, const UString& s) { return jsString(&exec->globalData(), s); } inline JSString* jsSingleCharacterString(ExecState* exec, UChar c) { return jsSingleCharacterString(&exec->globalData(), c); } - inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset) { return jsSingleCharacterSubstring(&exec->globalData(), s, offset); } inline JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length) { return jsSubstring(&exec->globalData(), s, offset, length); } inline JSString* jsNontrivialString(ExecState* exec, const UString& s) { return jsNontrivialString(&exec->globalData(), s); } inline JSString* jsNontrivialString(ExecState* exec, const char* s) { return jsNontrivialString(&exec->globalData(), s); } @@ -178,14 +543,14 @@ namespace JSC { ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) { if (propertyName == exec->propertyNames().length) { - slot.setValue(jsNumber(exec, m_value.size())); + slot.setValue(jsNumber(m_length)); return true; } bool isStrictUInt32; - unsigned i = propertyName.toStrictUInt32(&isStrictUInt32); - if (isStrictUInt32 && i < static_cast(m_value.size())) { - slot.setValue(jsSingleCharacterSubstring(exec, m_value, i)); + unsigned i = propertyName.toUInt32(isStrictUInt32); + if (isStrictUInt32 && i < m_length) { + slot.setValue(getIndex(exec, i)); return true; } @@ -194,19 +559,55 @@ namespace JSC { ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) { - if (propertyName < static_cast(m_value.size())) { - slot.setValue(jsSingleCharacterSubstring(exec, m_value, propertyName)); + if (propertyName < m_length) { + slot.setValue(getIndex(exec, propertyName)); return true; } return false; } + inline bool isJSString(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsStringVPtr; } + // --- JSValue inlines ---------------------------- - inline JSString* JSValuePtr::toThisJSString(ExecState* exec) + inline UString JSValue::toString(ExecState* exec) const + { + if (isString()) + return static_cast(asCell())->value(exec); + if (isInt32()) + return exec->globalData().numericStrings.add(asInt32()); + if (isDouble()) + return exec->globalData().numericStrings.add(asDouble()); + if (isTrue()) + return "true"; + if (isFalse()) + return "false"; + if (isNull()) + return "null"; + if (isUndefined()) + return "undefined"; + ASSERT(isCell()); + return asCell()->toString(exec); + } + + inline UString JSValue::toPrimitiveString(ExecState* exec) const { - return JSImmediate::isImmediate(asValue()) ? jsString(exec, JSImmediate::toString(asValue())) : asCell()->toThisJSString(exec); + ASSERT(!isString()); + if (isInt32()) + return exec->globalData().numericStrings.add(asInt32()); + if (isDouble()) + return exec->globalData().numericStrings.add(asDouble()); + if (isTrue()) + return "true"; + if (isFalse()) + return "false"; + if (isNull()) + return "null"; + if (isUndefined()) + return "undefined"; + ASSERT(isCell()); + return asCell()->toPrimitive(exec, NoPreference).toString(exec); } } // namespace JSC