diff --git a/common_components/common_runtime/base_runtime.cpp b/common_components/common_runtime/base_runtime.cpp index 0b75a938596e2556c4abed32ab01ea2f8fef165f..9549e1824771aef477aa96da6c8df927687d6226 100755 --- a/common_components/common_runtime/base_runtime.cpp +++ b/common_components/common_runtime/base_runtime.cpp @@ -246,4 +246,9 @@ void BaseRuntime::NotifyHighSensitive(bool isStart) { Heap::GetHeap().NotifyHighSensitive(isStart); } + +void BaseRuntime::FillFreeObject(void *object, size_t size) +{ + common::FillFreeObject(object, size); +} } // namespace common diff --git a/common_components/objects/base_string.cpp b/common_components/objects/base_string.cpp index 6ec3ada819b297d382b7cdc0a1bd3bce8883bbb3..82cb1e5abce0b14ccb21410bc0cbe0fc7a4599e2 100644 --- a/common_components/objects/base_string.cpp +++ b/common_components/objects/base_string.cpp @@ -12,475 +12,125 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "common_interfaces/objects/string/base_string.h" -#include -#include - +#include "common_interfaces/objects/string/base_string-inl.h" +#include "common_interfaces/objects/utils/utf_utils.h" #include "common_components/base/utf_helper.h" -#include "common_interfaces/objects/base_string.h" - #include "common_components/platform/string_hash.h" #include "common_components/platform/string_hash_helper.h" -namespace common { - constexpr size_t LOW_3BITS = 0x7; - constexpr size_t LOW_4BITS = 0xF; - constexpr size_t LOW_5BITS = 0x1F; - constexpr size_t LOW_6BITS = 0x3F; - constexpr size_t L_SURROGATE_START = 0xDC00; - constexpr size_t H_SURROGATE_START = 0xD800; - constexpr size_t SURROGATE_RAIR_START = 0x10000; - constexpr size_t OFFSET_18POS = 18; - constexpr size_t OFFSET_12POS = 12; - constexpr size_t OFFSET_10POS = 10; - constexpr size_t OFFSET_6POS = 6; - - size_t UtfUtils::DebuggerConvertRegionUtf16ToUtf8(const uint16_t* utf16In, uint8_t* utf8Out, size_t utf16Len, - size_t utf8Len, size_t start, bool modify, bool isWriteBuffer) - { - return common::utf_helper::DebuggerConvertRegionUtf16ToUtf8(utf16In, utf8Out, utf16Len, utf8Len, - start, modify, isWriteBuffer); - } - - size_t UtfUtils::Utf8ToUtf16Size(const uint8_t* utf8, size_t utf8Len) - { - return common::utf_helper::Utf8ToUtf16Size(utf8, utf8Len); - } - - size_t UtfUtils::Utf16ToUtf8Size(const uint16_t* utf16, uint32_t length, bool modify, bool isGetBufferSize, - bool cesu8) - { - return common::utf_helper::Utf16ToUtf8Size(utf16, length, modify, isGetBufferSize, cesu8); - } - - size_t UtfUtils::ConvertRegionUtf8ToUtf16(const uint8_t* utf8In, uint16_t* utf16Out, size_t utf8Len, - size_t utf16Len) - { - return common::utf_helper::ConvertRegionUtf8ToUtf16(utf8In, utf16Out, utf8Len, utf16Len); - } - - size_t UtfUtils::ConvertRegionUtf16ToLatin1(const uint16_t* utf16In, uint8_t* latin1Out, size_t utf16Len, - size_t latin1Len) - { - return common::utf_helper::ConvertRegionUtf16ToLatin1(utf16In, latin1Out, utf16Len, latin1Len); - } - - size_t UtfUtils::ConvertRegionUtf16ToUtf8(const uint16_t* utf16In, uint8_t* utf8Out, size_t utf16Len, - size_t utf8Len, size_t start, bool modify, bool isWriteBuffer, bool cesu) - { - return common::utf_helper::ConvertRegionUtf16ToUtf8( - utf16In, utf8Out, utf16Len, utf8Len, start, modify, isWriteBuffer, cesu); - } - - - // To change the hash algorithm of BaseString, please modify BaseString::CalculateConcatHashCode - // and BaseStringHashHelper::ComputeHashForDataPlatform simultaneously!! - template - uint32_t ComputeHashForDataInternal(const T *data, size_t size, uint32_t hashSeed) - { - if (size <= static_cast(StringHash::MIN_SIZE_FOR_UNROLLING)) { - uint32_t hash = hashSeed; - for (uint32_t i = 0; i < size; i++) { - hash = (hash << static_cast(StringHash::HASH_SHIFT)) - hash + data[i]; - } - return hash; - } - return StringHashHelper::ComputeHashForDataPlatform(data, size, hashSeed); - } - - PUBLIC_API uint32_t BaseString::ComputeHashForData(const uint8_t *data, size_t size, uint32_t hashSeed) - { - return ComputeHashForDataInternal(data, size, hashSeed); - } - - PUBLIC_API uint32_t BaseString::ComputeHashForData(const uint16_t *data, size_t size, uint32_t hashSeed) - { - return ComputeHashForDataInternal(data, size, hashSeed); - } - - uint32_t BaseString::ComputeHashcodeUtf8(const uint8_t *utf8Data, size_t utf8Len, bool canBeCompress) - { - if (utf8Len == 0) { - return MixHashcode(0, NOT_INTEGER); - } - if (canBeCompress) { - uint32_t mixHash = 0; - // String using UTF8 encoding, and length smaller than 10, try to compute integer hash. - if (utf8Len < MAX_ELEMENT_INDEX_LEN && HashIntegerString(utf8Data, utf8Len, &mixHash, 0)) { - return mixHash; - } - uint32_t hash = ComputeHashForData(utf8Data, utf8Len, 0); - return MixHashcode(hash, NOT_INTEGER); - } - auto utf16Len = UtfUtils::Utf8ToUtf16Size(utf8Data, utf8Len); - std::vector tmpBuffer(utf16Len); - [[maybe_unused]] auto len = UtfUtils::ConvertRegionUtf8ToUtf16(utf8Data, tmpBuffer.data(), utf8Len, - utf16Len); - DCHECK_CC(len == utf16Len); - uint32_t hash = ComputeHashForData(tmpBuffer.data(), utf16Len, 0); - return MixHashcode(hash, NOT_INTEGER); - } - - /* static */ - uint32_t BaseString::ComputeHashcodeUtf16(const uint16_t *utf16Data, uint32_t length) - { - if (length == 0) { - return MixHashcode(0, NOT_INTEGER); - } - uint32_t mixHash = 0; - // String length smaller than 10, try to compute integer hash. - if (length < MAX_ELEMENT_INDEX_LEN && HashIntegerString(utf16Data, length, &mixHash, 0)) { - return mixHash; - } - uint32_t hash = ComputeHashForData(utf16Data, length, 0); - return MixHashcode(hash, NOT_INTEGER); - } - - - // drop the tail bytes if the remain length can't fill the length it represents. - static size_t FixUtf8Len(const uint8_t* utf8, size_t utf8Len) - { - constexpr size_t TWO_BYTES_LENGTH = 2; - constexpr size_t THREE_BYTES_LENGTH = 3; - size_t trimSize = 0; - if (utf8Len >= 1 && utf8[utf8Len - 1] >= 0xC0) { - // The last one char claim there are more than 1 byte next to it, it's invalid, so drop the last one. - trimSize = 1; - } - if (utf8Len >= TWO_BYTES_LENGTH && utf8[utf8Len - TWO_BYTES_LENGTH] >= 0xE0) { - // The second to last char claim there are more than 2 bytes next to it, it's invalid, so drop the last two. - trimSize = TWO_BYTES_LENGTH; - } - if (utf8Len >= THREE_BYTES_LENGTH && utf8[utf8Len - THREE_BYTES_LENGTH] >= 0xF0) { - // The third to last char claim there are more than 3 bytes next to it, it's invalid, so drop the last - // three. - trimSize = THREE_BYTES_LENGTH; - } - return utf8Len - trimSize; - } - - /* static */ - bool BaseString::IsUtf8EqualsUtf16(const uint8_t* utf8Data, size_t utf8Len, - const uint16_t* utf16Data, uint32_t utf16Len) - { - size_t safeUtf8Len = FixUtf8Len(utf8Data, utf8Len); - const uint8_t* utf8End = utf8Data + utf8Len; - const uint8_t* utf8SafeEnd = utf8Data + safeUtf8Len; - const uint16_t* utf16End = utf16Data + utf16Len; - while (utf8Data < utf8SafeEnd && utf16Data < utf16End) { - uint8_t src = *utf8Data; - switch (src & 0xF0) { - case 0xF0: - { - const uint8_t c2 = *(++utf8Data); - const uint8_t c3 = *(++utf8Data); - const uint8_t c4 = *(++utf8Data); - uint32_t codePoint = ((src & LOW_3BITS) << OFFSET_18POS) | ((c2 & LOW_6BITS) << OFFSET_12POS) | - ((c3 & LOW_6BITS) << OFFSET_6POS) | (c4 & LOW_6BITS); - if (codePoint >= SURROGATE_RAIR_START) { - if (utf16Data >= utf16End - 1) { - return false; - } - codePoint -= SURROGATE_RAIR_START; - if (*utf16Data++ != static_cast((codePoint >> OFFSET_10POS) | - H_SURROGATE_START)) { - return false; - } else if (*utf16Data++ != static_cast((codePoint & 0x3FF) | L_SURROGATE_START)) { - return false; - } - } else { - if (*utf16Data++ != static_cast(codePoint)) { - return false; - } - } - utf8Data++; - break; - } - case 0xE0: - { - const uint8_t c2 = *(++utf8Data); - const uint8_t c3 = *(++utf8Data); - if (*utf16Data++ != static_cast(((src & LOW_4BITS) << OFFSET_12POS) | - ((c2 & LOW_6BITS) << OFFSET_6POS) | (c3 & LOW_6BITS))) { - return false; - } - utf8Data++; - break; - } - case 0xD0: - case 0xC0: - { - const uint8_t c2 = *(++utf8Data); - if (*utf16Data++ != static_cast(((src & LOW_5BITS) << OFFSET_6POS) | (c2 & - LOW_6BITS))) { - return false; - } - utf8Data++; - break; - } - default: - do { - if (*utf16Data++ != static_cast(*utf8Data++)) { - return false; - } - } - while (utf8Data < utf8SafeEnd && utf16Data < utf16End && *utf8Data < 0x80); - break; - } - } - // The remain chars should be treated as single byte char. - while (utf8Data < utf8End && utf16Data < utf16End) { - if (*utf16Data++ != static_cast(*utf8Data++)) { - return false; - } - } - return utf8Data == utf8End && utf16Data == utf16End; - } - - // static - template - uint32_t BaseString::CalculateDataConcatHashCode(const T1* dataFirst, size_t sizeFirst, - const T2* dataSecond, size_t sizeSecond) - { - uint32_t totalHash = ComputeHashForData(dataFirst, sizeFirst, 0); - totalHash = ComputeHashForData(dataSecond, sizeSecond, totalHash); - return MixHashcode(totalHash, NOT_INTEGER); - } - - template - uint32_t BaseString::CalculateDataConcatHashCode(const uint8_t* dataFirst, size_t sizeFirst, - const uint8_t* dataSecond, size_t sizeSecond); - template - uint32_t BaseString::CalculateDataConcatHashCode(const uint16_t* dataFirst, size_t sizeFirst, - const uint16_t* dataSecond, size_t sizeSecond); - template - uint32_t BaseString::CalculateDataConcatHashCode(const uint8_t* dataFirst, size_t sizeFirst, - const uint16_t* dataSecond, size_t sizeSecond); - template - uint32_t BaseString::CalculateDataConcatHashCode(const uint16_t* dataFirst, size_t sizeFirst, - const uint8_t* dataSecond, size_t sizeSecond); - - - bool BaseString::CanBeCompressed(const BaseString* string) - { - DCHECK_CC(string->IsLineString()); - if (string->IsUtf8()) { - return CanBeCompressed(string->GetDataUtf8(), string->GetLength()); - } - return CanBeCompressed(string->GetDataUtf16(), string->GetLength()); - } - - // static - bool BaseString::CanBeCompressed(const uint8_t* utf8Data, uint32_t utf8Len) - { - uint32_t index = 0; - for (; index + 4 <= utf8Len; index += 4) { - // 4: process the data in chunks of 4 elements to improve speed - // Check if all four characters in the current block are ASCII characters - if (!IsASCIICharacter(utf8Data[index]) || - !IsASCIICharacter(utf8Data[index + 1]) || // 1: the second element of the block - !IsASCIICharacter(utf8Data[index + 2]) || // 2: the third element of the block - !IsASCIICharacter(utf8Data[index + 3])) { - // 3: the fourth element of the block - return false; - } - } - // Check remaining characters if they are ASCII - for (; index < utf8Len; ++index) { - if (!IsASCIICharacter(utf8Data[index])) { - return false; - } - } - return true; - } - - /* static */ - bool BaseString::CanBeCompressed(const uint16_t* utf16Data, uint32_t utf16Len) - { - uint32_t index = 0; - for (; index + 4 <= utf16Len; index += 4) { - // 4: process the data in chunks of 4 elements to improve speed - // Check if all four characters in the current block are ASCII characters - if (!IsASCIICharacter(utf16Data[index]) || - !IsASCIICharacter(utf16Data[index + 1]) || // 1: the second element of the block - !IsASCIICharacter(utf16Data[index + 2]) || // 2: the third element of the block - !IsASCIICharacter(utf16Data[index + 3])) { - // 3: the fourth element of the block - return false; - } - } - // Check remaining characters if they are ASCII - for (; index < utf16Len; ++index) { - if (!IsASCIICharacter(utf16Data[index])) { - return false; - } - } - return true; - } - +#include +#include - bool BaseString::IsASCIICharacter(uint16_t data) - { - if (data == 0) { +namespace common { +size_t UtfUtils::DebuggerConvertRegionUtf16ToUtf8(const uint16_t* utf16In, uint8_t* utf8Out, size_t utf16Len, + size_t utf8Len, size_t start, bool modify, bool isWriteBuffer) +{ + return utf_helper::DebuggerConvertRegionUtf16ToUtf8(utf16In, utf8Out, utf16Len, utf8Len, start, modify, + isWriteBuffer); +} + +size_t UtfUtils::ConvertRegionUtf16ToLatin1(const uint16_t* utf16In, uint8_t* latin1Out, size_t utf16Len, + size_t latin1Len) +{ + return utf_helper::ConvertRegionUtf16ToLatin1(utf16In, latin1Out, utf16Len, latin1Len); +} + +// To change the hash algorithm of BaseString, please modify BaseString::CalculateConcatHashCode +// and BaseStringHashHelper::ComputeHashForDataPlatform simultaneously!! +template +uint32_t ComputeHashForDataInternal(const T *data, size_t size, uint32_t hashSeed) +{ + if (size <= static_cast(StringHash::MIN_SIZE_FOR_UNROLLING)) { + uint32_t hash = hashSeed; + for (uint32_t i = 0; i < size; i++) { + hash = (hash << static_cast(StringHash::HASH_SHIFT)) - hash + data[i]; + } + return hash; + } + return StringHashHelper::ComputeHashForDataPlatform(data, size, hashSeed); +} + +PUBLIC_API uint32_t BaseString::ComputeHashForData(const uint8_t *data, size_t size, uint32_t hashSeed) +{ + return ComputeHashForDataInternal(data, size, hashSeed); +} + +PUBLIC_API uint32_t BaseString::ComputeHashForData(const uint16_t *data, size_t size, uint32_t hashSeed) +{ + return ComputeHashForDataInternal(data, size, hashSeed); +} + +// static +template +uint32_t BaseString::CalculateDataConcatHashCode(const T1* dataFirst, size_t sizeFirst, + const T2* dataSecond, size_t sizeSecond) +{ + uint32_t totalHash = ComputeHashForData(dataFirst, sizeFirst, 0); + totalHash = ComputeHashForData(dataSecond, sizeSecond, totalHash); + return MixHashcode(totalHash, NOT_INTEGER); +} + +template +uint32_t BaseString::CalculateDataConcatHashCode(const uint8_t* dataFirst, size_t sizeFirst, + const uint8_t* dataSecond, size_t sizeSecond); +template +uint32_t BaseString::CalculateDataConcatHashCode(const uint16_t* dataFirst, size_t sizeFirst, + const uint16_t* dataSecond, size_t sizeSecond); +template +uint32_t BaseString::CalculateDataConcatHashCode(const uint8_t* dataFirst, size_t sizeFirst, + const uint16_t* dataSecond, size_t sizeSecond); +template +uint32_t BaseString::CalculateDataConcatHashCode(const uint16_t* dataFirst, size_t sizeFirst, + const uint8_t* dataSecond, size_t sizeSecond); + + +template +bool IsSubStringAtSpan(common::Span& lhsSp, common::Span& rhsSp, uint32_t offset) +{ + size_t rhsSize = rhsSp.size(); + DCHECK_CC(rhsSize + offset <= lhsSp.size()); + for (size_t i = 0; i < rhsSize; ++i) { + auto left = static_cast(lhsSp[offset + static_cast(i)]); + auto right = static_cast(rhsSp[i]); + if (left != right) { return false; } - // \0 is not considered ASCII in Ecma-Modified-UTF8 [only modify '\u0000'] - return data <= UtfUtils::UTF8_1B_MAX; - } - - - /* static */ - template - int32_t BaseString::IndexOf(Span& lhsSp, Span& rhsSp, int32_t pos, int32_t max) - { - DCHECK_CC(rhsSp.size() > 0); - auto first = static_cast(rhsSp[0]); - for (int32_t i = pos; i <= max; i++) { - if (static_cast(lhsSp[i]) != first) { - i++; - while (i <= max && static_cast(lhsSp[i]) != first) { - i++; - } - } - /* Found first character, now look at the rest of rhsSp */ - if (i <= max) { - int j = i + 1; - int end = j + static_cast(rhsSp.size()) - 1; - - for (int k = 1; j < end && static_cast(lhsSp[j]) == static_cast(rhsSp[k]); j++, k++) { - } - if (j == end) { - /* Found whole string. */ - return i; - } - } - } - return -1; - } - - template - int32_t BaseString::IndexOf(Span& lhsSp, Span& rhsSp, int32_t pos, - int32_t max); - template - int32_t BaseString::IndexOf(Span& lhsSp, Span& rhsSp, - int32_t pos, int32_t max); - - template - int32_t BaseString::IndexOf(Span& lhsSp, Span& rhsSp, int32_t pos, - int32_t max); - - template - int32_t BaseString::IndexOf(Span& lhsSp, Span& rhsSp, int32_t pos, - int32_t max); - - - template - int32_t BaseString::LastIndexOf(Span& lhsSp, Span& rhsSp, int32_t pos) - { - int rhsSize = static_cast(rhsSp.size()); - DCHECK_CC(rhsSize > 0); - auto first = rhsSp[0]; - for (int32_t i = pos; i >= 0; i--) { - if (lhsSp[i] != first) { - continue; - } - /* Found first character, now look at the rest of rhsSp */ - int j = 1; - while (j < rhsSize) { - if (rhsSp[j] != lhsSp[i + j]) { - break; - } - j++; - } - if (j == rhsSize) { - return i; - } - } - return -1; - } - - template - int32_t BaseString::LastIndexOf(Span& lhsSp, Span& rhsSp, - int32_t pos); - template - int32_t BaseString::LastIndexOf(Span& lhsSp, Span& rhsSp, - int32_t pos); - template - int32_t BaseString::LastIndexOf(Span& lhsSp, Span& rhsSp, - int32_t pos); - template - int32_t BaseString::LastIndexOf(Span& lhsSp, Span& rhsSp, - int32_t pos); - - - template - int32_t CompareStringSpan(Span& lhsSp, Span& rhsSp, int32_t count) - { - for (int32_t i = 0; i < count; ++i) { - auto left = static_cast(lhsSp[i]); - auto right = static_cast(rhsSp[i]); - if (left != right) { - return left - right; - } - } - return 0; - } - - template - int32_t CompareStringSpan(Span& lhsSp, Span& rhsSp, - int32_t count); - template - int32_t CompareStringSpan(Span& lhsSp, Span& rhsSp, - int32_t count); - template - int32_t CompareStringSpan(Span& lhsSp, Span& rhsSp, - int32_t count); - template - int32_t CompareStringSpan(Span& lhsSp, Span& rhsSp, - int32_t count); - - - template - bool IsSubStringAtSpan(Span& lhsSp, Span& rhsSp, uint32_t offset) - { - int rhsSize = static_cast(rhsSp.size()); - DCHECK_CC(rhsSize + offset <= lhsSp.size()); - for (int i = 0; i < rhsSize; ++i) { - auto left = static_cast(lhsSp[offset + static_cast(i)]); - auto right = static_cast(rhsSp[i]); - if (left != right) { - return false; - } - } - return true; - } - - template - bool IsSubStringAtSpan(Span& lhsSp, Span& rhsSp, - uint32_t offset); - template - bool IsSubStringAtSpan(Span& lhsSp, Span& rhsSp, - uint32_t offset); - template - bool IsSubStringAtSpan(Span& lhsSp, Span& rhsSp, - uint32_t offset); - template - bool IsSubStringAtSpan(Span& lhsSp, Span& rhsSp, - uint32_t offset); - - - std::u16string Utf16ToU16String(const uint16_t* utf16Data, uint32_t dataLen) - { - auto* char16tData = reinterpret_cast(utf16Data); - std::u16string u16str(char16tData, dataLen); - return u16str; - } - - std::u16string Utf8ToU16String(const uint8_t* utf8Data, uint32_t dataLen) - { - auto* charData = reinterpret_cast(utf8Data); - std::string str(charData, dataLen); - std::u16string u16str = std::wstring_convert, char16_t>{}.from_bytes(str); - return u16str; } -} // namespace common + return true; +} + +template +bool IsSubStringAtSpan(common::Span& lhsSp, + common::Span& rhsSp, + uint32_t offset); +template +bool IsSubStringAtSpan(common::Span& lhsSp, + common::Span& rhsSp, + uint32_t offset); +template +bool IsSubStringAtSpan(common::Span& lhsSp, + common::Span& rhsSp, + uint32_t offset); +template +bool IsSubStringAtSpan(common::Span& lhsSp, + common::Span& rhsSp, + uint32_t offset); + + +std::u16string Utf16ToU16String(const uint16_t* utf16Data, uint32_t dataLen) +{ + auto* char16tData = reinterpret_cast(utf16Data); + std::u16string u16str(char16tData, dataLen); + return u16str; +} + +std::u16string Utf8ToU16String(const uint8_t* utf8Data, uint32_t dataLen) +{ + auto* charData = reinterpret_cast(utf8Data); + std::string str(charData, dataLen); + std::u16string u16str = std::wstring_convert, char16_t>{}.from_bytes(str); + return u16str; +} +} // namespace common diff --git a/common_components/objects/base_string_table.cpp b/common_components/objects/base_string_table.cpp index d8d3cc807341c5128fd7543f05bd4b95e3e42bf1..3edad3bdd5c9f19098d9b49a860c62aaa02a62ce 100644 --- a/common_components/objects/base_string_table.cpp +++ b/common_components/objects/base_string_table.cpp @@ -22,7 +22,14 @@ #include "common_components/objects/string_table_internal.h" #include "common_components/taskpool/taskpool.h" #include "common_components/mutator/thread_local.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" +#include "common_interfaces/objects/string/base_string-inl.h" +#include "common_interfaces/objects/string/line_string.h" +#include "common_interfaces/objects/string/line_string-inl.h" +#include "common_interfaces/objects/string/tree_string.h" +#include "common_interfaces/objects/string/tree_string-inl.h" +#include "common_interfaces/objects/string/sliced_string.h" +#include "common_interfaces/objects/string/sliced_string-inl.h" #include "common_interfaces/thread/thread_holder.h" #include "common_interfaces/thread/thread_state_transition.h" #include "heap/heap_allocator.h" @@ -31,10 +38,10 @@ namespace common { template BaseString* BaseStringTableInternal::AllocateLineStringObject(size_t size) { - size = AlignUp(size, ALIGN_OBJECT); + size = AlignmentUp(size, ALIGN_OBJECT); BaseString* str = reinterpret_cast(HeapAllocator::AllocateInOldOrHuge(size, LanguageType::DYNAMIC)); - BaseClass* cls = BaseRuntime::GetInstance()->GetBaseClassRoots().GetBaseClass(CommonType::LINE_STRING); + BaseClass* cls = BaseRuntime::GetInstance()->GetBaseClassRoots().GetBaseClass(ObjectType::LINE_STRING); str->SetFullBaseClassWithoutBarrier(cls); return str; } @@ -44,7 +51,7 @@ BaseString* BaseStringTableInternal::GetOrInternFlattenString( ThreadHolder* holder, const HandleCreator& handleCreator, BaseString* string) { - ASSERT(string->NotTreeString()); + DCHECK_CC(!string->IsTreeString()); if (string->IsInternString()) { return string; } @@ -62,7 +69,7 @@ BaseString* BaseStringTableInternal::GetOrInternFlattenString( ReadOnlyHandle stringHandle = handleCreator(holder, string); BaseString* result = stringTable_.template StoreOrLoad( holder, readBarrier, hashcode, loadResult, stringHandle); - ASSERT(result != nullptr); + DCHECK_CC(result != nullptr); return result; } @@ -72,7 +79,7 @@ BaseString* BaseStringTableInternal::GetOrInternStringFromCompr const ReadOnlyHandle& string, uint32_t offset, uint32_t utf8Len) { - const uint8_t* utf8Data = string->GetDataUtf8() + offset; + const uint8_t* utf8Data = ReadOnlyHandle::Cast(string)->GetDataUtf8() + offset; uint32_t hashcode = BaseString::ComputeHashcodeUtf8(utf8Data, utf8Len, true); auto readBarrier = [](void* obj, size_t offset)-> BaseObject* { return BaseObject::Cast( @@ -83,24 +90,24 @@ BaseString* BaseStringTableInternal::GetOrInternStringFromCompr if (loadResult.value != nullptr) { return loadResult.value; } - auto allocator = [](size_t size, CommonType type)-> BaseString* { - ASSERT(type == CommonType::LINE_STRING); + auto allocator = [](size_t size, ObjectType type)-> BaseString* { + DCHECK_CC(type == ObjectType::LINE_STRING); return AllocateLineStringObject(size); }; BaseString* result = stringTable_.template StoreOrLoad( holder, hashcode, loadResult, [holder, string, offset, utf8Len, hashcode, handleCreator, allocator]() { - BaseString* str = BaseString::CreateFromUtf8CompressedSubString( + BaseString* str = LineString::CreateFromUtf8CompressedSubString( std::move(allocator), string, offset, utf8Len); str->SetMixHashcode(hashcode); - ASSERT(!str->IsInternString()); - ASSERT(str->NotTreeString()); + DCHECK_CC(!str->IsInternString()); + DCHECK_CC(!str->IsTreeString()); // Strings in string table should not be in the young space. ReadOnlyHandle strHandle = handleCreator(holder, str); return strHandle; }, [utf8Len, string, offset](const BaseString* foundString) { - const uint8_t* utf8Data = string->GetDataUtf8() + offset; + const uint8_t* utf8Data = ReadOnlyHandle::Cast(string)->GetDataUtf8() + offset; auto readBarrier = [](void* obj, size_t offset)-> BaseObject* { return BaseObject::Cast( reinterpret_cast(BaseRuntime::ReadBarrier( @@ -108,7 +115,7 @@ BaseString* BaseStringTableInternal::GetOrInternStringFromCompr }; return BaseString::StringIsEqualUint8Data(readBarrier, foundString, utf8Data, utf8Len, true); }); - ASSERT(result != nullptr); + DCHECK_CC(result != nullptr); return result; } @@ -120,17 +127,17 @@ BaseString* BaseStringTableInternal::GetOrInternString(ThreadHo bool canBeCompress) { uint32_t hashcode = BaseString::ComputeHashcodeUtf8(utf8Data, utf8Len, canBeCompress); - auto allocator = [](size_t size, CommonType type)-> BaseString* { - ASSERT(type == CommonType::LINE_STRING); + auto allocator = [](size_t size, ObjectType type)-> BaseString* { + DCHECK_CC(type == ObjectType::LINE_STRING); return AllocateLineStringObject(size); }; BaseString* result = stringTable_.template LoadOrStore( holder, hashcode, [holder, hashcode, utf8Data, utf8Len, canBeCompress, handleCreator, allocator]() { - BaseString* value = BaseString::CreateFromUtf8(std::move(allocator), utf8Data, utf8Len, canBeCompress); + BaseString* value = LineString::CreateFromUtf8(std::move(allocator), utf8Data, utf8Len, canBeCompress); value->SetMixHashcode(hashcode); - ASSERT(!value->IsInternString()); - ASSERT(value->NotTreeString()); + DCHECK_CC(!value->IsInternString()); + DCHECK_CC(!value->IsTreeString()); ReadOnlyHandle stringHandle = handleCreator(holder, value); return stringHandle; }, @@ -143,7 +150,7 @@ BaseString* BaseStringTableInternal::GetOrInternString(ThreadHo return BaseString::StringIsEqualUint8Data(readBarrier, foundString, utf8Data, utf8Len, canBeCompress); }); - ASSERT(result != nullptr); + DCHECK_CC(result != nullptr); return result; } @@ -154,18 +161,18 @@ BaseString* BaseStringTableInternal::GetOrInternString( bool canBeCompress) { uint32_t hashcode = BaseString::ComputeHashcodeUtf16(const_cast(utf16Data), utf16Len); - auto allocator = [](size_t size, CommonType type)-> BaseString* { - ASSERT(type == CommonType::LINE_STRING); + auto allocator = [](size_t size, ObjectType type)-> BaseString* { + DCHECK_CC(type == ObjectType::LINE_STRING); return AllocateLineStringObject(size); }; BaseString* result = stringTable_.template LoadOrStore( holder, hashcode, [holder, utf16Data, utf16Len, canBeCompress, hashcode, handleCreator, allocator]() { - BaseString* value = BaseString::CreateFromUtf16(std::move(allocator), utf16Data, utf16Len, + BaseString* value = LineString::CreateFromUtf16(std::move(allocator), utf16Data, utf16Len, canBeCompress); value->SetMixHashcode(hashcode); - ASSERT(!value->IsInternString()); - ASSERT(value->NotTreeString()); + DCHECK_CC(!value->IsInternString()); + DCHECK_CC(!value->IsTreeString()); // Strings in string table should not be in the young space. ReadOnlyHandle stringHandle = handleCreator(holder, value); return stringHandle; @@ -178,7 +185,7 @@ BaseString* BaseStringTableInternal::GetOrInternString( }; return BaseString::StringsAreEqualUtf16(readBarrier, foundString, utf16Data, utf16Len); }); - ASSERT(result != nullptr); + DCHECK_CC(result != nullptr); return result; } @@ -199,7 +206,7 @@ template > void BaseStringTableInternal::SweepWeakRef(const WeakRefFieldVisitor& visitor, uint32_t rootID, std::vector& waitDeleteEntries) { - ASSERT(rootID >= 0 && rootID < TrieMapConfig::ROOT_SIZE); + DCHECK_CC(rootID >= 0 && rootID < TrieMapConfig::ROOT_SIZE); auto rootNode = stringTable_.root_[rootID].load(std::memory_order_relaxed); if (rootNode == nullptr) { return; diff --git a/common_components/objects/composite_base_class.cpp b/common_components/objects/composite_base_class.cpp index c1d2b8fb9c0ff5bf69441e3b91679791214a61e7..e014bdfb37c84901cbc0392f823640d14b84b71d 100644 --- a/common_components/objects/composite_base_class.cpp +++ b/common_components/objects/composite_base_class.cpp @@ -25,12 +25,12 @@ void BaseClassRoots::InitializeCompositeBaseClass(CompositeBaseClassAllocator &a if (initialized_.exchange(true)) { return; } - CreateCompositeBaseClass(CommonType::LINE_STRING, allocator); - CreateCompositeBaseClass(CommonType::SLICED_STRING, allocator); - CreateCompositeBaseClass(CommonType::TREE_STRING, allocator); + CreateCompositeBaseClass(ObjectType::LINE_STRING, allocator); + CreateCompositeBaseClass(ObjectType::SLICED_STRING, allocator); + CreateCompositeBaseClass(ObjectType::TREE_STRING, allocator); } -void BaseClassRoots::CreateCompositeBaseClass(CommonType type, CompositeBaseClassAllocator& allocator) +void BaseClassRoots::CreateCompositeBaseClass(ObjectType type, CompositeBaseClassAllocator& allocator) { CompositeBaseClass* classObject = allocator(); classObject->class_.ClearBitField(); @@ -40,7 +40,7 @@ void BaseClassRoots::CreateCompositeBaseClass(CommonType type, CompositeBaseClas baseClasses_[index] = &classObject->class_; } -BaseClass* BaseClassRoots::GetBaseClass(CommonType type) const +BaseClass* BaseClassRoots::GetBaseClass(ObjectType type) const { return baseClasses_[TypeToIndex[static_cast(type)]]; } diff --git a/common_components/objects/string_table/hashtriemap-inl.h b/common_components/objects/string_table/hashtriemap-inl.h index d6c89eb4a4c52acb3facef2a189219ab731cb6fa..f995a98e0354e2538fc6ff2054c5a7bc91452a1d 100644 --- a/common_components/objects/string_table/hashtriemap-inl.h +++ b/common_components/objects/string_table/hashtriemap-inl.h @@ -18,7 +18,8 @@ #include "common_components/log/log.h" #include "common_interfaces/objects/readonly_handle.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" +#include "common_interfaces/objects/string/line_string-inl.h" #include "common_components/objects/string_table/hashtriemap.h" #include "common_components/objects/string_table/integer_cache.h" @@ -49,7 +50,7 @@ typename HashTrieMap::Node* HashTrieMap::Load(ReadBarrier&& re } LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } // LoadOrStore returns the existing value of the key, if it exists. @@ -172,7 +173,7 @@ BaseString* HashTrieMap::LoadOrStore(ThreadHol #ifndef NDEBUG if (!haveInsertPoint) { LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } #endif // invoke the callback to create str @@ -185,7 +186,7 @@ BaseString* HashTrieMap::LoadOrStore(ThreadHol GetMutex().LockWithThreadState(holder); } - ASSERT(slot != nullptr); + DCHECK_CC(slot != nullptr); node = slot->load(std::memory_order_acquire); if (node == nullptr || node->IsEntry()) { // see is still real, so can continue to insert. @@ -225,7 +226,7 @@ BaseString* HashTrieMap::LoadOrStore(ThreadHol } BaseString* value = *str; - ASSERT(value != nullptr); + DCHECK_CC(value != nullptr); value->SetIsInternString(); IntegerCache::InitIntegerCache(value); Entry* newEntry = new Entry(value); @@ -297,14 +298,14 @@ BaseString* HashTrieMap::LoadOrStoreForJit(Thr #ifndef NDEBUG if (!haveInsertPoint) { LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } #endif // Jit need to lock the object before creating the object GetMutex().LockWithThreadState(holder); // invoke the callback to create str value = std::invoke(std::forward(loaderCallback)); - ASSERT(slot != nullptr); + DCHECK_CC(slot != nullptr); node = slot->load(std::memory_order_acquire); if (node == nullptr || node->IsEntry()) { // see is still real, so can continue to insert. @@ -337,7 +338,7 @@ BaseString* HashTrieMap::LoadOrStoreForJit(Thr } } - ASSERT(value != nullptr); + DCHECK_CC(value != nullptr); value->SetIsInternString(); IntegerCache::InitIntegerCache(value); Entry* newEntry = new Entry(value); @@ -411,7 +412,7 @@ BaseString* HashTrieMap::StoreOrLoad(ThreadHol #ifndef NDEBUG if (!haveInsertPoint) { LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } #endif // lock and double-check @@ -448,7 +449,7 @@ BaseString* HashTrieMap::StoreOrLoad(ThreadHol } BaseString* value = *str; - ASSERT(value != nullptr); + DCHECK_CC(value != nullptr); value->SetIsInternString(); IntegerCache::InitIntegerCache(value); Entry* newEntry = new Entry(value); @@ -502,7 +503,7 @@ HashTrieMapLoadResult HashTrieMap::Load(ReadBa } LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } // Load returns the value of the key stored in the mapping, or HashTrieMapLoadResult for StoreOrLoad @@ -514,7 +515,7 @@ HashTrieMapLoadResult HashTrieMap::Load(ReadBa { uint32_t hash = key; Indirect* current = GetRootAndProcessHash(hash); - const uint8_t* utf8Data = string->GetDataUtf8() + offset; + const uint8_t* utf8Data = ReadOnlyHandle::Cast(string)->GetDataUtf8() + offset; for (uint32_t hashShift = 0; hashShift < TrieMapConfig::TOTAL_HASH_BITS; hashShift += TrieMapConfig::N_CHILDREN_LOG2) { size_t index = (hash >> hashShift) & TrieMapConfig::N_CHILDREN_MASK; @@ -543,7 +544,7 @@ HashTrieMapLoadResult HashTrieMap::Load(ReadBa } LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } // Based on the loadResult, try the store first @@ -605,7 +606,7 @@ BaseString* HashTrieMap::StoreOrLoad(ThreadHol #ifndef NDEBUG if (!haveInsertPoint) { LOG_COMMON(FATAL) << "StringTable: ran out of hash bits while iterating"; - UNREACHABLE(); + UNREACHABLE_CC(); } #endif // lock and double-check @@ -647,7 +648,7 @@ BaseString* HashTrieMap::StoreOrLoad(ThreadHol } BaseString* value = *str; - ASSERT(value != nullptr); + DCHECK_CC(value != nullptr); value->SetIsInternString(); IntegerCache::InitIntegerCache(value); Entry* newEntry = new Entry(value); @@ -688,7 +689,7 @@ template bool HashTrieMap::CheckValidity(ReadBarrier&& readBarrier, BaseString* value, bool& isValid) { - if (!value->NotTreeString()) { + if (value->IsTreeString()) { isValid = false; return false; } diff --git a/common_components/objects/string_table/hashtriemap.h b/common_components/objects/string_table/hashtriemap.h index 4a51642851c7843e929f2a074fd735bb68194484..e86aa9e3a3207a6bc23197be46cdbb52717c689c 100644 --- a/common_components/objects/string_table/hashtriemap.h +++ b/common_components/objects/string_table/hashtriemap.h @@ -18,8 +18,10 @@ #include "common_components/heap/heap.h" #include "common_components/log/log.h" +#include "common_interfaces/base/common.h" #include "common_interfaces/objects/readonly_handle.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" +#include "common_interfaces/objects/string/base_string-inl.h" namespace panda::ecmascript { class TaggedObject; @@ -172,13 +174,13 @@ struct HashTrieMapLoadResult { inline HashTrieMapEntry* HashTrieMapNode::AsEntry() { - ASSERT(IsEntry() && "HashTrieMap: called entry on non-entry node"); + DCHECK_CC(IsEntry() && "HashTrieMap: called entry on non-entry node"); return static_cast(this); } inline HashTrieMapIndirect* HashTrieMapNode::AsIndirect() { - ASSERT(!IsEntry() && "HashTrieMap: called indirect on entry node"); + DCHECK_CC(!IsEntry() && "HashTrieMap: called indirect on entry node"); return static_cast(this); } @@ -197,7 +199,7 @@ public: { Clear(); }; - + #if ECMASCRIPT_ENABLE_TRACE_STRING_TABLE class StringTableTracer { public: @@ -207,10 +209,10 @@ public: static StringTableTracer tracer; return tracer; } - + NO_COPY_SEMANTIC_CC(StringTableTracer); NO_MOVE_SEMANTIC_CC(StringTableTracer); - + void TraceFindSuccess(uint32_t hashShift) { totalDepth_.fetch_add(hashShift / TrieMapConfig::N_CHILDREN_LOG2 + 1, std::memory_order_relaxed); @@ -219,33 +221,33 @@ public: DumpWithLock(currentSuccess); } } - + void TraceFindFail() { totalFailNum_.fetch_add(1, std::memory_order_relaxed); } - + private: StringTableTracer() = default; - + void DumpWithLock(uint64_t triggerPoint) { std::lock_guard lock(mu_); - + if (triggerPoint >= lastDumpPoint_.load(std::memory_order_relaxed) + DUMP_THRESHOLD) { lastDumpPoint_ = triggerPoint; DumpInfo(); } } - + void DumpInfo() const { uint64_t depth = totalDepth_.load(std::memory_order_relaxed); uint64_t success = totalSuccessNum_.load(std::memory_order_relaxed); uint64_t fail = totalFailNum_.load(std::memory_order_relaxed); - + double avgDepth = (static_cast(depth) / success); - + LOG_COMMON(INFO) << "------------------------------------------------------------" << "---------------------------------------------------------"; LOG_COMMON(INFO) << "StringTableTotalSuccessFindNum: " << success; @@ -254,14 +256,14 @@ public: LOG_COMMON(INFO) << "------------------------------------------------------------" << "---------------------------------------------------------"; } - + std::mutex mu_; std::atomic totalDepth_{0}; std::atomic totalSuccessNum_{0}; std::atomic totalFailNum_{0}; std::atomic lastDumpPoint_{0}; }; - + void TraceFindSuccessDepth(uint32_t hashShift) { StringTableTracer::GetInstance().TraceFindSuccess(hashShift); @@ -294,7 +296,7 @@ public: template BaseString* LoadOrStoreForJit(ThreadHolder* holder, const uint32_t key, LoaderCallback loaderCallback, EqualsCallback equalsCallback); - + static void ProcessHash(uint32_t &hash) { hash >>= TrieMapConfig::ROOT_BIT; @@ -310,7 +312,7 @@ public: } else { Indirect* expected = nullptr; Indirect* newRoot = new Indirect(); - + if (root_[rootID].compare_exchange_strong(expected, newRoot, std::memory_order_release, std::memory_order_acquire)) { return newRoot; @@ -377,7 +379,7 @@ public: // ut used const std::atomic& GetRoot(uint32_t index) const { - ASSERT(index < TrieMapConfig::ROOT_SIZE); + DCHECK_CC(index < TrieMapConfig::ROOT_SIZE); return root_[index]; } @@ -475,8 +477,8 @@ public: hashTrieMap_->DecreaseInuseCount(); } - NO_COPY_SEMANTIC(HashTrieMapInUseScope); - NO_MOVE_SEMANTIC(HashTrieMapInUseScope); + NO_COPY_SEMANTIC_CC(HashTrieMapInUseScope); + NO_MOVE_SEMANTIC_CC(HashTrieMapInUseScope); private: HashTrieMap* hashTrieMap_; diff --git a/common_components/objects/string_table/integer_cache.h b/common_components/objects/string_table/integer_cache.h index 0c875414bb4d9a47488af125ab6e818eef462340..0aaf85255e875d099d7649df82d55125fcb05ed3 100644 --- a/common_components/objects/string_table/integer_cache.h +++ b/common_components/objects/string_table/integer_cache.h @@ -17,7 +17,8 @@ #define COMMON_COMPONENTS_OBJECTS_STRING_TABLE_INTEGER_CACHE_H #include -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" +#include "common_interfaces/objects/string/line_string.h" namespace common { @@ -52,7 +53,7 @@ public: { DCHECK_CC(string->IsUtf8() && string->GetLength() <= MAX_INTEGER_CACHE_SIZE && string->GetLength() > 0 && string->IsInternString()); - IntegerCache* cache = reinterpret_cast(string->GetData()); + IntegerCache* cache = reinterpret_cast(LineString::Cast(string)->GetData()); return cache; } diff --git a/common_components/objects/string_table_internal.h b/common_components/objects/string_table_internal.h index 0834bd1e47f84dcca2b9f3a414fc2cc4139016a2..67edfcde547822ceadf53708b6c82000947d92a4 100644 --- a/common_components/objects/string_table_internal.h +++ b/common_components/objects/string_table_internal.h @@ -71,8 +71,8 @@ public: void CleanUp(); private: - NO_COPY_SEMANTIC(BaseStringTableCleaner); - NO_MOVE_SEMANTIC(BaseStringTableCleaner); + NO_COPY_SEMANTIC_CC(BaseStringTableCleaner); + NO_MOVE_SEMANTIC_CC(BaseStringTableCleaner); static void ProcessSweepWeakRef(IteratorPtr &iter, BaseStringTableCleaner *cleaner, const WeakRefFieldVisitor &visitor); @@ -100,8 +100,8 @@ private: bool Run(uint32_t threadIndex) override; - NO_COPY_SEMANTIC(CMCSweepWeakRefTask); - NO_MOVE_SEMANTIC(CMCSweepWeakRefTask); + NO_COPY_SEMANTIC_CC(CMCSweepWeakRefTask); + NO_MOVE_SEMANTIC_CC(CMCSweepWeakRefTask); private: IteratorPtr iter_; diff --git a/common_components/objects/tests/base_string_table_test.cpp b/common_components/objects/tests/base_string_table_test.cpp index aa165055f1b83685b0faa961d924c2bb8705d48a..1ce5a23bb9fba635c42b62dbdc021785e2d0365d 100644 --- a/common_components/objects/tests/base_string_table_test.cpp +++ b/common_components/objects/tests/base_string_table_test.cpp @@ -17,11 +17,11 @@ #include "common_interfaces/objects/base_string_table.h" #include "common_components/objects/string_table_internal.h" #include "common_interfaces/thread/mutator_base.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" #include "common_interfaces/thread/thread_holder.h" #include "common_interfaces/base_runtime.h" #include "common_interfaces/heap/heap_allocator.h" -#include "common_interfaces/objects/string/base_string-inl2.h" +#include "common_interfaces/objects/string/base_string-inl.h" namespace common { @@ -66,7 +66,7 @@ protected: BaseString* CreateUtf8String(const char* utf8Data, uint32_t length, bool canBeCompress) { - auto allocator = [](size_t size, CommonType type) -> BaseString* { + auto allocator = [](size_t size, ObjectType type) -> BaseString* { void* mem = reinterpret_cast(HeapAllocator::AllocateInOldOrHuge(size, LanguageType::DYNAMIC)); if (mem == nullptr) { return nullptr; @@ -74,7 +74,7 @@ protected: return reinterpret_cast(mem); }; - BaseString* str = BaseString::CreateFromUtf8(allocator, + BaseString* str = LineString::CreateFromUtf8(allocator, reinterpret_cast(utf8Data), length, canBeCompress); if (str == nullptr) { diff --git a/common_components/objects/tests/base_string_test.cpp b/common_components/objects/tests/base_string_test.cpp index 495a30af2373d5ca1364719403757f43fced9725..25d510a7543d3c691080dcd8282f8bbc5f3ac5a2 100755 --- a/common_components/objects/tests/base_string_test.cpp +++ b/common_components/objects/tests/base_string_test.cpp @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" #include "common_components/platform/string_hash.h" #include "common_components/tests/test_helper.h" +#include "objects/string/base_string-inl.h" using namespace common; diff --git a/common_components/objects/tests/composite_base_class_test.cpp b/common_components/objects/tests/composite_base_class_test.cpp index d5a1c91b7f19b1f4c39b001150f8cdc4a4e6a2b7..370d23065d5c88e5de9480e20d3f22479f95cdfd 100644 --- a/common_components/objects/tests/composite_base_class_test.cpp +++ b/common_components/objects/tests/composite_base_class_test.cpp @@ -29,7 +29,7 @@ protected: void* memory = ::operator new(sizeof(uint64_t) * 16); auto* baseClass = reinterpret_cast(memory); - baseClass->SetObjectType(CommonType::LINE_STRING); + baseClass->SetObjectType(ObjectType::LINE_STRING); baseClass->ClearBitField(); return reinterpret_cast(baseClass); @@ -68,9 +68,9 @@ HWTEST_F_L0(CompositeBaseClassTest, CreateAndGetType) roots_->InitializeCompositeBaseClass(allocator); - auto* baseClass = roots_->GetBaseClass(CommonType::LINE_STRING); + auto* baseClass = roots_->GetBaseClass(ObjectType::LINE_STRING); ASSERT_NE(baseClass, nullptr); - EXPECT_EQ(baseClass->GetObjectType(), CommonType::LINE_STRING); + EXPECT_EQ(baseClass->GetObjectType(), ObjectType::LINE_STRING); } HWTEST_F_L0(CompositeBaseClassTest, GetBaseClassReturnsCorrectType) @@ -81,17 +81,17 @@ HWTEST_F_L0(CompositeBaseClassTest, GetBaseClassReturnsCorrectType) roots_->InitializeCompositeBaseClass(allocator); - auto* lineString = roots_->GetBaseClass(CommonType::LINE_STRING); - auto* slicedString = roots_->GetBaseClass(CommonType::SLICED_STRING); - auto* treeString = roots_->GetBaseClass(CommonType::TREE_STRING); + auto* lineString = roots_->GetBaseClass(ObjectType::LINE_STRING); + auto* slicedString = roots_->GetBaseClass(ObjectType::SLICED_STRING); + auto* treeString = roots_->GetBaseClass(ObjectType::TREE_STRING); ASSERT_NE(lineString, nullptr); ASSERT_NE(slicedString, nullptr); ASSERT_NE(treeString, nullptr); - EXPECT_EQ(lineString->GetObjectType(), CommonType::LINE_STRING); - EXPECT_EQ(slicedString->GetObjectType(), CommonType::SLICED_STRING); - EXPECT_EQ(treeString->GetObjectType(), CommonType::TREE_STRING); + EXPECT_EQ(lineString->GetObjectType(), ObjectType::LINE_STRING); + EXPECT_EQ(slicedString->GetObjectType(), ObjectType::SLICED_STRING); + EXPECT_EQ(treeString->GetObjectType(), ObjectType::TREE_STRING); } HWTEST_F_L0(CompositeBaseClassTest, IterateCompositeBaseClass) diff --git a/ecmascript/base/json_helper.cpp b/ecmascript/base/json_helper.cpp index 42e809eda3988d12751593ad013cb62161afca90..4722187827d16dcb5c05c6d9fc17f2658fbf9a5e 100644 --- a/ecmascript/base/json_helper.cpp +++ b/ecmascript/base/json_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,6 +15,7 @@ #include "ecmascript/base/json_helper.h" #include "common_components/base/utf_helper.h" +#include "libpandabase/utils/span.h" namespace panda::ecmascript::base { @@ -68,7 +69,7 @@ bool DoNotEscape(uint16_t c) (c >= 0x23 && c != 0x5C && (c < 0xD800 || c > 0xDFFF)); } -bool JsonHelper::IsFastValueToQuotedString(const Span &sp) +bool JsonHelper::IsFastValueToQuotedString(const common::Span &sp) { for (const auto utf8Ch : sp) { if (!DoNotEscape(utf8Ch)) { @@ -105,7 +106,8 @@ bool JsonHelper::IsFastValueToQuotedString(const CString& str) #endif #if ENABLE_NEXT_OPTIMIZATION -void JsonHelper::AppendQuotedValueToC16String(const Span &sp, uint32_t &index, C16String &output) +void JsonHelper::AppendQuotedValueToC16String(const common::Span& sp, uint32_t& index, + C16String& output) { auto ch = sp[index]; if (common::utf_helper::IsUTF16Surrogate(ch)) { @@ -128,7 +130,7 @@ void JsonHelper::AppendQuotedValueToC16String(const Span &sp, ui } template -void JsonHelper::AppendValueToQuotedString(const Span &sp, DstType &output) +void JsonHelper::AppendValueToQuotedString(const common::Span &sp, DstType &output) { static_assert(sizeof(typename DstType::value_type) >= sizeof(SrcType)); AppendString(output, "\""); @@ -154,11 +156,11 @@ void JsonHelper::AppendValueToQuotedString(const Span &sp, DstTyp AppendString(output, "\""); } template void JsonHelper::AppendValueToQuotedString( - const Span &sp, CString &output); + const common::Span &sp, CString &output); template void JsonHelper::AppendValueToQuotedString( - const Span &sp, C16String &output); + const common::Span &sp, C16String &output); template void JsonHelper::AppendValueToQuotedString( - const Span &sp, C16String &output); + const common::Span &sp, C16String &output); #else void JsonHelper::AppendValueToQuotedString(const CString& str, CString& output) diff --git a/ecmascript/base/json_helper.h b/ecmascript/base/json_helper.h index d860523de903e8d157efc5482087bad58c9c5882..188d22b434e14535864fe5588405a572c5bb9253 100644 --- a/ecmascript/base/json_helper.h +++ b/ecmascript/base/json_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,7 +19,7 @@ #include "ecmascript/js_handle.h" #include "ecmascript/mem/c_string.h" #include "ecmascript/property_attributes.h" -#include "libpandabase/utils/span.h" +#include "common_interfaces/objects/utils/span.h" namespace panda::ecmascript::base { constexpr int HEX_DIGIT_MASK = 0xF; @@ -84,7 +84,7 @@ public: } #if ENABLE_NEXT_OPTIMIZATION - static bool IsFastValueToQuotedString(const Span &sp); + static bool IsFastValueToQuotedString(const common::Span &sp); #else static bool IsFastValueToQuotedString(const CString &str); #endif @@ -93,9 +93,10 @@ public: // Control characters code units are replaced with escape sequences \uHHHH, or with the shorter forms, // \b (BACKSPACE), \f (FORM FEED), \n (LINE FEED), \r (CARRIAGE RETURN), \t (CHARACTER TABULATION). #if ENABLE_NEXT_OPTIMIZATION - static void AppendQuotedValueToC16String(const Span &sp, uint32_t &index, C16String &output); + static void AppendQuotedValueToC16String(const common::Span &sp, uint32_t &index, + C16String &output); template - static void AppendValueToQuotedString(const Span &sp, DstType &output); + static void AppendValueToQuotedString(const common::Span &sp, DstType &output); #else static void AppendValueToQuotedString(const CString& str, CString& output); #endif diff --git a/ecmascript/base/json_parser.cpp b/ecmascript/base/json_parser.cpp index 185105f423c98b3f1c8514ea3958891ad7f70302..6bde7f667e85ed946ec39b44ea1bcbc7e026f977 100644 --- a/ecmascript/base/json_parser.cpp +++ b/ecmascript/base/json_parser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #include "ecmascript/interpreter/interpreter.h" #include "ecmascript/base/json_parser.h" #include "ecmascript/linked_hash_table.h" +#include "ecmascript/ecma_string-inl.h" #include "ecmascript/ecma_string_table.h" #include "ecmascript/platform/json_platform_helper.h" diff --git a/ecmascript/base/number_helper.cpp b/ecmascript/base/number_helper.cpp index 2e3a331f18344e6981309c4cd4f8e91b07950518..2963e320f552594fb8b4f6b082f32fbc5aab4a45 100644 --- a/ecmascript/base/number_helper.cpp +++ b/ecmascript/base/number_helper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -571,7 +571,7 @@ JSTaggedValue NumberHelper::StringToNumber(JSThread *thread, EcmaString *string, } } CVector buf; - Span str = EcmaStringAccessor(string).ToUtf8Span(thread, buf); + common::Span str = EcmaStringAccessor(string).ToUtf8Span(thread, buf); JSTaggedValue result = NumberHelper::StringToDoubleWithRadix(str.begin(), str.end(), radix, &negative); if (result.GetNumber() == 0 && negative == true) { @@ -1171,7 +1171,7 @@ JSTaggedValue NumberHelper::StringToBigInt(JSThread *thread, JSHandle buf; - Span str = EcmaStringAccessor(strObj).ToUtf8Span(thread, buf); + common::Span str = EcmaStringAccessor(strObj).ToUtf8Span(thread, buf); auto p = const_cast(str.begin()); auto end = str.end(); diff --git a/ecmascript/base/string_helper.h b/ecmascript/base/string_helper.h index e029fe18b10cd5f5ad0a3abe456cc80476b32e0b..254330a5c23c76a688cfe8150f43c9e147750796 100644 --- a/ecmascript/base/string_helper.h +++ b/ecmascript/base/string_helper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,12 +24,13 @@ #include #include +#include "securec.h" #include "common_components/base/utf_helper.h" +#include "common_interfaces/objects/utils/span.h" #include "ecmascript/mem/c_containers.h" #include "ecmascript/mem/c_string.h" #include "libpandabase/utils/span.h" -#include "securec.h" #include "unicode/unistr.h" namespace panda::ecmascript::base { @@ -367,7 +368,7 @@ public: } template - static inline uint32_t GetStart(Span &data, uint32_t length) + static inline uint32_t GetStart(common::Span &data, uint32_t length) { uint32_t start = 0; while (start < length && IsNonspace(data[start])) { @@ -377,7 +378,7 @@ public: } template - static inline int32_t GetEnd(Span &data, int32_t start, uint32_t length) + static inline int32_t GetEnd(common::Span &data, int32_t start, uint32_t length) { if (length == 0U) { return 0; diff --git a/ecmascript/base/tests/json_helper_test.cpp b/ecmascript/base/tests/json_helper_test.cpp index 2364d6e62db475746a784ec939d6217295618541..e6ca461eca124cfdf8435139ce4467607ac83bcf 100644 --- a/ecmascript/base/tests/json_helper_test.cpp +++ b/ecmascript/base/tests/json_helper_test.cpp @@ -32,9 +32,9 @@ class JsonHelperTest : public BaseTestWithScope { */ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_001) { - Span sp1(reinterpret_cast("Hello World"), 11); - Span sp2(reinterpret_cast("0123456789"), 10); - Span sp3(reinterpret_cast("!#$%&'()*+,-./:;=?@[]^_`{|}~"), 28); + common::Span sp1(reinterpret_cast("Hello World"), 11); + common::Span sp2(reinterpret_cast("0123456789"), 10); + common::Span sp3(reinterpret_cast("!#$%&'()*+,-./:;=?@[]^_`{|}~"), 28); EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp1)); EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp2)); EXPECT_TRUE(JsonHelper::IsFastValueToQuotedString(sp3)); @@ -48,13 +48,13 @@ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_001) */ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_002) { - Span sp1(reinterpret_cast("\""), 1); - Span sp2(reinterpret_cast("\\"), 1); - Span sp3(reinterpret_cast("\b"), 1); - Span sp4(reinterpret_cast("\f"), 1); - Span sp5(reinterpret_cast("\n"), 1); - Span sp6(reinterpret_cast("\r"), 1); - Span sp7(reinterpret_cast("\t"), 1); + common::Span sp1(reinterpret_cast("\""), 1); + common::Span sp2(reinterpret_cast("\\"), 1); + common::Span sp3(reinterpret_cast("\b"), 1); + common::Span sp4(reinterpret_cast("\f"), 1); + common::Span sp5(reinterpret_cast("\n"), 1); + common::Span sp6(reinterpret_cast("\r"), 1); + common::Span sp7(reinterpret_cast("\t"), 1); EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp1)); EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp2)); EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp3)); @@ -66,7 +66,7 @@ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_002) for (uint8_t c = 0; c < 32; c++) { EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString( - Span(reinterpret_cast(&c), 1))); + common::Span(reinterpret_cast(&c), 1))); } } @@ -78,9 +78,9 @@ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_002) */ HWTEST_F_L0(JsonHelperTest, IsFastValueToQuotedString_003) { - Span sp1(reinterpret_cast("Hello\nWorld"), 11); - Span sp2(reinterpret_cast("Test\"Quote"), 10); - Span sp3(reinterpret_cast("Test\\BackSlash"), 14); + common::Span sp1(reinterpret_cast("Hello\nWorld"), 11); + common::Span sp2(reinterpret_cast("Test\"Quote"), 10); + common::Span sp3(reinterpret_cast("Test\\BackSlash"), 14); EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp1)); EXPECT_FALSE(JsonHelper::IsFastValueToQuotedString(sp2)); diff --git a/ecmascript/base/tests/number_helper_test.cpp b/ecmascript/base/tests/number_helper_test.cpp index 88d5c3747178dbe3c7063b6f6934aeed427b1753..b6d8bd210f0db58c1593f1bfa582ffd2e2f88f45 100644 --- a/ecmascript/base/tests/number_helper_test.cpp +++ b/ecmascript/base/tests/number_helper_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -572,7 +572,7 @@ HWTEST_F_L0(NumberHelperTest, StringToDoubleWithRadix) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); int radix; - Span sp; + common::Span sp; CVector buf; JSHandle resultStr; diff --git a/ecmascript/builtins/builtins_number.cpp b/ecmascript/builtins/builtins_number.cpp index 99f9b6845dc4a73d8f19cde8e60c7914436c190f..0540fe09e7bb4c84bc3bb91a9bf686bb4eeee095 100644 --- a/ecmascript/builtins/builtins_number.cpp +++ b/ecmascript/builtins/builtins_number.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -165,7 +165,7 @@ JSTaggedValue BuiltinsNumber::ParseFloat(EcmaRuntimeCallInfo *argv) // 2. ReturnIfAbrupt(inputString). RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); CVector buf; - Span str = EcmaStringAccessor(numberString).ToUtf8Span(thread, buf); + common::Span str = EcmaStringAccessor(numberString).ToUtf8Span(thread, buf); // 4. If neither trimmedString nor any prefix of trimmedString satisfies the syntax of a StrDecimalLiteral // (see 7.1.3.1), return NaN. if (NumberHelper::IsEmptyString(str.begin(), str.end())) { diff --git a/ecmascript/builtins/tests/builtins_number_test.cpp b/ecmascript/builtins/tests/builtins_number_test.cpp index 25ce0ce010d12c4e45d2bf43f2d0f3cd874803b0..340bf9e63edc452de61f2c4ff86c2de6cfeac5e4 100644 --- a/ecmascript/builtins/tests/builtins_number_test.cpp +++ b/ecmascript/builtins/tests/builtins_number_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -461,7 +461,7 @@ HWTEST_F_L0(BuiltinsNumberTest, parseInt) HWTEST_F_L0(BuiltinsNumberTest, StringToDoubleFlags) { JSHandle str; - Span sp; + common::Span sp; CVector buf; // flags of IGNORE_TRAILING @@ -543,7 +543,7 @@ HWTEST_F_L0(BuiltinsNumberTest, StringToDoubleFlags) HWTEST_F_L0(BuiltinsNumberTest, StringToDoubleRadix) { JSHandle str; - Span sp; + common::Span sp; CVector buf; int radix; diff --git a/ecmascript/compiler/builtins/builtins_string_stub_builder.cpp b/ecmascript/compiler/builtins/builtins_string_stub_builder.cpp index aa143b9696446e34f96949d7e198fea23b8b24db..911f2428b0aa665dadbd7a42c9b15d90bb8f7d4d 100644 --- a/ecmascript/compiler/builtins/builtins_string_stub_builder.cpp +++ b/ecmascript/compiler/builtins/builtins_string_stub_builder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -2257,8 +2257,7 @@ GateRef BuiltinsStringStubBuilder::StringAdd(GateRef glue, GateRef leftString, G builder_.Int32(SlicedString::MIN_SLICED_STRING_LENGTH)), &slowPath, &stringConcatOpt); builder_.Bind(&stringConcatOpt); { - GateRef backStoreLength = - builder_.Int32Mul(newLength, builder_.Int32(LineString::INIT_LENGTH_TIMES)); + GateRef backStoreLength = builder_.Int32Mul(newLength, builder_.Int32(INIT_LENGTH_TIMES)); GateRef leftIsUtf8 = builder_.IsUtf8String(left); GateRef rightIsUtf8 = builder_.IsUtf8String(right); GateRef canBeCompressed = builder_.BitAnd(leftIsUtf8, rightIsUtf8); diff --git a/ecmascript/compiler/builtins/builtins_string_stub_builder.h b/ecmascript/compiler/builtins/builtins_string_stub_builder.h index 5e4eae67416f57106b2419b2b04b088bc7773323..6f62f1cb6cbb3aa75565494262dcbd0a27a1806a 100644 --- a/ecmascript/compiler/builtins/builtins_string_stub_builder.h +++ b/ecmascript/compiler/builtins/builtins_string_stub_builder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -98,6 +98,7 @@ private: GateRef GetSingleCharCodeFromSlicedString(GateRef glue, GateRef str, GateRef index); void CheckParamsAndGetPosition(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* pos, Label *exit, Label *slowPath, Label *posIsValid); + static constexpr uint32_t INIT_LENGTH_TIMES = 4; }; class FlatStringStubBuilder : public StubBuilder { diff --git a/ecmascript/compiler/circuit_builder.cpp b/ecmascript/compiler/circuit_builder.cpp index e38f53c00c7368324f50d4b08d2520f0153f3210..d364e867a393c8e895479c1b3b3e6261df8b585c 100644 --- a/ecmascript/compiler/circuit_builder.cpp +++ b/ecmascript/compiler/circuit_builder.cpp @@ -702,8 +702,8 @@ GateRef CircuitBuilder::IsCompositeHClass(GateRef hClass) { GateRef objectType = GetObjectType(hClass); return BitAnd( - Int32LessThanOrEqual(objectType, Int32(static_cast(common::CommonType::LAST_OBJECT_TYPE))), - Int32GreaterThanOrEqual(objectType, Int32(static_cast(common::CommonType::FIRST_OBJECT_TYPE)))); + Int32LessThanOrEqual(objectType, Int32(static_cast(common::ObjectType::LAST_OBJECT_TYPE))), + Int32GreaterThanOrEqual(objectType, Int32(static_cast(common::ObjectType::FIRST_OBJECT_TYPE)))); } void CircuitBuilder::CheckHClassFieldInvalidAccess([[maybe_unused]]GateRef glue, [[maybe_unused]] GateRef hClass) diff --git a/ecmascript/compiler/mcr_circuit_builder.h b/ecmascript/compiler/mcr_circuit_builder.h index 8b6cf64f0aa4dffc6983ed2f79bdfd0466519cb4..e9f5d54a40e99e70da72c9f66b56e148985f3673 100644 --- a/ecmascript/compiler/mcr_circuit_builder.h +++ b/ecmascript/compiler/mcr_circuit_builder.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -820,13 +820,13 @@ GateRef CircuitBuilder::TreeStringIsFlat(GateRef glue, GateRef string) GateRef CircuitBuilder::GetFirstFromTreeString(GateRef glue, GateRef string) { - GateRef offset = IntPtr(TreeString::FIRST_OFFSET); + GateRef offset = IntPtr(TreeString::LEFT_OFFSET); return Load(VariableType::JS_POINTER(), glue, string, offset); } GateRef CircuitBuilder::GetSecondFromTreeString(GateRef glue, GateRef string) { - GateRef offset = IntPtr(TreeString::SECOND_OFFSET); + GateRef offset = IntPtr(TreeString::RIGHT_OFFSET); return Load(VariableType::JS_POINTER(), glue, string, offset); } diff --git a/ecmascript/compiler/new_object_stub_builder.cpp b/ecmascript/compiler/new_object_stub_builder.cpp index 3f7c55329ac37c95a31018acbbe2773490618406..7254c8887fe2a3f844b73dc0c5c55413e252c3a8 100644 --- a/ecmascript/compiler/new_object_stub_builder.cpp +++ b/ecmascript/compiler/new_object_stub_builder.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -2012,8 +2012,8 @@ void NewObjectStubBuilder::AllocTreeStringObject(Variable *result, Label *exit, StoreHClass(glue_, result->ReadVariable(), stringClass); InitStringLengthAndFlags(glue_, result->ReadVariable(), length, compressed); SetMixHashcode(glue_, result->ReadVariable(), Int32(0)); - Store(VariableType::JS_POINTER(), glue_, result->ReadVariable(), IntPtr(TreeString::FIRST_OFFSET), first); - Store(VariableType::JS_POINTER(), glue_, result->ReadVariable(), IntPtr(TreeString::SECOND_OFFSET), second); + Store(VariableType::JS_POINTER(), glue_, result->ReadVariable(), IntPtr(TreeString::LEFT_OFFSET), first); + Store(VariableType::JS_POINTER(), glue_, result->ReadVariable(), IntPtr(TreeString::RIGHT_OFFSET), second); Jump(exit); } diff --git a/ecmascript/dfx/hprof/tests/heap_snapshot_test.cpp b/ecmascript/dfx/hprof/tests/heap_snapshot_test.cpp index 92212c42f5d206a9fcf98e69ffd8c89e4a691473..a91d4c5ef2f888cb6df1a15eaa1673ee4fd9ffcf 100644 --- a/ecmascript/dfx/hprof/tests/heap_snapshot_test.cpp +++ b/ecmascript/dfx/hprof/tests/heap_snapshot_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -132,10 +132,17 @@ HWTEST_F_L0(HeapSnapShotTest, TestGenerateStringNode) totalSize += node->GetSelfSize(); } } +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + // lineString: 32 + // treeString: 40 + // totalSize: 8 * 32 + 7 * 40 + ASSERT_EQ(totalSize, 536); +#else // lineString: 24 // treeString: 32 // totalSize: 8 * 24 + 7 * 32 ASSERT_EQ(totalSize, 416); +#endif } HWTEST_F_L0(HeapSnapShotTest, TestGeneratePrivateStringNode) @@ -146,8 +153,13 @@ HWTEST_F_L0(HeapSnapShotTest, TestGeneratePrivateStringNode) HeapSnapShotFriendTest heapSnapShotTest(ecmaVm_, tester.GetEcmaStringTableTest(), dumpOption, false, tester.GetEntryIdMapTest()); Node *node = heapSnapShotTest.GeneratePrivateStringNodeTest(0); +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + // lineString: 32 + ASSERT_EQ(node->GetSelfSize(), 32); +#else // lineString: 24 ASSERT_EQ(node->GetSelfSize(), 24); +#endif } HWTEST_F_L0(HeapSnapShotTest, TestMoveNode) @@ -170,7 +182,12 @@ HWTEST_F_L0(HeapSnapShotTest, TestMoveNode) heapSnapShotTest.MoveNodeTest(address, reinterpret_cast(treeString), 0); HeapEntryMap &heapEntryMap = heapSnapShotTest.GetEntryMapTest(); Node *movedNode = heapEntryMap.FindEntry(reinterpret_cast(treeString)); +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + // treeString: 40 + ASSERT_EQ(movedNode->GetSelfSize(), 40); +#else // treeString: 32 ASSERT_EQ(movedNode->GetSelfSize(), 32); +#endif } } \ No newline at end of file diff --git a/ecmascript/dfx/hprof/tests/js_metadata_test.cpp b/ecmascript/dfx/hprof/tests/js_metadata_test.cpp index b5955cbbc697f38c9f702ca18c504ed452ab73c4..75a5cb7161d45a22951e48ec51fee79bc0ea22ac 100644 --- a/ecmascript/dfx/hprof/tests/js_metadata_test.cpp +++ b/ecmascript/dfx/hprof/tests/js_metadata_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2025 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1031,8 +1031,8 @@ public: TransWithProtoHandler::TRANSITION_HCLASS_OFFSET, TransWithProtoHandler::PROTO_CELL_OFFSET, TransWithProtoHandler::SIZE - TransWithProtoHandler::HANDLER_INFO_OFFSET}}, - {JSType::TREE_STRING, {TreeString::FIRST_OFFSET, TreeString::SECOND_OFFSET, - TreeString::SIZE - TreeString::FIRST_OFFSET}}, + {JSType::TREE_STRING, {TreeString::LEFT_OFFSET, TreeString::RIGHT_OFFSET, + TreeString::SIZE - TreeString::LEFT_OFFSET}}, {JSType::VTABLE, {TaggedArray::LAST_OFFSET - TaggedArray::LENGTH_OFFSET}} }; // { typeName: [all parents of this type]} @@ -1766,8 +1766,8 @@ public: TransWithProtoHandler::TRANSITION_HCLASS_OFFSET - TransWithProtoHandler::HANDLER_INFO_OFFSET, TransWithProtoHandler::PROTO_CELL_OFFSET - TransWithProtoHandler::TRANSITION_HCLASS_OFFSET, TransWithProtoHandler::SIZE - TransWithProtoHandler::PROTO_CELL_OFFSET}}, - {JSType::TREE_STRING, {TreeString::SECOND_OFFSET - TreeString::FIRST_OFFSET, - TreeString::SIZE - TreeString::SECOND_OFFSET}}, + {JSType::TREE_STRING, {TreeString::RIGHT_OFFSET - TreeString::LEFT_OFFSET, + TreeString::SIZE - TreeString::RIGHT_OFFSET}}, {JSType::VTABLE, {}} }; } diff --git a/ecmascript/ecma_string-inl.h b/ecmascript/ecma_string-inl.h index df4380f88f33d8e8fbacfe1cb3c5b27ac81f3910..68dd1e992d81bc8a709bfd35ea60ea84e4c41e79 100644 --- a/ecmascript/ecma_string-inl.h +++ b/ecmascript/ecma_string-inl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #ifndef ECMASCRIPT_STRING_INL_H #define ECMASCRIPT_STRING_INL_H +#include "common_interfaces/objects/string/base_string-inl.h" #include "ecmascript/ecma_string.h" #include "ecmascript/base/string_helper.h" #include "ecmascript/ecma_vm.h" @@ -24,7 +25,6 @@ #include "ecmascript/mem/space.h" #include "ecmascript/object_factory-inl.h" #include "ecmascript/debugger/js_debugger_manager.h" -#include "common_interfaces/objects/string/base_string-inl2.h" namespace panda::ecmascript { /* static */ @@ -44,11 +44,11 @@ inline EcmaString *EcmaString::CreateFromUtf8(const EcmaVM *vm, const uint8_t *u if (utf8Len == 0) { return vm->GetFactory()->GetEmptyString().GetObject(); } - auto allocator = [vm, type](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm, type](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); return EcmaString::AllocLineString(vm, size, type)->ToBaseString(); }; - BaseString *str = BaseString::CreateFromUtf8(std::move(allocator), utf8Data, utf8Len, canBeCompress); + BaseString *str = LineString::CreateFromUtf8(std::move(allocator), utf8Data, utf8Len, canBeCompress); return EcmaString::FromBaseString(str); } #else @@ -86,11 +86,11 @@ inline EcmaString *EcmaString::CreateFromUtf8CompressedSubString(const EcmaVM *v return vm->GetFactory()->GetEmptyString().GetObject(); } - auto allocator = [vm, type](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm, type](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); return EcmaString::AllocLineString(vm, size, type)->ToBaseString(); }; - BaseString *str = BaseString::CreateFromUtf8CompressedSubString(std::move(allocator), string, offset, utf8Len); + BaseString *str = LineString::CreateFromUtf8CompressedSubString(std::move(allocator), string, offset, utf8Len); return EcmaString::FromBaseString(str); } @@ -124,36 +124,36 @@ inline EcmaString *EcmaString::CreateFromUtf16(const EcmaVM *vm, const uint16_t return vm->GetFactory()->GetEmptyString().GetObject(); } - auto allocator = [vm, type](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm, type](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); return EcmaString::AllocLineString(vm, size, type)->ToBaseString(); }; - BaseString *str = BaseString::CreateFromUtf16(std::move(allocator), utf16Data, utf16Len, canBeCompress); + BaseString *str = LineString::CreateFromUtf16(std::move(allocator), utf16Data, utf16Len, canBeCompress); return EcmaString::FromBaseString(str); } /* static */ inline EcmaString *EcmaString::CreateLineString(const EcmaVM *vm, size_t length, bool compressed) { - auto allocator = [vm](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); EcmaString* string = vm->GetFactory()->AllocLineStringObject(size); return string; }; - BaseString *str = BaseString::CreateLineString(std::move(allocator), length, compressed); + BaseString *str = LineString::Create(std::move(allocator), length, compressed); return EcmaString::FromBaseString(str); } /* static */ inline EcmaString *EcmaString::CreateLineStringNoGC(const EcmaVM *vm, size_t length, bool compressed) { - auto allocator = [vm](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); size = AlignUp(size, static_cast(MemAlignment::MEM_ALIGN_OBJECT)); EcmaString* string = vm->GetFactory()->AllocLineStringObjectNoGC(size); return string; }; - BaseString *str = BaseString::CreateLineString(std::move(allocator), length, compressed); + BaseString *str = LineString::Create(std::move(allocator), length, compressed); return EcmaString::FromBaseString(str); } @@ -181,27 +181,27 @@ inline EcmaString* EcmaString::AllocLineString(const EcmaVM* vm, size_t size, Me inline EcmaString *EcmaString::CreateLineStringWithSpaceType(const EcmaVM *vm, size_t length, bool compressed, MemSpaceType type) { - auto allocator = [vm, type](size_t size, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::LINE_STRING && "Can only allocate line string"); + auto allocator = [vm, type](size_t size, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::LINE_STRING && "Can only allocate line string"); ASSERT(IsSMemSpace(type)); return AllocLineString(vm, size, type); }; - BaseString *str = BaseString::CreateLineString(std::move(allocator), length, compressed); + BaseString *str = LineString::Create(std::move(allocator), length, compressed); return EcmaString::FromBaseString(str); } inline SlicedEcmaString* EcmaString::CreateSlicedString(const EcmaVM* vm, JSHandle parent, MemSpaceType type) { - auto allocator = [vm, type](size_t, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::SLICED_STRING && "Can only allocate sliced string"); + auto allocator = [vm, type](size_t, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::SLICED_STRING && "Can only allocate sliced string"); EcmaString* string = vm->GetFactory()->AllocSlicedStringObject(type); return string; }; auto writeBarrier = [vm](void* obj, size_t offset, BaseObject* str) { Barriers::SetObject(vm->GetJSThread(), obj, offset, reinterpret_cast(str)); }; - SlicedString* slicedString = BaseString::CreateSlicedString(std::move(allocator), std::move(writeBarrier), parent); + SlicedString* slicedString = SlicedString::Create(std::move(allocator), std::move(writeBarrier), parent); return SlicedEcmaString::FromBaseString(slicedString); } @@ -232,16 +232,16 @@ inline EcmaString *EcmaString::CreateTreeString(const EcmaVM *vm, JSThread *thread = nullptr; GetDebuggerThread(vm, &thread); - auto allocator = [vm](size_t, common::CommonType stringType) -> BaseObject* { - ASSERT(stringType == common::CommonType::TREE_STRING && "Can only allocate tree string"); + auto allocator = [vm](size_t, common::ObjectType stringType) -> BaseObject* { + ASSERT(stringType == common::ObjectType::TREE_STRING && "Can only allocate tree string"); EcmaString* string = vm->GetFactory()->AllocTreeStringObject(); return string; }; auto writeBarrier = [thread](void* obj, size_t offset, BaseObject* str) { Barriers::SetObject(thread, obj, offset, reinterpret_cast(str)); }; - TreeString* treeString = BaseString::CreateTreeString(std::move(allocator), std::move(writeBarrier), left, right, - length, compressed); + TreeString* treeString = TreeString::Create(std::move(allocator), std::move(writeBarrier), left, right, + length, compressed); return TreeEcmaString::FromBaseString(treeString); } @@ -252,8 +252,8 @@ EcmaString *EcmaString::FastSubUtf8String(const EcmaVM *vm, const JSHandle string(vm->GetJSThread(), CreateLineString(vm, length, true)); // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) FlatStringInfo srcFlat = FlattenAllString(vm, src); - Span dst(string->GetDataUtf8Writable(), length); - Span source(srcFlat.GetDataUtf8() + start, length); + common::Span dst(string->GetDataUtf8Writable(), length); + common::Span source(srcFlat.GetDataUtf8() + start, length); EcmaString::MemCopyChars(dst, length, source, length); ASSERT_PRINT(CanBeCompressed(*string), "canBeCompresse does not match the real value!"); @@ -275,8 +275,8 @@ EcmaString *EcmaString::FastSubUtf16String(const EcmaVM *vm, const JSHandle dst(string->GetDataUtf16Writable(), length); - Span source(srcFlat.GetDataUtf16() + start, length); + common::Span dst(string->GetDataUtf16Writable(), length); + common::Span source(srcFlat.GetDataUtf16() + start, length); EcmaString::MemCopyChars(dst, len, source, len); } ASSERT_PRINT(canBeCompressed == CanBeCompressed(*string), "canBeCompresse does not match the real value!"); @@ -285,23 +285,23 @@ EcmaString *EcmaString::FastSubUtf16String(const EcmaVM *vm, const JSHandleGetDataUtf8(); + return LineString::ConstCast(this)->GetDataUtf8(); } inline const uint16_t* EcmaString::GetDataUtf16() const { - return ToBaseString()->GetDataUtf16(); + return LineString::ConstCast(this)->GetDataUtf16(); } // require is LineString inline uint8_t* EcmaString::GetDataUtf8Writable() { - return ToBaseString()->GetDataUtf8Writable(); + return LineString::Cast(this)->GetDataUtf8Writable(); } inline uint16_t* EcmaString::GetDataUtf16Writable() { - return ToBaseString()->GetDataUtf16Writable(); + return LineString::Cast(this)->GetDataUtf16Writable(); } inline size_t EcmaString::GetUtf8Length(const JSThread *thread, bool modify, bool isGetBufferSize) const @@ -321,17 +321,23 @@ uint16_t EcmaString::At(const JSThread *thread, int32_t index) const return ToBaseString()->At(std::move(readBarrier), index); } +template +bool EcmaString::StringsAreEquals(common::Span& str1, common::Span& str2) +{ + return BaseString::StringsAreEquals(str1, str2); +} + inline void EcmaString::WriteData(uint32_t index, uint16_t src) { - return ToBaseString()->WriteData(index, src); + return LineString::Cast(this)->Set(index, src); } -inline Span EcmaString::FastToUtf8Span() const +inline common::Span EcmaString::FastToUtf8Span() const { uint32_t len = GetLength(); ASSERT(IsUtf8()); const uint8_t *data = GetDataUtf8(); - return Span(data, len); + return common::Span(data, len); } inline bool EcmaString::IsFlat(const JSThread *thread) const @@ -395,8 +401,8 @@ inline uint32_t EcmaString::CopyDataUtf16(const JSThread *thread, uint16_t *buf, return ToBaseString()->CopyDataUtf16(std::move(readBarrier), buf, maxLength); } -inline Span EcmaString::ToUtf8Span(const JSThread *thread, CVector &buf, bool modify, - bool cesu8) +inline common::Span EcmaString::ToUtf8Span(const JSThread *thread, CVector &buf, bool modify, + bool cesu8) { auto readBarrier = [thread](const void *obj, size_t offset) -> TaggedObject * { return Barriers::GetTaggedObject(thread, obj, offset); @@ -404,7 +410,8 @@ inline Span EcmaString::ToUtf8Span(const JSThread *thread, CVecto return ToBaseString()->ToUtf8Span(std::move(readBarrier), buf, modify, cesu8); } -inline Span EcmaString::DebuggerToUtf8Span(const JSThread *thread, CVector &buf, bool modify) +inline common::Span EcmaString::DebuggerToUtf8Span(const JSThread *thread, CVector &buf, + bool modify) { auto readBarrier = [thread](const void *obj, size_t offset) -> TaggedObject * { return Barriers::GetTaggedObject(thread, obj, offset); @@ -454,9 +461,14 @@ inline void EcmaStringAccessor::ReadData(const JSThread *thread, EcmaString *dst dst->WriteData(thread, src, start, destSize, length); } -inline Span EcmaStringAccessor::FastToUtf8Span() const +inline common::Span EcmaStringAccessor::FastToUtf8Span() const { return string_->FastToUtf8Span(); } + +inline bool EcmaStringAccessor::IsASCIICharacter(uint16_t data) +{ + return BaseString::IsASCIICharacter(data); +} } // namespace panda::ecmascript #endif diff --git a/ecmascript/ecma_string.cpp b/ecmascript/ecma_string.cpp index a4d6843695199b735a75dbceef146adeead683fe..43bf3cf22ca3ee779a9c0f27f99c60e592986825 100755 --- a/ecmascript/ecma_string.cpp +++ b/ecmascript/ecma_string.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,7 +15,7 @@ #include "ecmascript/ecma_string-inl.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string-inl.h" #include "ecmascript/base/json_helper.h" namespace panda::ecmascript { @@ -65,20 +65,20 @@ EcmaString *EcmaString::Concat(const EcmaVM *vm, strRight = *right; if (compressed) { // copy left part - Span sp(newString->GetDataUtf8Writable(), newLength); - Span srcLeft(strLeft->GetDataUtf8(), leftLength); + common::Span sp(newString->GetDataUtf8Writable(), newLength); + common::Span srcLeft(strLeft->GetDataUtf8(), leftLength); EcmaString::MemCopyChars(sp, newLength, srcLeft, leftLength); // copy right part sp = sp.SubSpan(leftLength); - Span srcRight(strRight->GetDataUtf8(), rightLength); + common::Span srcRight(strRight->GetDataUtf8(), rightLength); EcmaString::MemCopyChars(sp, rightLength, srcRight, rightLength); } else { // copy left part - Span sp(newString->GetDataUtf16Writable(), newLength); + common::Span sp(newString->GetDataUtf16Writable(), newLength); if (strLeft->IsUtf8()) { BaseString::CopyChars(sp.data(), strLeft->GetDataUtf8(), leftLength); } else { - Span srcLeft(strLeft->GetDataUtf16(), leftLength); + common::Span srcLeft(strLeft->GetDataUtf16(), leftLength); EcmaString::MemCopyChars(sp, newLength << 1U, srcLeft, leftLength << 1U); } // copy right part @@ -86,7 +86,7 @@ EcmaString *EcmaString::Concat(const EcmaVM *vm, if (strRight->IsUtf8()) { BaseString::CopyChars(sp.data(), strRight->GetDataUtf8(), rightLength); } else { - Span srcRight(strRight->GetDataUtf16(), rightLength); + common::Span srcRight(strRight->GetDataUtf16(), rightLength); EcmaString::MemCopyChars(sp, rightLength << 1U, srcRight, rightLength << 1U); } } @@ -105,16 +105,16 @@ EcmaString *EcmaString::CopyStringToOldSpace(const EcmaVM *vm, const JSHandle sp(newString->GetDataUtf8Writable(), length); - Span srcSp(strOrigin.GetDataUtf8(), length); + common::Span sp(newString->GetDataUtf8Writable(), length); + common::Span srcSp(strOrigin.GetDataUtf8(), length); EcmaString::MemCopyChars(sp, length, srcSp, length); } else { // copy left part - Span sp(newString->GetDataUtf16Writable(), length); + common::Span sp(newString->GetDataUtf16Writable(), length); if (strOrigin.IsUtf8()) { BaseString::CopyChars(sp.data(), strOrigin.GetDataUtf8(), length); } else { - Span srcSp(strOrigin.GetDataUtf16(), length); + common::Span srcSp(strOrigin.GetDataUtf16(), length); EcmaString::MemCopyChars(sp, length << 1U, srcSp, length << 1U); } } @@ -212,30 +212,30 @@ int32_t EcmaString::Compare(const EcmaVM *vm, const JSHandle &left, int32_t countDiff = lhsCount - rhsCount; int32_t minCount = (countDiff < 0) ? lhsCount : rhsCount; if (!lhs.IsUtf16() && !rhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); - int32_t charDiff = common::CompareStringSpan(lhsSp, rhsSp, minCount); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); + int32_t charDiff = CompareStringSpan(lhsSp, rhsSp, minCount); if (charDiff != 0) { return charDiff; } } else if (!lhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); - int32_t charDiff = common::CompareStringSpan(lhsSp, rhsSp, minCount); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); + int32_t charDiff = CompareStringSpan(lhsSp, rhsSp, minCount); if (charDiff != 0) { return charDiff; } } else if (!rhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf16(), rhsCount); - Span rhsSp(rhs.GetDataUtf8(), lhsCount); - int32_t charDiff = common::CompareStringSpan(lhsSp, rhsSp, minCount); + common::Span lhsSp(lhs.GetDataUtf16(), rhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), lhsCount); + int32_t charDiff = CompareStringSpan(lhsSp, rhsSp, minCount); if (charDiff != 0) { return charDiff; } } else { - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); - int32_t charDiff = common::CompareStringSpan(lhsSp, rhsSp, minCount); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); + int32_t charDiff = CompareStringSpan(lhsSp, rhsSp, minCount); if (charDiff != 0) { return charDiff; } @@ -259,34 +259,34 @@ bool EcmaString::IsSubStringAt(const EcmaVM *vm, const JSHandle& lef int32_t lhsCount = static_cast(lhs.GetLength()); int32_t rhsCount = static_cast(rhs.GetLength()); if (!lhs.IsUtf16() && !rhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); - return common::IsSubStringAtSpan(lhsSp, rhsSp, offset); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); + return IsSubStringAtSpan(lhsSp, rhsSp, offset); } else if (!lhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); - return common::IsSubStringAtSpan(lhsSp, rhsSp, offset); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); + return IsSubStringAtSpan(lhsSp, rhsSp, offset); } else if (!rhs.IsUtf16()) { - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); - return common::IsSubStringAtSpan(lhsSp, rhsSp, offset); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); + return IsSubStringAtSpan(lhsSp, rhsSp, offset); } else { - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); - return common::IsSubStringAtSpan(lhsSp, rhsSp, offset); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); + return IsSubStringAtSpan(lhsSp, rhsSp, offset); } return false; } /* static */ -template -int32_t EcmaString::IndexOf(Span &lhsSp, Span &rhsSp, int32_t pos, int32_t max) +template +int32_t EcmaString::IndexOf(common::Span& lhsSp, common::Span& rhsSp, int32_t pos, int32_t max) { - return BaseString::IndexOf(lhsSp, rhsSp, pos, max); + return BaseString::IndexOf(lhsSp, rhsSp, pos, max); } -template -int32_t EcmaString::LastIndexOf(Span &lhsSp, Span &rhsSp, int32_t pos) +template +int32_t EcmaString::LastIndexOf(common::Span& lhsSp, common::Span& rhsSp, int32_t pos) { return BaseString::LastIndexOf(lhsSp, rhsSp, pos); } @@ -329,18 +329,18 @@ int32_t EcmaString::IndexOf(const EcmaVM *vm, lhs.SetString(*string); if (rhs.IsUtf8() && lhs.IsUtf8()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); return EcmaString::IndexOf(lhsSp, rhsSp, pos, max); } else if (rhs.IsUtf16() && lhs.IsUtf16()) { // NOLINT(readability-else-after-return) - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); return EcmaString::IndexOf(lhsSp, rhsSp, pos, max); } else if (rhs.IsUtf16()) { return -1; } else { // NOLINT(readability-else-after-return) - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); return EcmaString::IndexOf(lhsSp, rhsSp, pos, max); } } @@ -381,18 +381,18 @@ int32_t EcmaString::LastIndexOf(const EcmaVM *vm, FlatStringInfo rhs = FlattenAllString(vm, search); lhs.SetString(*string); if (rhs.IsUtf8() && lhs.IsUtf8()) { - Span lhsSp(lhs.GetDataUtf8(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf8(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); return EcmaString::LastIndexOf(lhsSp, rhsSp, pos); } else if (rhs.IsUtf16() && lhs.IsUtf16()) { // NOLINT(readability-else-after-return) - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf16(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf16(), rhsCount); return EcmaString::LastIndexOf(lhsSp, rhsSp, pos); } else if (rhs.IsUtf16()) { return -1; } else { // NOLINT(readability-else-after-return) - Span lhsSp(lhs.GetDataUtf16(), lhsCount); - Span rhsSp(rhs.GetDataUtf8(), rhsCount); + common::Span lhsSp(lhs.GetDataUtf16(), lhsCount); + common::Span rhsSp(rhs.GetDataUtf8(), rhsCount); return EcmaString::LastIndexOf(lhsSp, rhsSp, pos); } } @@ -464,14 +464,14 @@ uint32_t EcmaString::CalculateConcatHashCode(const JSThread *thread, const JSHan bool EcmaString::HashIntegerString(uint32_t length, uint32_t *hash, const uint32_t hashSeed) const { ASSERT(length >= 0); - Span str = FastToUtf8Span(); + common::Span str = FastToUtf8Span(); return BaseString::HashIntegerString(str.data(), length, hash, hashSeed); } // static bool EcmaString::CanBeCompressed(const EcmaString *string) { - return BaseString::CanBeCompressed(string->ToBaseString()); + return LineString::CanBeCompressed(LineString::ConstCast(string)); } // static @@ -510,20 +510,20 @@ bool EcmaString::StringsAreEqualDiffUtfEncoding(const FlatStringInfo &left, cons int32_t lhsCount = static_cast(left.GetLength()); int32_t rhsCount = static_cast(right.GetLength()); if (!left.IsUtf16() && !right.IsUtf16()) { - Span lhsSp(left.GetDataUtf8(), lhsCount); - Span rhsSp(right.GetDataUtf8(), rhsCount); + common::Span lhsSp(left.GetDataUtf8(), lhsCount); + common::Span rhsSp(right.GetDataUtf8(), rhsCount); return EcmaString::StringsAreEquals(lhsSp, rhsSp); } else if (!left.IsUtf16()) { - Span lhsSp(left.GetDataUtf8(), lhsCount); - Span rhsSp(right.GetDataUtf16(), rhsCount); + common::Span lhsSp(left.GetDataUtf8(), lhsCount); + common::Span rhsSp(right.GetDataUtf16(), rhsCount); return EcmaString::StringsAreEquals(lhsSp, rhsSp); } else if (!right.IsUtf16()) { - Span lhsSp(left.GetDataUtf16(), rhsCount); - Span rhsSp(right.GetDataUtf8(), lhsCount); + common::Span lhsSp(left.GetDataUtf16(), rhsCount); + common::Span rhsSp(right.GetDataUtf8(), lhsCount); return EcmaString::StringsAreEquals(lhsSp, rhsSp); } else { - Span lhsSp(left.GetDataUtf16(), lhsCount); - Span rhsSp(right.GetDataUtf16(), rhsCount); + common::Span lhsSp(left.GetDataUtf16(), lhsCount); + common::Span rhsSp(right.GetDataUtf16(), rhsCount); return EcmaString::StringsAreEquals(lhsSp, rhsSp); } } @@ -580,7 +580,7 @@ bool EcmaString::StringsAreEqualUtf16(const JSThread *thread, const EcmaString * } template -bool EcmaString::MemCopyChars(Span &dst, size_t dstMax, Span &src, size_t count) +bool EcmaString::MemCopyChars(common::Span &dst, size_t dstMax, common::Span &src, size_t count) { ASSERT(dstMax >= count); ASSERT(dst.Size() >= src.Size()); @@ -717,8 +717,9 @@ bool EcmaString::ToTypedArrayIndex(const JSThread *thread, uint32_t *index) return false; } -template -EcmaString *EcmaString::TrimBody(const JSThread *thread, const JSHandle &src, Span &data, TrimMode mode) +template +EcmaString* EcmaString::TrimBody(const JSThread* thread, const JSHandle& src, common::Span& data, + TrimMode mode) { uint32_t srcLen = src->GetLength(); int32_t start = 0; @@ -730,7 +731,7 @@ EcmaString *EcmaString::TrimBody(const JSThread *thread, const JSHandleGetEcmaVM(), src, start, static_cast(end - start + 1)); + EcmaString* res = FastSubString(thread->GetEcmaVM(), src, start, static_cast(end - start + 1)); return res; } @@ -757,7 +758,7 @@ EcmaString *EcmaString::TryToLower(const EcmaVM *vm, const JSHandle const char start = 'A'; const char end = 'Z'; uint32_t upperIndex = srcLength; - Span data(srcFlat.GetDataUtf8Writable(), srcLength); + common::Span data(srcFlat.GetDataUtf8Writable(), srcLength); for (uint32_t index = 0; index < srcLength; ++index) { if (base::StringHelper::Utf8CharInRange(data[index], start, end)) { upperIndex = index; @@ -778,7 +779,7 @@ EcmaString *EcmaString::TryToUpper(const EcmaVM *vm, const JSHandle const char start = 'a'; const char end = 'z'; uint32_t lowerIndex = srcLength; - Span data(srcFlat.GetDataUtf8Writable(), srcLength); + common::Span data(srcFlat.GetDataUtf8Writable(), srcLength); for (uint32_t index = 0; index < srcLength; ++index) { if (base::StringHelper::Utf8CharInRange(data[index], start, end)) { lowerIndex = index; @@ -800,7 +801,7 @@ EcmaString *EcmaString::ConvertUtf8ToLowerOrUpper(const EcmaVM *vm, const JSHand uint32_t srcLength = src->GetLength(); JSHandle newString(vm->GetJSThread(), CreateLineString(vm, srcLength, true)); auto srcFlat = FlattenAllString(vm, src); - Span data(srcFlat.GetDataUtf8Writable(), srcLength); + common::Span data(srcFlat.GetDataUtf8Writable(), srcLength); auto newStringPtr = newString->GetDataUtf8Writable(); if (startIndex > 0) { if (memcpy_s(newStringPtr, startIndex * sizeof(uint8_t), data.data(), startIndex * sizeof(uint8_t)) != EOK) { @@ -861,10 +862,10 @@ EcmaString *EcmaString::Trim(const JSThread *thread, const JSHandle return EcmaString::Cast(thread->GlobalConstants()->GetEmptyString().GetTaggedObject()); } if (srcFlat.IsUtf8()) { - Span data(srcFlat.GetDataUtf8(), srcLen); + common::Span data(srcFlat.GetDataUtf8(), srcLen); return TrimBody(thread, src, data, mode); } else { - Span data(srcFlat.GetDataUtf16(), srcLen); + common::Span data(srcFlat.GetDataUtf16(), srcLen); return TrimBody(thread, src, data, mode); } } @@ -1041,7 +1042,7 @@ std::string EcmaStringAccessor::ToStdString(const JSThread *thread, StringConver } bool modify = (usage != StringConvertedUsage::PRINT); CVector buf; - Span sp = string_->ToUtf8Span(thread, buf, modify); + common::Span sp = string_->ToUtf8Span(thread, buf, modify); #if ENABLE_NEXT_OPTIMIZATION return std::string(reinterpret_cast(sp.data()), sp.size()); #else @@ -1080,7 +1081,7 @@ std::string EcmaStringAccessor::DebuggerToStdString(const JSThread *thread, Stri bool modify = (usage != StringConvertedUsage::PRINT); CVector buf; - Span sp = string_->DebuggerToUtf8Span(thread, buf, modify); + common::Span sp = string_->DebuggerToUtf8Span(thread, buf, modify); #if ENABLE_NEXT_OPTIMIZATION return std::string(reinterpret_cast(sp.data()), sp.size()); #else @@ -1100,7 +1101,7 @@ CString EcmaStringAccessor::ToCString(const JSThread *thread, StringConvertedUsa } bool modify = (usage != StringConvertedUsage::PRINT); CVector buf; - Span sp = string_->ToUtf8Span(thread, buf, modify, cesu8); + common::Span sp = string_->ToUtf8Span(thread, buf, modify, cesu8); #if ENABLE_NEXT_OPTIMIZATION return CString(reinterpret_cast(sp.data()), sp.size()); #else @@ -1135,13 +1136,13 @@ void EcmaStringAccessor::AppendToC16String(const JSThread *thread, C16String &st // In real world, space is usually utf8. if LIKELY(string_->IsUtf8()) { CVector buf; - const uint8_t *data = EcmaString::GetUtf8DataFlat(thread, string_, buf); + const uint8_t* data = EcmaString::GetUtf8DataFlat(thread, string_, buf); // only ascii codes, no need to convert to UTF-16, just append. AppendString(str, reinterpret_cast(data), GetLength()); } else { CVector buf; - const uint16_t *data = EcmaString::GetUtf16DataFlat(thread, string_, buf); - str.append(reinterpret_cast(data), GetLength()); + const uint16_t* data = EcmaString::GetUtf16DataFlat(thread, string_, buf); + str.append(reinterpret_cast(data), GetLength()); } } #endif diff --git a/ecmascript/ecma_string.h b/ecmascript/ecma_string.h index c57e4c8550b5093dbbd21f0b4d6d68f1bdb5c66e..4d6aad66e7b4f6358e570c19f0345d57e1b39fde 100755 --- a/ecmascript/ecma_string.h +++ b/ecmascript/ecma_string.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -21,11 +21,11 @@ #include #include "common_components/base/utf_helper.h" -#include "common_interfaces/objects/base_string.h" +#include "common_interfaces/objects/string/base_string.h" +#include "common_interfaces/objects/string/base_string-inl.h" #include "common_interfaces/objects/string/line_string.h" #include "common_interfaces/objects/string/sliced_string.h" #include "common_interfaces/objects/string/tree_string.h" -#include "common_interfaces/objects/string/base_string-inl1.h" #include "ecmascript/common.h" #include "ecmascript/ecma_macros.h" #include "ecmascript/js_hclass.h" @@ -178,7 +178,7 @@ private: inline const uint8_t *GetDataUtf8() const; inline const uint16_t *GetDataUtf16() const; - inline Span FastToUtf8Span() const; + inline common::Span FastToUtf8Span() const; // require is LineString inline uint8_t *GetDataUtf8Writable(); @@ -254,10 +254,7 @@ private: // Check that two spans are equal. Should have the same length. /* static */ template - static bool StringsAreEquals(Span &str1, Span &str2) - { - return BaseString::StringsAreEquals(str1, str2); - } + static bool StringsAreEquals(common::Span &str1, common::Span &str2); // Compares string1 + string2 by bytes, It doesn't check canonical unicode equivalence. bool EqualToSplicedString(const JSThread *thread, const EcmaString *str1, const EcmaString *str2); @@ -309,10 +306,10 @@ private: std::u16string ToU16String(const JSThread *thread, uint32_t len = 0); - Span ToUtf8Span(const JSThread *thread, CVector &buf, bool modify = true, - bool cesu8 = false); + common::Span ToUtf8Span(const JSThread *thread, CVector &buf, bool modify = true, + bool cesu8 = false); - Span DebuggerToUtf8Span(const JSThread *thread, CVector &buf, bool modify = true); + common::Span DebuggerToUtf8Span(const JSThread *thread, CVector &buf, bool modify = true); Span ToUtf16Span(const JSThread *thread, CVector &buf) { @@ -341,7 +338,8 @@ private: auto readBarrier = [thread](const void *obj, size_t offset) -> TaggedObject * { return Barriers::GetTaggedObject(thread, obj, offset); }; - ToBaseString()->WriteData(std::move(readBarrier), src->ToBaseString(), start, destSize, length); + LineString::Cast(ToBaseString())->WriteData(std::move(readBarrier), src->ToBaseString(), start, destSize, + length); } static bool CanBeCompressed(const uint8_t *utf8Data, uint32_t utf8Len); @@ -356,20 +354,21 @@ private: bool PUBLIC_API ToTypedArrayIndex(const JSThread *thread, uint32_t *index); - template - static EcmaString *TrimBody(const JSThread *thread, const JSHandle &src, Span &data, TrimMode mode); + template + static EcmaString* TrimBody(const JSThread* thread, const JSHandle& src, common::Span& data, + TrimMode mode); - static EcmaString *Trim(const JSThread *thread, const JSHandle &src, TrimMode mode = TrimMode::TRIM); + static EcmaString* Trim(const JSThread* thread, const JSHandle& src, TrimMode mode = TrimMode::TRIM); // memory block copy template - static bool MemCopyChars(Span &dst, size_t dstMax, Span &src, size_t count); + static bool MemCopyChars(common::Span &dst, size_t dstMax, common::Span &src, size_t count); template - static int32_t IndexOf(Span &lhsSp, Span &rhsSp, int32_t pos, int32_t max); + static int32_t IndexOf(common::Span &lhsSp, common::Span &rhsSp, int32_t pos, int32_t max); template - static int32_t LastIndexOf(Span &lhsSp, Span &rhsSp, int32_t pos); + static int32_t LastIndexOf(common::Span &lhsSp, common::Span &rhsSp, int32_t pos); bool IsFlat(const JSThread *thread) const; @@ -606,7 +605,7 @@ class TreeEcmaString : public EcmaString { private: using TaggedObject::SIZE; public: - DECL_VISIT_OBJECT(TreeString::FIRST_OFFSET, TreeString::SIZE); + DECL_VISIT_OBJECT(TreeString::LEFT_OFFSET, TreeString::SIZE); CAST_CHECK(TreeEcmaString, IsTreeString); @@ -640,7 +639,7 @@ public: auto readBarrier = [thread](const void* obj, size_t offset)-> TaggedObject* { return Barriers::GetTaggedObject(thread, obj, offset); }; - return JSTaggedValue(ToTreeString()->GetFirst(std::move(readBarrier))); + return JSTaggedValue(ToTreeString()->GetLeftSubString(std::move(readBarrier))); } template @@ -651,7 +650,7 @@ public: Barriers::SetObject(thread, obj, offset, reinterpret_cast(str)); } else { Barriers::SetPrimitive(obj, offset, reinterpret_cast(str)); } }; - ToTreeString()->SetFirst(std::move(writeBarrier), value->GetTaggedObject()); + ToTreeString()->SetLeftSubString(std::move(writeBarrier), value->GetTaggedObject()); } void SetFirst(const JSThread* thread, JSTaggedValue value, BarrierMode mode = WRITE_BARRIER) @@ -661,7 +660,7 @@ public: Barriers::SetObject(thread, obj, offset, reinterpret_cast(str)); } else { Barriers::SetPrimitive(obj, offset, reinterpret_cast(str)); } }; - ToTreeString()->SetFirst(std::move(writeBarrier), value.GetTaggedObject()); + ToTreeString()->SetLeftSubString(std::move(writeBarrier), value.GetTaggedObject()); }; JSTaggedValue GetSecond(const JSThread *thread) const @@ -669,7 +668,7 @@ public: auto readBarrier = [thread](const void* obj, size_t offset)-> TaggedObject* { return Barriers::GetTaggedObject(thread, obj, offset); }; - return JSTaggedValue(ToTreeString()->GetSecond(std::move(readBarrier))); + return JSTaggedValue(ToTreeString()->GetRightSubString(std::move(readBarrier))); } template @@ -680,7 +679,7 @@ public: Barriers::SetObject(thread, obj, offset, reinterpret_cast(str)); } else { Barriers::SetPrimitive(obj, offset, reinterpret_cast(str)); } }; - ToTreeString()->SetSecond(std::move(writeBarrier), value->GetTaggedObject()); + ToTreeString()->SetRightSubString(std::move(writeBarrier), value->GetTaggedObject()); } void SetSecond(const JSThread* thread, JSTaggedValue value, BarrierMode mode = WRITE_BARRIER) @@ -690,7 +689,7 @@ public: Barriers::SetObject(thread, obj, offset, reinterpret_cast(str)); } else { Barriers::SetPrimitive(obj, offset, reinterpret_cast(str)); } }; - ToTreeString()->SetSecond(std::move(writeBarrier), value.GetTaggedObject()); + ToTreeString()->SetRightSubString(std::move(writeBarrier), value.GetTaggedObject()); }; bool IsFlat(const JSThread *thread) const @@ -915,7 +914,7 @@ public: // not change string data structure. // if string is not flat, this func has low efficiency. - Span ToUtf8Span(const JSThread *thread, CVector &buf) + common::Span ToUtf8Span(const JSThread *thread, CVector &buf) { return string_->ToUtf8Span(thread, buf); } @@ -1164,14 +1163,7 @@ public: return EcmaString::Trim(thread, src, mode); } - static bool IsASCIICharacter(uint16_t data) - { - if (data == 0) { - return false; - } - // \0 is not considered ASCII in Ecma-Modified-UTF8 [only modify '\u0000'] - return data <= common::utf_helper::UTF8_1B_MAX; - } + static bool IsASCIICharacter(uint16_t data); bool IsFlat(const JSThread *thread) const { @@ -1198,7 +1190,7 @@ public: return string_->NotTreeString(); } - inline Span FastToUtf8Span() const; + inline common::Span FastToUtf8Span() const; // the returned string may be a linestring or slicestring!! PUBLIC_API static EcmaString *Flatten(const EcmaVM *vm, const JSHandle &string, diff --git a/ecmascript/ecma_string_table.h b/ecmascript/ecma_string_table.h index c9524bb1d9f3553f095a7c950eb2d22d0f8fb685..3c34cee9a3ba43f81a2806591211ea9a88cf81d9 100644 --- a/ecmascript/ecma_string_table.h +++ b/ecmascript/ecma_string_table.h @@ -28,7 +28,7 @@ #include "ecmascript/platform/mutex.h" #include "ecmascript/tagged_array.h" #include "common_interfaces/objects/base_string_table.h" -#include "common_interfaces/objects/string/base_string_declare.h" +#include "common_interfaces/objects/string/base_string.h" namespace panda::ecmascript { #if ENABLE_NEXT_OPTIMIZATION diff --git a/ecmascript/extractortool/tests/extractor_test.cpp b/ecmascript/extractortool/tests/extractor_test.cpp index 0fc2388a85380b622810860f081ae94ca63b7f73..33919ca5e15ce007b454da6a07c4b3aea0df9dfd 100644 --- a/ecmascript/extractortool/tests/extractor_test.cpp +++ b/ecmascript/extractortool/tests/extractor_test.cpp @@ -18,6 +18,7 @@ #include "ecmascript/extractortool/src/extractor.h" #include "ecmascript/tests/test_helper.h" +#include "securec.h" using namespace panda::ecmascript; diff --git a/ecmascript/global_env_constants.cpp b/ecmascript/global_env_constants.cpp index da1b2c02efdfdad1b1797e7d96a35a72ffd5d012..41e1a404f3edf6c8f05972645d982af75be9d00d 100644 --- a/ecmascript/global_env_constants.cpp +++ b/ecmascript/global_env_constants.cpp @@ -132,11 +132,11 @@ void GlobalEnvConstants::InitSharedRootsClasses(ObjectFactory *factory) factory->NewSEcmaReadOnlyHClass(hClass, FreeObject::SIZE, JSType::FREE_OBJECT_WITH_TWO_FIELD)); if (g_isEnableCMCGC) { SetConstant(ConstantIndex::LINE_STRING_CLASS_INDEX, - factory->InitHClassInCompositeBaseClass(hClass, common::CommonType::LINE_STRING)); + factory->InitHClassInCompositeBaseClass(hClass, common::ObjectType::LINE_STRING)); SetConstant(ConstantIndex::SLICED_STRING_CLASS_INDEX, - factory->InitHClassInCompositeBaseClass(hClass, common::CommonType::SLICED_STRING)); + factory->InitHClassInCompositeBaseClass(hClass, common::ObjectType::SLICED_STRING)); SetConstant(ConstantIndex::TREE_STRING_CLASS_INDEX, - factory->InitHClassInCompositeBaseClass(hClass, common::CommonType::TREE_STRING)); + factory->InitHClassInCompositeBaseClass(hClass, common::ObjectType::TREE_STRING)); } else { SetConstant(ConstantIndex::LINE_STRING_CLASS_INDEX, factory->NewSEcmaReadOnlyHClass(hClass, 0, JSType::LINE_STRING)); diff --git a/ecmascript/js_api/js_api_buffer.cpp b/ecmascript/js_api/js_api_buffer.cpp index a0adb5f6a24abffe70647ca1071b5480696e1583..cacd7303ddbda5e77701cc61e42a93f4065170fb 100644 --- a/ecmascript/js_api/js_api_buffer.cpp +++ b/ecmascript/js_api/js_api_buffer.cpp @@ -180,7 +180,7 @@ string_view FromStringBase64(JSThread *thread, const JSHandle &st { auto strAccessor = EcmaStringAccessor(JSHandle(str)); CVector buf; - Span sp = strAccessor.ToUtf8Span(thread, buf); + common::Span sp = strAccessor.ToUtf8Span(thread, buf); StringConverter::Base64Decode(string_view(reinterpret_cast(sp.data()), sp.size()), stringDecoded); return std::string_view(stringDecoded); } diff --git a/ecmascript/js_hclass-inl.h b/ecmascript/js_hclass-inl.h index ab5584bb2349eb0bc4e5e9db32aa3566b12d2f4f..afc0c9de19d894635ab6ac12f4e6579fd88dd322 100644 --- a/ecmascript/js_hclass-inl.h +++ b/ecmascript/js_hclass-inl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/ecmascript/js_hclass.h b/ecmascript/js_hclass.h index 13ae3dd9adda37dcbe5809e2b8446b9facdd0a2c..4417205113cd498187790b2c8aea9274124b1e08 100644 --- a/ecmascript/js_hclass.h +++ b/ecmascript/js_hclass.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -358,11 +358,11 @@ enum class JSType : uint8_t { PRESET_JSTYPE_DECL, }; -static_assert(static_cast(JSType::LINE_STRING) == static_cast(common::CommonType::LINE_STRING) && +static_assert(static_cast(JSType::LINE_STRING) == static_cast(common::ObjectType::LINE_STRING) && "line string type should be same with common type"); -static_assert(static_cast(JSType::SLICED_STRING) == static_cast(common::CommonType::SLICED_STRING) && +static_assert(static_cast(JSType::SLICED_STRING) == static_cast(common::ObjectType::SLICED_STRING) && "sliced string type should be same with common type"); -static_assert(static_cast(JSType::TREE_STRING) == static_cast(common::CommonType::TREE_STRING) && +static_assert(static_cast(JSType::TREE_STRING) == static_cast(common::ObjectType::TREE_STRING) && "tree string type should be same with common type"); struct TransitionResult { @@ -682,8 +682,8 @@ public: // These types are not complete hclass, does not has profile field. inline bool IsCompositeHClass() const { - common::CommonType jsType = static_cast(GetObjectType()); - return (common::CommonType::FIRST_OBJECT_TYPE <= jsType && jsType <= common::CommonType::LAST_OBJECT_TYPE); + common::ObjectType jsType = static_cast(GetObjectType()); + return (common::ObjectType::FIRST_OBJECT_TYPE <= jsType && jsType <= common::ObjectType::LAST_OBJECT_TYPE); } inline bool IsString() const diff --git a/ecmascript/js_tagged_value-inl.h b/ecmascript/js_tagged_value-inl.h index f07aaee73a09f6988a22e11b953e17b754fa0683..257d48f254f1006b888ddd1794d30c28845abbc4 100644 --- a/ecmascript/js_tagged_value-inl.h +++ b/ecmascript/js_tagged_value-inl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1429,7 +1429,7 @@ inline JSTaggedNumber JSTaggedValue::StringToDouble(JSThread *thread, JSTaggedVa return JSTaggedNumber(0); } CVector buf; - Span str = EcmaStringAccessor(strObj).ToUtf8Span(thread, buf); + common::Span str = EcmaStringAccessor(strObj).ToUtf8Span(thread, buf); double d = base::NumberHelper::StringToDouble(str.begin(), str.end(), 0, base::ALLOW_BINARY + base::ALLOW_OCTAL + base::ALLOW_HEX); return JSTaggedNumber(d); diff --git a/ecmascript/js_tagged_value.cpp b/ecmascript/js_tagged_value.cpp index 384bea36e8648593d2e9664525ec62d7fe2a5842..2b9d78460bff8cf1cd42bccfda6af38aefc9a0bd 100644 --- a/ecmascript/js_tagged_value.cpp +++ b/ecmascript/js_tagged_value.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -1770,7 +1770,7 @@ JSTaggedNumber JSTaggedValue::StringToNumber(JSThread *thread, JSTaggedValue tag } } #endif - Span str = strAccessor.FastToUtf8Span(); + common::Span str = strAccessor.FastToUtf8Span(); if (strAccessor.GetLength() == 0) { return JSTaggedNumber(0); } @@ -1780,7 +1780,7 @@ JSTaggedNumber JSTaggedValue::StringToNumber(JSThread *thread, JSTaggedValue tag } } CVector buf; - Span str = strAccessor.ToUtf8Span(thread, buf); + common::Span str = strAccessor.ToUtf8Span(thread, buf); double d = base::NumberHelper::StringToDouble(str.begin(), str.end(), 0, base::ALLOW_BINARY + base::ALLOW_OCTAL + base::ALLOW_HEX); return JSTaggedNumber(d); diff --git a/ecmascript/mem/c_string.cpp b/ecmascript/mem/c_string.cpp index 6b14c004b5f7b7031b3eb2bb14e2726c9c476423..ad34e0f386468ad2a17052055e398d1baa224a2b 100644 --- a/ecmascript/mem/c_string.cpp +++ b/ecmascript/mem/c_string.cpp @@ -260,7 +260,7 @@ void ConvertQuotedAndAppendToCString(const JSThread *thread, CString &str, const uint32_t strLen = EcmaStringAccessor(const_cast(s)).GetLength(); CVector buf; const uint8_t *data = EcmaStringAccessor::GetUtf8DataFlat(thread, s, buf); - const Span dataSpan(data, strLen); + const common::Span dataSpan(data, strLen); base::JsonHelper::AppendValueToQuotedString(dataSpan, str); } @@ -272,12 +272,12 @@ void ConvertQuotedAndAppendToC16String(const JSThread *thread, C16String &str, c if (EcmaStringAccessor(const_cast(s)).IsUtf8()) { CVector buf; const uint8_t *data = EcmaStringAccessor::GetUtf8DataFlat(thread, s, buf); - const Span dataSpan(data, strLen); + const common::Span dataSpan(data, strLen); base::JsonHelper::AppendValueToQuotedString(dataSpan, str); } else { CVector buf; const uint16_t *data = EcmaStringAccessor::GetUtf16DataFlat(thread, s, buf); - const Span dataSpan(data, strLen); + const common::Span dataSpan(data, strLen); base::JsonHelper::AppendValueToQuotedString(dataSpan, str); } } diff --git a/ecmascript/mem/tagged_state_word.h b/ecmascript/mem/tagged_state_word.h index 402a3158b8e3cffdb56cb0dc86c1cc56e9663403..89919b5714fb721d35e20252514d27b73e2c556d 100644 --- a/ecmascript/mem/tagged_state_word.h +++ b/ecmascript/mem/tagged_state_word.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -103,7 +103,8 @@ private: friend class TaggedObject; }; static_assert(sizeof(TaggedStateWord) == sizeof(uint64_t), "Excepts 8 bytes"); -static_assert(common::BaseStateWord::PADDING_WIDTH == 60, "Excepts 60 bits"); +static_assert(common::BaseStateWord::BASECLASS_WIDTH == 48, "Excepts 48 bits"); +static_assert(common::BaseStateWord::PADDING_WIDTH == 12, "Excepts 12 bits"); static_assert(common::BaseStateWord::FORWARD_WIDTH == 2, "Excepts 2 bits"); static_assert(common::BaseStateWord::LANGUAGE_WIDTH == 2, "Excepts 2 bits"); } // namespace panda::ecmascript diff --git a/ecmascript/object_factory.h b/ecmascript/object_factory.h index cac0e93b06820cdfc3cdb2643f86345c531a292f..b1b4cf864dbe219971b2542eca604c91cbf4856f 100644 --- a/ecmascript/object_factory.h +++ b/ecmascript/object_factory.h @@ -819,7 +819,7 @@ public: JSHandle NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); - JSTaggedValue InitHClassInCompositeBaseClass(JSHClass* hclass, common::CommonType type); + JSTaggedValue InitHClassInCompositeBaseClass(JSHClass* hclass, common::ObjectType type); JSHandle NewSEcmaReadOnlySharedHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); diff --git a/ecmascript/shared_object_factory.cpp b/ecmascript/shared_object_factory.cpp index 8d3077c3a4e2587462f1a6e87c5ad1e86c23a863..9f6e518fc4876eefc1a1bb6611fcaa8bc546a73d 100644 --- a/ecmascript/shared_object_factory.cpp +++ b/ecmascript/shared_object_factory.cpp @@ -186,7 +186,7 @@ JSHandle ObjectFactory::NewSEcmaReadOnlySharedHClass(JSHClass *hclass, return newClass; } -JSTaggedValue ObjectFactory::InitHClassInCompositeBaseClass(JSHClass* hclass, common::CommonType type) +JSTaggedValue ObjectFactory::InitHClassInCompositeBaseClass(JSHClass* hclass, common::ObjectType type) { common::BaseClassRoots& classRoots = common::BaseRuntime::GetInstance()->GetBaseClassRoots(); auto* newClass = reinterpret_cast(classRoots.GetBaseClass(type)); diff --git a/ecmascript/stubs/runtime_stubs.cpp b/ecmascript/stubs/runtime_stubs.cpp index 4f213f59f51f88a4faf33212bbd8a087536a6d01..01f9af3283489c07eec7842ee6885536b001d421 100644 --- a/ecmascript/stubs/runtime_stubs.cpp +++ b/ecmascript/stubs/runtime_stubs.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -3173,7 +3173,6 @@ DEF_RUNTIME_STUBS(ContainerRBTreeForEach) return JSTaggedValue::True().GetRawData(); } - DEF_RUNTIME_STUBS(GetOrInternStringFromHashTrieTable) { RUNTIME_STUBS_HEADER(GetOrInternStringFromHashTrieTable); @@ -3564,7 +3563,7 @@ JSTaggedValue RuntimeStubs::NumberHelperStringToDouble(uintptr_t argGlue, EcmaSt DISALLOW_GARBAGE_COLLECTION; CVector buf; JSThread *thread = JSThread::GlueToJSThread(argGlue); - Span str = EcmaStringAccessor(numberString).ToUtf8Span(thread, buf); + common::Span str = EcmaStringAccessor(numberString).ToUtf8Span(thread, buf); if (base::NumberHelper::IsEmptyString(str.begin(), str.end())) { return base::BuiltinsBase::GetTaggedDouble(base::NAN_VALUE); } @@ -3847,10 +3846,10 @@ int32_t RuntimeStubs::StringGetStart(bool isUtf8, EcmaString *srcString, int32_t { DISALLOW_GARBAGE_COLLECTION; if (isUtf8) { - Span data(EcmaStringAccessor(srcString).GetDataUtf8() + startIndex, length); + common::Span data(EcmaStringAccessor(srcString).GetDataUtf8() + startIndex, length); return static_cast(base::StringHelper::GetStart(data, length)); } else { - Span data(EcmaStringAccessor(srcString).GetDataUtf16() + startIndex, length); + common::Span data(EcmaStringAccessor(srcString).GetDataUtf16() + startIndex, length); return static_cast(base::StringHelper::GetStart(data, length)); } } @@ -3860,10 +3859,10 @@ int32_t RuntimeStubs::StringGetEnd(bool isUtf8, EcmaString *srcString, { DISALLOW_GARBAGE_COLLECTION; if (isUtf8) { - Span data(EcmaStringAccessor(srcString).GetDataUtf8() + startIndex, length); + common::Span data(EcmaStringAccessor(srcString).GetDataUtf8() + startIndex, length); return base::StringHelper::GetEnd(data, start, length); } else { - Span data(EcmaStringAccessor(srcString).GetDataUtf16() + startIndex, length); + common::Span data(EcmaStringAccessor(srcString).GetDataUtf16() + startIndex, length); return base::StringHelper::GetEnd(data, start, length); } } diff --git a/ecmascript/tagged_dictionary.cpp b/ecmascript/tagged_dictionary.cpp index b93c90ef2903379265fd0c4eef81c474094f8e44..63b6d9d741240e9cf7ca78b29300873f5300ac32 100644 --- a/ecmascript/tagged_dictionary.cpp +++ b/ecmascript/tagged_dictionary.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -57,8 +57,8 @@ bool NameDictionary::IsMatch(const uint8_t* str, int size, const JSTaggedValue & } EcmaString *keyString = reinterpret_cast(other.GetTaggedObject()); - Span data1(EcmaStringAccessor(keyString).GetDataUtf8(), keyString->GetLength()); - Span data2(str, size); + common::Span data1(EcmaStringAccessor(keyString).GetDataUtf8(), keyString->GetLength()); + common::Span data2(str, size); if (data1.Size() != data2.Size()) { return false; } diff --git a/ecmascript/tests/base_string_test.cpp b/ecmascript/tests/base_string_test.cpp index fe3f069f78ca6be1b5cb465c2c35efb7289a2967..054a96df609ed7b354e61dee757644c395a77b23 100644 --- a/ecmascript/tests/base_string_test.cpp +++ b/ecmascript/tests/base_string_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,7 +14,7 @@ */ #include "ecmascript/ecma_string-inl.h" -#include "common_interfaces/objects/string/base_string_impl.h" +#include "common_interfaces/objects/string/base_string-inl.h" #include "ecmascript/object_factory.h" #include "ecmascript/tests/ecma_test_common.h" @@ -695,12 +695,12 @@ namespace panda::test { instance, &arrayU8WriteFrom[0], lengthEcmaStrU8WriteFrom, true)); size_t sizeEcmaStrU8WriteTo = 5; - JSHandle handleEcmaStrAllocTrueWriteTo(thread, + JSHandle handleEcmaStrAllocTrueWriteTo(thread, EcmaStringAccessor::CreateLineString( instance, sizeEcmaStrU8WriteTo, true)); uint32_t indexStartWriteFromArrayU8 = 2; uint32_t lengthWriteFromArrayU8 = 2; - handleEcmaStrAllocTrueWriteTo->ToBaseString()->WriteData( + LineString::Cast(handleEcmaStrAllocTrueWriteTo->ToBaseString())->WriteData( LoadTBarrier(), handleEcmaStrU8WriteFrom->ToBaseString(), indexStartWriteFromArrayU8, sizeEcmaStrU8WriteTo, lengthWriteFromArrayU8); for (uint32_t i = 0; i < lengthWriteFromArrayU8; i++) { @@ -722,11 +722,11 @@ namespace panda::test { // WriteData(). From char to EcmaString made by CreateLineString( , true, ). char u8Write = 'a'; size_t sizeEcmaStrU8WriteTo = 5; - JSHandle handleEcmaStrAllocTrueWriteTo(thread, + JSHandle handleEcmaStrAllocTrueWriteTo(thread, EcmaStringAccessor::CreateLineString( instance, sizeEcmaStrU8WriteTo, true)); uint32_t indexAtWriteFromU8 = 4; - handleEcmaStrAllocTrueWriteTo->ToBaseString()->WriteData(indexAtWriteFromU8, u8Write); + LineString::Cast(handleEcmaStrAllocTrueWriteTo->ToBaseString())->Set(indexAtWriteFromU8, u8Write); EXPECT_EQ(handleEcmaStrAllocTrueWriteTo->ToBaseString()->At(LoadTBarrier(), indexAtWriteFromU8), u8Write); } @@ -749,12 +749,12 @@ namespace panda::test { instance, &arrayU16WriteFrom[0], lengthEcmaStrU16WriteFrom, false)); size_t sizeEcmaStrU16WriteTo = 10; - JSHandle handleEcmaStrU16WriteTo(thread, + JSHandle handleEcmaStrU16WriteTo(thread, EcmaStringAccessor::CreateLineString( instance, sizeEcmaStrU16WriteTo, false)); uint32_t indexStartWriteFromArrayU16 = 3; uint32_t numBytesWriteFromArrayU16 = 2 * 3; - handleEcmaStrU16WriteTo->ToBaseString()->WriteData(LoadTBarrier(), handleEcmaStrU16WriteFrom->ToBaseString(), + handleEcmaStrU16WriteTo->ToLineString()->WriteData(LoadTBarrier(), handleEcmaStrU16WriteFrom->ToBaseString(), indexStartWriteFromArrayU16, sizeEcmaStrU16WriteTo, numBytesWriteFromArrayU16); for (uint32_t i = 0; i < (numBytesWriteFromArrayU16 / 2); i++) { @@ -780,14 +780,14 @@ namespace panda::test { instance, &arrayU8WriteFrom[0], lengthEcmaStrU8WriteFrom, true)); size_t sizeEcmaStrU16WriteTo = 10; - JSHandle handleEcmaStrU16WriteTo(thread, + JSHandle handleEcmaStrU16WriteTo(thread, EcmaStringAccessor::CreateLineString( instance, sizeEcmaStrU16WriteTo, false)); uint32_t indexStartWriteFromU8ToU16 = 1; uint32_t numBytesWriteFromU8ToU16 = 4; - handleEcmaStrU16WriteTo->ToBaseString()->WriteData(LoadTBarrier(), handleEcmaStrU8WriteFrom->ToBaseString(), - indexStartWriteFromU8ToU16, sizeEcmaStrU16WriteTo, - numBytesWriteFromU8ToU16); + LineString::Cast(handleEcmaStrU16WriteTo->ToBaseString())->WriteData(LoadTBarrier(), + handleEcmaStrU8WriteFrom->ToBaseString(), indexStartWriteFromU8ToU16, sizeEcmaStrU16WriteTo, + numBytesWriteFromU8ToU16); for (uint32_t i = 0; i < numBytesWriteFromU8ToU16; i++) { EXPECT_EQ(handleEcmaStrU16WriteTo->ToBaseString()->At(LoadTBarrier(), i + indexStartWriteFromU8ToU16), handleEcmaStrU8WriteFrom->ToBaseString()->At(LoadTBarrier(), i)); @@ -805,13 +805,13 @@ namespace panda::test { { // WriteData(). From char to EcmaString made by CreateLineString( , false, ). size_t sizeEcmaStrU16WriteTo = 10; - JSHandle handleEcmaStrU16WriteTo(thread, + JSHandle handleEcmaStrU16WriteTo(thread, EcmaStringAccessor::CreateLineString( instance, sizeEcmaStrU16WriteTo, false)); char u8Write = 'a'; uint32_t indexAt = 4; - handleEcmaStrU16WriteTo->ToBaseString()->WriteData(indexAt, u8Write); - EXPECT_EQ(handleEcmaStrU16WriteTo->ToBaseString()->At(LoadTBarrier(), indexAt), u8Write); + handleEcmaStrU16WriteTo->ToLineString()->Set(indexAt, u8Write); + EXPECT_EQ(handleEcmaStrU16WriteTo->ToLineString()->At(LoadTBarrier(), indexAt), u8Write); } /* @@ -855,21 +855,21 @@ namespace panda::test { // From EcmaString made by CreateFromUtf8(). uint8_t arrayU8[] = {"abcde"}; uint32_t lengthEcmaStrU8 = sizeof(arrayU8) - 1; - JSHandle handleEcmaStrU8(thread, + JSHandle handleEcmaStrU8(thread, EcmaStringAccessor::CreateFromUtf8( instance, &arrayU8[0], lengthEcmaStrU8, true)); for (uint32_t i = 0; i < lengthEcmaStrU8; i++) { - EXPECT_EQ(*(handleEcmaStrU8->ToBaseString()->GetDataUtf8() + i), arrayU8[i]); + EXPECT_EQ(*(handleEcmaStrU8->ToLineString()->GetDataUtf8() + i), arrayU8[i]); } // From EcmaString made by CreateFromUtf16( , , , true). uint16_t arrayU16Comp[] = {3, 1, 34, 123, 127, 111, 42, 3, 20, 10}; uint32_t lengthEcmaStrU16Comp = sizeof(arrayU16Comp) / sizeof(arrayU16Comp[0]); - JSHandle handleEcmaStrU16Comp(thread, + JSHandle handleEcmaStrU16Comp(thread, EcmaStringAccessor::CreateFromUtf16( instance, &arrayU16Comp[0], lengthEcmaStrU16Comp, true)); for (uint32_t i = 0; i < sizeof(arrayU16Comp) / arrayU16Comp[0]; i++) { - EXPECT_EQ(*(handleEcmaStrU16Comp->ToBaseString()->GetDataUtf8() + i), arrayU16Comp[i]); + EXPECT_EQ(*(handleEcmaStrU16Comp->ToLineString()->GetDataUtf8() + i), arrayU16Comp[i]); } } @@ -884,12 +884,12 @@ namespace panda::test { // From EcmaString made by CreateFromUtf16( , , , false). uint16_t arrayU16NotComp[] = {67, 777, 1999, 1, 45, 66, 23456, 65535, 127, 333}; uint32_t lengthEcmaStrU16NotComp = sizeof(arrayU16NotComp) / sizeof(arrayU16NotComp[0]); - JSHandle handleEcmaStrU16NotComp(thread, + JSHandle handleEcmaStrU16NotComp(thread, EcmaStringAccessor::CreateFromUtf16( instance, &arrayU16NotComp[0], lengthEcmaStrU16NotComp, false)); for (uint32_t i = 0; i < lengthEcmaStrU16NotComp; i++) { - EXPECT_EQ(*(handleEcmaStrU16NotComp->ToBaseString()->GetDataUtf16() + i), arrayU16NotComp[i]); + EXPECT_EQ(*(handleEcmaStrU16NotComp->ToLineString()->GetDataUtf16() + i), arrayU16NotComp[i]); } } @@ -905,9 +905,8 @@ namespace panda::test { // CopyDataRegionUtf8(). From EcmaString made by CreateFromUtf8(). uint8_t arrayU8CopyFrom[6] = {1, 12, 34, 56, 127}; uint32_t lengthEcmaStrU8CopyFrom = sizeof(arrayU8CopyFrom) - 1; - JSHandle handleEcmaStrU8CopyFrom(thread, - EcmaStringAccessor::CreateFromUtf8( - instance, &arrayU8CopyFrom[0], lengthEcmaStrU8CopyFrom, true)); + JSHandle handleEcmaStrU8CopyFrom(thread, EcmaStringAccessor::CreateFromUtf8( + instance, &arrayU8CopyFrom[0], lengthEcmaStrU8CopyFrom, true)); const size_t lengthArrayU8Target = 7; uint8_t defaultByteForU8CopyTo = 1; uint8_t arrayU8CopyTo[lengthArrayU8Target]; @@ -932,10 +931,8 @@ namespace panda::test { // CopyDataRegionUtf8(). From EcmaString made by CreateFromUtf16( , , , true). uint16_t arrayU16CompCopyFrom[] = {1, 12, 34, 56, 127}; uint32_t lengthEcmaStrU16CompCopyFrom = sizeof(arrayU16CompCopyFrom) / sizeof(arrayU16CompCopyFrom[0]); - JSHandle handleEcmaStrU16CompCopyFrom(thread, - EcmaStringAccessor::CreateFromUtf16( - instance, &arrayU16CompCopyFrom[0], - lengthEcmaStrU16CompCopyFrom, true)); + JSHandle handleEcmaStrU16CompCopyFrom(thread, EcmaStringAccessor::CreateFromUtf16( + instance, &arrayU16CompCopyFrom[0], lengthEcmaStrU16CompCopyFrom, true)); const size_t lengthArrayU16Target = 8; uint8_t defaultByteForU16CompCopyTo = 1; uint8_t arrayU16CompCopyTo[lengthArrayU16Target]; @@ -1568,7 +1565,7 @@ namespace panda::test { * @tc.type: FUNC * @tc.require: */ - HWTEST_F_L0(BaseStringTest, EqualToSplicedString) + HWTEST_F_L0(BaseStringTest, EqualToSplicedString001) { ObjectFactory* factory = instance->GetFactory(); { @@ -1618,7 +1615,18 @@ namespace panda::test { LoadTBarrier(), firstString->ToBaseString(), secondString->ToBaseString()); EXPECT_TRUE(result); } + } + /* + * @tc.name: EqualToSplicedString + * @tc.desc: Tests whether the source string is equal to the concatenated string. + * is within expectations. + * @tc.type: FUNC + * @tc.require: + */ + HWTEST_F_L0(BaseStringTest, EqualToSplicedString002) + { + ObjectFactory* factory = instance->GetFactory(); { JSHandle sourceString = factory->NewFromUtf8("Startstart"); JSHandle firstString = factory->NewFromASCII("Start"); diff --git a/ecmascript/tests/dynamic_type_converter_test.cpp b/ecmascript/tests/dynamic_type_converter_test.cpp index 1a05b2169933933ad178ddcaf87a5d2a4fd78c81..33744e1da015f9f3798de8db300536562fd7d410 100644 --- a/ecmascript/tests/dynamic_type_converter_test.cpp +++ b/ecmascript/tests/dynamic_type_converter_test.cpp @@ -215,7 +215,7 @@ HWTEST_F_L0(DynamicTypeConverterTest, WrapTagged_Test2) EcmaString* str1 = EcmaString::Cast(result.GetTaggedObject()); // Verify BigInt properties EXPECT_EQ(EcmaStringAccessor(str1).GetLength(), str->ToBaseString()->GetLength()); - EXPECT_EQ(EcmaStringAccessor(str1).GetDataUtf8(), str->ToBaseString()->GetDataUtf8()); + EXPECT_EQ(EcmaStringAccessor(str1).GetDataUtf8(), LineString::Cast(str->ToBaseString())->GetDataUtf8()); } } diff --git a/ecmascript/tests/ecma_string_equals_test.cpp b/ecmascript/tests/ecma_string_equals_test.cpp index a8ae4373d1772ee9e77cf42d2bf730d1d3803f1e..1ebb7ab4eb036b5992821d0b8351056f1186dcc6 100644 --- a/ecmascript/tests/ecma_string_equals_test.cpp +++ b/ecmascript/tests/ecma_string_equals_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "common_interfaces/objects/string/base_string-inl.h" #include "ecmascript/ecma_string-inl.h" #include "ecmascript/object_factory.h" #include "ecmascript/tests/ecma_test_common.h" diff --git a/ecmascript/tests/js_hclass_test.cpp b/ecmascript/tests/js_hclass_test.cpp index 9534e15859acf26670a9643c36a211125288d775..97ab4799c0ebb19ba9f7a572dcfc2c45c6ee135f 100644 --- a/ecmascript/tests/js_hclass_test.cpp +++ b/ecmascript/tests/js_hclass_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -75,10 +75,18 @@ HWTEST_F_L0(JSHClassTest, SizeFromJSHClass) EXPECT_EQ(objectSize, 96U); EcmaString *string = EcmaStringAccessor::CreateEmptyString(vm); objectSize = string->GetSize(); +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + EXPECT_EQ(objectSize, 24U); +#else EXPECT_EQ(objectSize, 16U); +#endif string = factory->AllocTreeStringObject(); objectSize = string->GetClass()->SizeFromJSHClass(string); +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + EXPECT_EQ(objectSize, 40U); +#else EXPECT_EQ(objectSize, 32U); +#endif MachineCodeDesc desc; desc.codeType = MachineCodeType::BASELINE_CODE; desc.instructionsSize = 100; diff --git a/ecmascript/tests/js_stable_array_test.cpp b/ecmascript/tests/js_stable_array_test.cpp index 7e9a5b9d438867fd8190e1434f2d62dc627e324b..6c818b99dd591cd9b56df1be6361ea8946209300 100644 --- a/ecmascript/tests/js_stable_array_test.cpp +++ b/ecmascript/tests/js_stable_array_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -370,6 +370,7 @@ HWTEST_F_L0(JSStableArrayTest, Join_StringElements_DefinedSep) EXPECT_FALSE(EcmaStringAccessor(handleEcmaStrRet).IsTreeString()); } + /** * @tc.name: Join_StringElements_ManyTiny * @tc.desc: Create a source Array whose elements are EcmaStrings and an EcmaRuntimeCallInfo, define the first arg of @@ -423,6 +424,27 @@ HWTEST_F_L0(JSStableArrayTest, Join_StringElements_ManyTiny) */ HWTEST_F_L0(JSStableArrayTest, Join_StringElements_LargeString) { +#if defined(ARK_HYBRID) || defined(USE_CMC_GC) + int32_t lengthArr = 8; + std::string sep = ""; + // large string should use tree string. + ObjectFactory* objFactory = thread->GetEcmaVM()->GetFactory(); + JSHandle handleTagArr(objFactory->NewTaggedArray(lengthArr)); + // 40 x a + JSHandle + handleTagValElementEcmaStr(objFactory->NewFromStdString("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); + for (int i = 0; i < lengthArr; i++) { + handleTagArr->Set(thread, i, handleTagValElementEcmaStr.GetTaggedValue()); + } + JSHandle handleTagValEcmaStrRet = CallJoin(handleTagArr, sep, lengthArr); + JSHandle handleEcmaStrRet(handleTagValEcmaStrRet); + EXPECT_STREQ(EcmaStringAccessor(handleEcmaStrRet).ToCString(thread).c_str(), + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrRet).IsTreeString()); +#else int32_t lengthArr = 8; std::string sep = ""; // large string should use tree string. @@ -442,6 +464,7 @@ HWTEST_F_L0(JSStableArrayTest, Join_StringElements_LargeString) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); EXPECT_TRUE(EcmaStringAccessor(handleEcmaStrRet).IsTreeString()); +#endif } /**