diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index 8000d0d139e..f7662463722 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -980,7 +980,7 @@ inline ThrowCompletionOr get_by_id(VM& vm, Optional { if constexpr (mode == GetByIdMode::Length) { if (base_value.is_string()) { - return Value(base_value.as_string().utf16_string().length_in_code_units()); + return Value(base_value.as_string().length_in_utf16_code_units()); } } diff --git a/Libraries/LibJS/Runtime/PrimitiveString.cpp b/Libraries/LibJS/Runtime/PrimitiveString.cpp index 1c6d8d5a56e..3a156123ed4 100644 --- a/Libraries/LibJS/Runtime/PrimitiveString.cpp +++ b/Libraries/LibJS/Runtime/PrimitiveString.cpp @@ -106,6 +106,11 @@ Utf16View PrimitiveString::utf16_string_view() const return m_utf16_string->view(); } +size_t PrimitiveString::length_in_utf16_code_units() const +{ + return utf16_string_view().length_in_code_units(); +} + bool PrimitiveString::operator==(PrimitiveString const& other) const { if (this == &other) @@ -123,8 +128,7 @@ ThrowCompletionOr> PrimitiveString::get(VM& vm, PropertyKey cons return Optional {}; if (property_key.is_string()) { if (property_key.as_string() == vm.names.length.as_string()) { - auto length = utf16_string().length_in_code_units(); - return Value(static_cast(length)); + return Value(static_cast(length_in_utf16_code_units())); } } auto index = canonical_numeric_index_string(property_key, CanonicalIndexMode::IgnoreNumericRoundtrip); diff --git a/Libraries/LibJS/Runtime/PrimitiveString.h b/Libraries/LibJS/Runtime/PrimitiveString.h index 7be62544c5e..3d7134bc1eb 100644 --- a/Libraries/LibJS/Runtime/PrimitiveString.h +++ b/Libraries/LibJS/Runtime/PrimitiveString.h @@ -45,6 +45,8 @@ public: [[nodiscard]] Utf16View utf16_string_view() const; bool has_utf16_string() const { return m_utf16_string.has_value(); } + size_t length_in_utf16_code_units() const; + ThrowCompletionOr> get(VM&, PropertyKey const&) const; [[nodiscard]] bool operator==(PrimitiveString const&) const; diff --git a/Libraries/LibJS/Runtime/StringObject.cpp b/Libraries/LibJS/Runtime/StringObject.cpp index 469bb188086..517feb9ae24 100644 --- a/Libraries/LibJS/Runtime/StringObject.cpp +++ b/Libraries/LibJS/Runtime/StringObject.cpp @@ -42,7 +42,7 @@ void StringObject::initialize(Realm& realm) auto& vm = this->vm(); Base::initialize(realm); - define_direct_property(vm.names.length, Value(m_string->utf16_string_view().length_in_code_units()), 0); + define_direct_property(vm.names.length, Value(m_string->length_in_utf16_code_units()), 0); } void StringObject::visit_edges(Cell::Visitor& visitor) @@ -136,12 +136,9 @@ ThrowCompletionOr> StringObject::internal_own_property_key auto keys = GC::RootVector { heap() }; // 2. Let str be O.[[StringData]]. - auto str = m_string->utf16_string_view(); - // 3. Assert: str is a String. - // 4. Let len be the length of str. - auto length = str.length_in_code_units(); + auto length = m_string->length_in_utf16_code_units(); // 5. For each integer i starting with 0 such that i < len, in ascending order, do for (size_t i = 0; i < length; ++i) { diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index 7cc5be8070e..61bc0de74ed 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -921,7 +921,7 @@ WebIDL::ExceptionOr> evaluate_key_path_on_a_value(JS::Realm& // If Type(value) is String, and identifier is "length" if (value.is_string() && identifier == "length") { // Let value be a Number equal to the number of elements in value. - value = JS::Value(value.as_string().utf16_string_view().length_in_code_units()); + value = JS::Value(value.as_string().length_in_utf16_code_units()); } // If value is an Array and identifier is "length"