X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/14957cd040308e3eeec43d26bae5d76da13fcd85..cb9aa2694aba0ae4f946ed34b8e0f6c99c1cfe44:/runtime/JSString.h diff --git a/runtime/JSString.h b/runtime/JSString.h index 668064f..1287b66 100644 --- a/runtime/JSString.h +++ b/runtime/JSString.h @@ -1,7 +1,7 @@ /* * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) * Copyright (C) 2001 Peter Kelly (pmk@post.com) - * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2014 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -28,467 +28,425 @@ #include "Identifier.h" #include "PropertyDescriptor.h" #include "PropertySlot.h" -#include "RopeImpl.h" #include "Structure.h" +#include namespace JSC { class JSString; + class JSRopeString; + class LLIntOffsetsExtractor; - JSString* jsEmptyString(JSGlobalData*); + JSString* jsEmptyString(VM*); JSString* jsEmptyString(ExecState*); - JSString* jsString(JSGlobalData*, const UString&); // returns empty string if passed null string - JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string + JSString* jsString(VM*, const String&); // returns empty string if passed null string + JSString* jsString(ExecState*, const String&); // returns empty string if passed null string - JSString* jsSingleCharacterString(JSGlobalData*, UChar); + JSString* jsSingleCharacterString(VM*, UChar); JSString* jsSingleCharacterString(ExecState*, UChar); - 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); + JSString* jsSingleCharacterSubstring(ExecState*, const String&, unsigned offset); + JSString* jsSubstring(VM*, const String&, unsigned offset, unsigned length); + JSString* jsSubstring(ExecState*, const String&, unsigned offset, unsigned length); // Non-trivial strings are two or more characters long. // These functions are faster than just calling jsString. - JSString* jsNontrivialString(JSGlobalData*, const UString&); - JSString* jsNontrivialString(ExecState*, const UString&); - JSString* jsNontrivialString(JSGlobalData*, const char*); - JSString* jsNontrivialString(ExecState*, const char*); + JSString* jsNontrivialString(VM*, const String&); + JSString* jsNontrivialString(ExecState*, const String&); // Should be used for strings that are owned by an object that will // likely outlive the JSValue this makes, such as the parse tree or a - // DOM object that contains a UString - JSString* jsOwnedString(JSGlobalData*, const UString&); - JSString* jsOwnedString(ExecState*, const UString&); + // DOM object that contains a String + JSString* jsOwnedString(VM*, const String&); + JSString* jsOwnedString(ExecState*, const String&); - class JS_EXPORTCLASS JSString : public JSCell { + JSRopeString* jsStringBuilder(VM*); + + class JSString : public JSCell { public: friend class JIT; - friend class JSGlobalData; + friend class VM; friend class SpecializedThunkJIT; + friend class JSRopeString; + friend class MarkStack; + friend class SlotVisitor; 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() { } + typedef JSCell Base; - 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; - }; + static const bool needsDestruction = true; + static const bool hasImmortalStructure = true; + static void destroy(JSCell*); - ALWAYS_INLINE JSString(JSGlobalData* globalData, const UString& value) - : JSCell(*globalData, globalData->stringStructure.get()) - , m_length(value.length()) + private: + JSString(VM& vm, PassRefPtr value) + : JSCell(vm, vm.stringStructure.get()) + , m_flags(0) , m_value(value) - , m_fiberCount(0) { - ASSERT(!m_value.isNull()); - Heap::heap(this)->reportExtraMemoryCost(value.impl()->cost()); } - enum HasOtherOwnerType { HasOtherOwner }; - JSString(JSGlobalData* globalData, const UString& value, HasOtherOwnerType) - : JSCell(*globalData, globalData->stringStructure.get()) - , m_length(value.length()) - , m_value(value) - , m_fiberCount(0) + JSString(VM& vm) + : JSCell(vm, vm.stringStructure.get()) + , m_flags(0) { - ASSERT(!m_value.isNull()); } - JSString(JSGlobalData* globalData, PassRefPtr value, HasOtherOwnerType) - : JSCell(*globalData, globalData->stringStructure.get()) - , m_length(value->length()) - , m_value(value) - , m_fiberCount(0) + + void finishCreation(VM& vm, size_t length) { ASSERT(!m_value.isNull()); + Base::finishCreation(vm); + m_length = length; + setIs8Bit(m_value.impl()->is8Bit()); + vm.m_newStringsSinceLastHashCons++; } - 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) + + void finishCreation(VM& vm, size_t length, size_t cost) { - ASSERT(fiberCount <= s_maxInternalRopeLength); - unsigned index = 0; - appendStringInConstruct(index, s1); - appendStringInConstruct(index, s2); - ASSERT(fiberCount == index); + ASSERT(!m_value.isNull()); + Base::finishCreation(vm); + m_length = length; + setIs8Bit(m_value.impl()->is8Bit()); + Heap::heap(this)->reportExtraMemoryCost(cost); + vm.m_newStringsSinceLastHashCons++; } - // 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) + + protected: + void finishCreation(VM& vm) { - ASSERT(fiberCount <= s_maxInternalRopeLength); - unsigned index = 0; - appendStringInConstruct(index, s1); - appendStringInConstruct(index, u2); - ASSERT(fiberCount == index); + Base::finishCreation(vm); + m_length = 0; + setIs8Bit(true); + vm.m_newStringsSinceLastHashCons++; } - // 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) + + public: + static JSString* create(VM& vm, PassRefPtr value) { - ASSERT(fiberCount <= s_maxInternalRopeLength); - unsigned index = 0; - appendStringInConstruct(index, u1); - appendStringInConstruct(index, s2); - ASSERT(fiberCount == index); + ASSERT(value); + int32_t length = value->length(); + RELEASE_ASSERT(length >= 0); + size_t cost = value->cost(); + JSString* newString = new (NotNull, allocateCell(vm.heap)) JSString(vm, value); + newString->finishCreation(vm, length, cost); + return newString; } - // 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) + static JSString* createHasOtherOwner(VM& vm, PassRefPtr value) { - unsigned index = 0; - appendValueInConstructAndIncrementLength(exec, index, v1); - appendValueInConstructAndIncrementLength(exec, index, v2); - appendValueInConstructAndIncrementLength(exec, index, v3); - ASSERT(index == s_maxInternalRopeLength); + ASSERT(value); + size_t length = value->length(); + JSString* newString = new (NotNull, allocateCell(vm.heap)) JSString(vm, value); + newString->finishCreation(vm, length); + return newString; } - // 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) + Identifier toIdentifier(ExecState*) const; + AtomicString toAtomicString(ExecState*) const; + AtomicStringImpl* toExistingAtomicString(ExecState*) const; + const String& value(ExecState*) const; + const String& tryGetValue() const; + const StringImpl* tryGetValueImpl() const; + unsigned length() const { return m_length; } + + JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const; + JS_EXPORT_PRIVATE bool toBoolean() const; + bool getPrimitiveNumber(ExecState*, double& number, JSValue&) const; + JSObject* toObject(ExecState*, JSGlobalObject*) const; + double toNumber(ExecState*) const; + + bool getStringPropertySlot(ExecState*, PropertyName, PropertySlot&); + bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); + bool getStringPropertyDescriptor(ExecState*, PropertyName, PropertyDescriptor&); + + bool canGetIndex(unsigned i) { return i < m_length; } + JSString* getIndex(ExecState*, unsigned); + + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { - unsigned index = 0; - appendStringInConstruct(index, u1); - appendStringInConstruct(index, u2); - ASSERT(index <= s_maxInternalRopeLength); + return Structure::create(vm, globalObject, proto, TypeInfo(StringType, StructureFlags), info()); } - // 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) + static size_t offsetOfLength() { return OBJECT_OFFSETOF(JSString, m_length); } + static size_t offsetOfFlags() { return OBJECT_OFFSETOF(JSString, m_flags); } + static size_t offsetOfValue() { return OBJECT_OFFSETOF(JSString, m_value); } + + DECLARE_EXPORT_INFO; + + static void dumpToStream(const JSCell*, PrintStream&); + static void visitChildren(JSCell*, SlotVisitor&); + + enum { + HashConsLock = 1u << 2, + IsHashConsSingleton = 1u << 1, + Is8Bit = 1u + }; + + protected: + static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | StructureIsImmortal; + + friend class JSValue; + + bool isRope() const { return m_value.isNull(); } + bool is8Bit() const { return m_flags & Is8Bit; } + void setIs8Bit(bool flag) { - unsigned index = 0; - appendStringInConstruct(index, u1); - appendStringInConstruct(index, u2); - appendStringInConstruct(index, u3); - ASSERT(index <= s_maxInternalRopeLength); + if (flag) + m_flags |= Is8Bit; + else + m_flags &= ~Is8Bit; } + bool shouldTryHashCons(); + bool isHashConsSingleton() const { return m_flags & IsHashConsSingleton; } + void clearHashConsSingleton() { m_flags &= ~IsHashConsSingleton; } + void setHashConsSingleton() { m_flags |= IsHashConsSingleton; } + bool tryHashConsLock(); + void releaseHashConsLock(); + + unsigned m_flags; + + // A string is represented either by a String or a rope of fibers. + unsigned m_length; + mutable String m_value; + + private: + friend class LLIntOffsetsExtractor; + + static JSValue toThis(JSCell*, ExecState*, ECMAMode); + + String& string() { ASSERT(!isRope()); return m_value; } + + friend JSValue jsString(ExecState*, JSString*, JSString*); + friend JSString* jsSubstring(ExecState*, JSString*, unsigned offset, unsigned length); + }; + + class JSRopeString : public JSString { + friend class JSString; + + friend JSRopeString* jsStringBuilder(VM*); - ~JSString() + class RopeBuilder { + public: + RopeBuilder(VM& vm) + : m_vm(vm) + , m_jsString(jsStringBuilder(&vm)) + , m_index(0) + { + } + + bool append(JSString* jsString) + { + if (m_index == JSRopeString::s_maxInternalRopeLength) + expand(); + if (static_cast(m_jsString->length() + jsString->length()) < 0) { + m_jsString = nullptr; + return false; + } + m_jsString->append(m_vm, m_index++, jsString); + return true; + } + + JSRopeString* release() + { + RELEASE_ASSERT(m_jsString); + JSRopeString* tmp = m_jsString; + m_jsString = 0; + return tmp; + } + + unsigned length() const { return m_jsString->m_length; } + + private: + void expand(); + + VM& m_vm; + JSRopeString* m_jsString; + size_t m_index; + }; + + private: + JSRopeString(VM& vm) + : JSString(vm) { - ASSERT(vptr() == JSGlobalData::jsStringVPtr); - for (unsigned i = 0; i < m_fiberCount; ++i) - RopeImpl::deref(m_fibers[i]); } - const UString& value(ExecState* exec) const + void finishCreation(VM& vm, JSString* s1, JSString* s2) { - if (isRope()) - resolveRope(exec); - return m_value; + Base::finishCreation(vm); + m_length = s1->length() + s2->length(); + setIs8Bit(s1->is8Bit() && s2->is8Bit()); + m_fibers[0].set(vm, this, s1); + m_fibers[1].set(vm, this, s2); } - const UString& tryGetValue() const + + void finishCreation(VM& vm, JSString* s1, JSString* s2, JSString* s3) { - if (isRope()) - resolveRope(0); - return m_value; + Base::finishCreation(vm); + m_length = s1->length() + s2->length() + s3->length(); + setIs8Bit(s1->is8Bit() && s2->is8Bit() && s3->is8Bit()); + m_fibers[0].set(vm, this, s1); + m_fibers[1].set(vm, this, s2); + m_fibers[2].set(vm, this, s3); } - 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 < m_length; } - JSString* getIndex(ExecState*, unsigned); - JSString* getIndexSlowCase(ExecState*, unsigned); - - JSValue replaceCharacter(ExecState*, UChar, const UString& replacement); - static Structure* createStructure(JSGlobalData& globalData, JSValue proto) + void finishCreation(VM& vm) { - return Structure::create(globalData, proto, TypeInfo(StringType, OverridesGetOwnPropertySlot | NeedsThisConversion), AnonymousSlotCount, &s_info); + JSString::finishCreation(vm); } - - static const ClassInfo s_info; - private: - JSString(VPtrStealingHackType) - : JSCell(VPtrStealingHack) - , m_fiberCount(0) + void append(VM& vm, size_t index, JSString* jsString) { + m_fibers[index].set(vm, this, jsString); + m_length += jsString->m_length; + RELEASE_ASSERT(static_cast(m_length) >= 0); + setIs8Bit(is8Bit() && jsString->is8Bit()); } - 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) + static JSRopeString* createNull(VM& vm) { - StringImpl* impl = string.impl(); - impl->ref(); - m_fibers[index++] = impl; + JSRopeString* newString = new (NotNull, allocateCell(vm.heap)) JSRopeString(vm); + newString->finishCreation(vm); + return newString; } - void appendStringInConstruct(unsigned& index, JSString* jsString) + public: + static JSString* create(VM& vm, JSString* s1, JSString* s2) { - 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()); + JSRopeString* newString = new (NotNull, allocateCell(vm.heap)) JSRopeString(vm); + newString->finishCreation(vm, s1, s2); + return newString; } - - void appendValueInConstructAndIncrementLength(ExecState* exec, unsigned& index, JSValue v) + static JSString* create(VM& vm, JSString* s1, JSString* s2, JSString* s3) { - 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(); - } + JSRopeString* newString = new (NotNull, allocateCell(vm.heap)) JSRopeString(vm); + newString->finishCreation(vm, s1, s2, s3); + return newString; } - 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*, JSGlobalObject*) const; - virtual UString toString(ExecState*) const; - - virtual JSObject* toThisObject(ExecState*) const; - - // Actually getPropertySlot, not getOwnPropertySlot (see JSCell). - virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); - virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); - virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&); + void visitFibers(SlotVisitor&); + + static ptrdiff_t offsetOfFibers() { return OBJECT_OFFSETOF(JSRopeString, m_fibers); } static const unsigned s_maxInternalRopeLength = 3; + + private: + friend JSValue jsStringFromRegisterArray(ExecState*, Register*, unsigned); + friend JSValue jsStringFromArguments(ExecState*, JSValue); + + JS_EXPORT_PRIVATE void resolveRope(ExecState*) const; + JS_EXPORT_PRIVATE void resolveRopeToAtomicString(ExecState*) const; + JS_EXPORT_PRIVATE AtomicStringImpl* resolveRopeToExistingAtomicString(ExecState*) const; + void resolveRopeSlowCase8(LChar*) const; + void resolveRopeSlowCase(UChar*) const; + void outOfMemory(ExecState*) const; + void resolveRopeInternal8(LChar*) const; + void resolveRopeInternal16(UChar*) const; + void clearFibers() const; + + JS_EXPORT_PRIVATE JSString* getIndexSlowCase(ExecState*, unsigned); - // 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); + mutable std::array, s_maxInternalRopeLength> m_fibers; }; - JSString* asString(JSValue); - // 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 const StringImpl* JSString::tryGetValueImpl() const + { + return m_value.impl(); + } + + JSString* asString(JSValue); inline JSString* asString(JSValue value) { ASSERT(value.asCell()->isString()); - return static_cast(value.asCell()); + return jsCast(value.asCell()); } - inline JSString* jsEmptyString(JSGlobalData* globalData) + inline JSString* jsEmptyString(VM* vm) { - return globalData->smallStrings.emptyString(globalData); + return vm->smallStrings.emptyString(); } - inline JSString* jsSingleCharacterString(JSGlobalData* globalData, UChar c) + ALWAYS_INLINE JSString* jsSingleCharacterString(VM* vm, UChar c) { if (c <= maxSingleCharacterString) - return globalData->smallStrings.singleCharacterString(globalData, c); - return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(&c, 1))); + return vm->smallStrings.singleCharacterString(c); + return JSString::create(*vm, String(&c, 1).impl()); } - inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset) + ALWAYS_INLINE JSString* jsSingleCharacterSubstring(ExecState* exec, const String& s, unsigned offset) { - JSGlobalData* globalData = &exec->globalData(); + VM* vm = &exec->vm(); ASSERT(offset < static_cast(s.length())); - UChar c = s.characters()[offset]; + UChar c = s.characterAt(offset); if (c <= maxSingleCharacterString) - return globalData->smallStrings.singleCharacterString(globalData, c); - return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(StringImpl::create(s.impl(), offset, 1)))); + return vm->smallStrings.singleCharacterString(c); + return JSString::create(*vm, StringImpl::createSubstringSharingImpl(s.impl(), offset, 1)); + } + + inline JSString* jsNontrivialString(VM* vm, const String& s) + { + ASSERT(s.length() > 1); + return JSString::create(*vm, s.impl()); } - inline JSString* jsNontrivialString(JSGlobalData* globalData, const char* s) + ALWAYS_INLINE Identifier JSString::toIdentifier(ExecState* exec) const { - ASSERT(s); - ASSERT(s[0]); - ASSERT(s[1]); - return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); + return Identifier(exec, toAtomicString(exec)); } - inline JSString* jsNontrivialString(JSGlobalData* globalData, const UString& s) + ALWAYS_INLINE AtomicString JSString::toAtomicString(ExecState* exec) const { - ASSERT(s.length() > 1); - return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); + if (isRope()) + static_cast(this)->resolveRopeToAtomicString(exec); + return AtomicString(m_value); + } + + ALWAYS_INLINE AtomicStringImpl* JSString::toExistingAtomicString(ExecState* exec) const + { + if (isRope()) + return static_cast(this)->resolveRopeToExistingAtomicString(exec); + if (m_value.impl()->isAtomic()) + return static_cast(m_value.impl()); + if (AtomicStringImpl* existingAtomicString = AtomicString::find(m_value.impl())) { + m_value = *existingAtomicString; + return existingAtomicString; + } + return nullptr; + } + + inline const String& JSString::value(ExecState* exec) const + { + if (isRope()) + static_cast(this)->resolveRope(exec); + return m_value; + } + + inline const String& JSString::tryGetValue() const + { + if (isRope()) + static_cast(this)->resolveRope(0); + return m_value; } inline JSString* JSString::getIndex(ExecState* exec, unsigned i) { ASSERT(canGetIndex(i)); if (isRope()) - return getIndexSlowCase(exec, i); + return static_cast(this)->getIndexSlowCase(exec, i); ASSERT(i < m_value.length()); return jsSingleCharacterSubstring(exec, m_value, i); } - inline JSString* jsString(JSGlobalData* globalData, const UString& s) + inline JSString* jsString(VM* vm, const String& s) { int size = s.length(); if (!size) - return globalData->smallStrings.emptyString(globalData); + return vm->smallStrings.emptyString(); if (size == 1) { - UChar c = s.characters()[0]; + UChar c = s.characterAt(0); if (c <= maxSingleCharacterString) - return globalData->smallStrings.singleCharacterString(globalData, c); + return vm->smallStrings.singleCharacterString(c); } - return fixupVPtr(globalData, new (globalData) JSString(globalData, s)); + return JSString::create(*vm, s.impl()); } inline JSString* jsSubstring(ExecState* exec, JSString* s, unsigned offset, unsigned length) @@ -496,118 +454,176 @@ namespace JSC { ASSERT(offset <= static_cast(s->length())); ASSERT(length <= static_cast(s->length())); ASSERT(offset + length <= static_cast(s->length())); - JSGlobalData* globalData = &exec->globalData(); + VM* vm = &exec->vm(); if (!length) - return globalData->smallStrings.emptyString(globalData); - if (s->isRope()) - return s->substringFromRope(exec, offset, length); - return jsSubstring(globalData, s->m_value, offset, length); + return vm->smallStrings.emptyString(); + return jsSubstring(vm, s->value(exec), offset, length); } - inline JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length) + inline JSString* jsSubstring8(VM* vm, const String& 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); + return vm->smallStrings.emptyString(); if (length == 1) { - UChar c = s.characters()[offset]; + UChar c = s.characterAt(offset); if (c <= maxSingleCharacterString) - return globalData->smallStrings.singleCharacterString(globalData, c); + return vm->smallStrings.singleCharacterString(c); } - return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(StringImpl::create(s.impl(), offset, length)), JSString::HasOtherOwner)); + return JSString::createHasOtherOwner(*vm, StringImpl::createSubstringSharingImpl8(s.impl(), offset, length)); } - inline JSString* jsOwnedString(JSGlobalData* globalData, const UString& s) + inline JSString* jsSubstring(VM* vm, const String& 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 vm->smallStrings.emptyString(); + if (length == 1) { + UChar c = s.characterAt(offset); + if (c <= maxSingleCharacterString) + return vm->smallStrings.singleCharacterString(c); + } + return JSString::createHasOtherOwner(*vm, StringImpl::createSubstringSharingImpl(s.impl(), offset, length)); + } + + inline JSString* jsOwnedString(VM* vm, const String& s) { int size = s.length(); if (!size) - return globalData->smallStrings.emptyString(globalData); + return vm->smallStrings.emptyString(); if (size == 1) { - UChar c = s.characters()[0]; + UChar c = s.characterAt(0); if (c <= maxSingleCharacterString) - return globalData->smallStrings.singleCharacterString(globalData, c); + return vm->smallStrings.singleCharacterString(c); } - return fixupVPtr(globalData, new (globalData) JSString(globalData, s, JSString::HasOtherOwner)); + return JSString::createHasOtherOwner(*vm, s.impl()); } - 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* 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); } - inline JSString* jsOwnedString(ExecState* exec, const UString& s) { return jsOwnedString(&exec->globalData(), s); } + inline JSRopeString* jsStringBuilder(VM* vm) + { + return JSRopeString::createNull(*vm); + } + + inline JSString* jsEmptyString(ExecState* exec) { return jsEmptyString(&exec->vm()); } + inline JSString* jsString(ExecState* exec, const String& s) { return jsString(&exec->vm(), s); } + inline JSString* jsSingleCharacterString(ExecState* exec, UChar c) { return jsSingleCharacterString(&exec->vm(), c); } + inline JSString* jsSubstring8(ExecState* exec, const String& s, unsigned offset, unsigned length) { return jsSubstring8(&exec->vm(), s, offset, length); } + inline JSString* jsSubstring(ExecState* exec, const String& s, unsigned offset, unsigned length) { return jsSubstring(&exec->vm(), s, offset, length); } + inline JSString* jsNontrivialString(ExecState* exec, const String& s) { return jsNontrivialString(&exec->vm(), s); } + inline JSString* jsOwnedString(ExecState* exec, const String& s) { return jsOwnedString(&exec->vm(), s); } + + JS_EXPORT_PRIVATE JSString* jsStringWithCacheSlowCase(VM&, StringImpl&); + + ALWAYS_INLINE JSString* jsStringWithCache(ExecState* exec, const String& s) + { + VM& vm = exec->vm(); + StringImpl* stringImpl = s.impl(); + if (!stringImpl || !stringImpl->length()) + return jsEmptyString(&vm); + + if (stringImpl->length() == 1) { + UChar singleCharacter = (*stringImpl)[0u]; + if (singleCharacter <= maxSingleCharacterString) + return vm.smallStrings.singleCharacterString(static_cast(singleCharacter)); + } + + if (JSString* lastCachedString = vm.lastCachedString.get()) { + if (lastCachedString->tryGetValueImpl() == stringImpl) + return lastCachedString; + } + + return jsStringWithCacheSlowCase(vm, *stringImpl); + } + + ALWAYS_INLINE JSString* jsStringWithCache(ExecState* exec, const AtomicString& s) + { + return jsStringWithCache(exec, s.string()); + } - ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) + ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, PropertyName propertyName, PropertySlot& slot) { if (propertyName == exec->propertyNames().length) { - slot.setValue(jsNumber(m_length)); + slot.setValue(this, DontEnum | DontDelete | ReadOnly, jsNumber(m_length)); return true; } - bool isStrictUInt32; - unsigned i = propertyName.toUInt32(isStrictUInt32); - if (isStrictUInt32 && i < m_length) { - slot.setValue(getIndex(exec, i)); + unsigned i = propertyName.asIndex(); + if (i < m_length) { + ASSERT(i != PropertyName::NotAnIndex); // No need for an explicit check, the above test would always fail! + slot.setValue(this, DontDelete | ReadOnly, getIndex(exec, i)); return true; } return false; } - + ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot) { if (propertyName < m_length) { - slot.setValue(getIndex(exec, propertyName)); + slot.setValue(this, DontDelete | ReadOnly, getIndex(exec, propertyName)); return true; } return false; } - inline bool isJSString(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsStringVPtr; } + inline bool isJSString(JSValue v) { return v.isCell() && v.asCell()->type() == StringType; } // --- JSValue inlines ---------------------------- + + inline bool JSValue::toBoolean(ExecState* exec) const + { + if (isInt32()) + return asInt32(); + if (isDouble()) + return asDouble() > 0.0 || asDouble() < 0.0; // false for NaN + if (isCell()) + return asCell()->toBoolean(exec); + return isTrue(); // false, null, and undefined all convert to false. + } + + inline JSString* JSValue::toString(ExecState* exec) const + { + if (isString()) + return jsCast(asCell()); + return toStringSlowCase(exec); + } - inline UString JSValue::toString(ExecState* exec) const + inline String JSValue::toWTFString(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); + return toWTFStringSlowCase(exec); } - inline UString JSValue::toPrimitiveString(ExecState* exec) const + ALWAYS_INLINE String inlineJSValueNotStringtoString(const JSValue& value, ExecState* 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); + VM& vm = exec->vm(); + if (value.isInt32()) + return vm.numericStrings.add(value.asInt32()); + if (value.isDouble()) + return vm.numericStrings.add(value.asDouble()); + if (value.isTrue()) + return vm.propertyNames->trueKeyword.string(); + if (value.isFalse()) + return vm.propertyNames->falseKeyword.string(); + if (value.isNull()) + return vm.propertyNames->nullKeyword.string(); + if (value.isUndefined()) + return vm.propertyNames->undefinedKeyword.string(); + return value.toString(exec)->value(exec); + } + + ALWAYS_INLINE String JSValue::toWTFStringInline(ExecState* exec) const + { + if (isString()) + return static_cast(asCell())->value(exec); + + return inlineJSValueNotStringtoString(*this, exec); } } // namespace JSC