2020-03-11 14:55:01 -03:00
|
|
|
/*
|
2025-03-28 10:55:39 -03:00
|
|
|
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
|
2022-12-06 19:17:27 -03:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2020-03-11 14:55:01 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-11 14:55:01 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-01-07 14:24:05 -03:00
|
|
|
#include <AK/Optional.h>
|
2023-01-13 14:24:02 -03:00
|
|
|
#include <AK/String.h>
|
2022-03-16 21:26:49 -03:00
|
|
|
#include <AK/StringView.h>
|
2025-07-09 15:13:22 -03:00
|
|
|
#include <AK/Utf16String.h>
|
2024-11-14 12:01:23 -03:00
|
|
|
#include <LibGC/CellAllocator.h>
|
2025-07-19 17:49:30 -03:00
|
|
|
#include <LibJS/Export.h>
|
2022-02-12 05:48:23 -03:00
|
|
|
#include <LibJS/Forward.h>
|
2021-05-17 14:50:20 -03:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2023-01-07 14:24:05 -03:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2022-02-13 10:55:23 -03:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-11 14:55:01 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API PrimitiveString : public Cell {
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_CELL(PrimitiveString, Cell);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(PrimitiveString);
|
2022-08-28 17:11:20 -03:00
|
|
|
|
2020-03-11 14:55:01 -03:00
|
|
|
public:
|
2026-01-29 08:50:29 -03:00
|
|
|
static constexpr bool OVERRIDES_FINALIZE = true;
|
|
|
|
|
|
2025-10-04 05:15:57 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, Utf16String const&);
|
2025-07-09 15:13:22 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, Utf16View const&);
|
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, Utf16FlyString const&);
|
|
|
|
|
|
2025-10-04 05:15:57 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, String const&);
|
2025-07-09 15:13:22 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, StringView);
|
2024-11-14 12:01:23 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, FlyString const&);
|
2025-07-09 15:13:22 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, PrimitiveString&, PrimitiveString&);
|
2026-04-10 12:13:54 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, PrimitiveString const&, size_t code_unit_offset, size_t code_unit_length);
|
2022-12-06 19:17:27 -03:00
|
|
|
|
2025-10-04 06:00:20 -03:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create_from_unsigned_integer(VM&, u64);
|
|
|
|
|
|
2026-01-29 08:50:29 -03:00
|
|
|
virtual ~PrimitiveString() override;
|
2020-03-11 14:55:01 -03:00
|
|
|
|
2021-08-01 20:02:19 -03:00
|
|
|
PrimitiveString(PrimitiveString const&) = delete;
|
|
|
|
|
PrimitiveString& operator=(PrimitiveString const&) = delete;
|
|
|
|
|
|
2022-07-17 16:08:53 -03:00
|
|
|
bool is_empty() const;
|
|
|
|
|
|
2023-08-08 14:17:55 -03:00
|
|
|
[[nodiscard]] String utf8_string() const;
|
2023-01-13 14:24:02 -03:00
|
|
|
|
2023-08-08 13:54:20 -03:00
|
|
|
[[nodiscard]] Utf16String utf16_string() const;
|
|
|
|
|
[[nodiscard]] Utf16View utf16_string_view() const;
|
2023-01-07 14:24:05 -03:00
|
|
|
bool has_utf16_string() const { return m_utf16_string.has_value(); }
|
2020-03-11 14:55:01 -03:00
|
|
|
|
2025-05-03 09:06:34 -03:00
|
|
|
size_t length_in_utf16_code_units() const;
|
|
|
|
|
|
2023-01-07 14:24:05 -03:00
|
|
|
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
|
2022-02-13 10:55:23 -03:00
|
|
|
|
2025-04-10 07:18:08 -03:00
|
|
|
[[nodiscard]] bool operator==(PrimitiveString const&) const;
|
|
|
|
|
|
2025-03-29 21:08:39 -03:00
|
|
|
protected:
|
2026-04-10 12:08:14 -03:00
|
|
|
enum class DeferredKind : u8 {
|
|
|
|
|
None,
|
|
|
|
|
Rope,
|
|
|
|
|
Substring,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit PrimitiveString(DeferredKind deferred_kind)
|
|
|
|
|
: m_deferred_kind(deferred_kind)
|
2025-03-29 21:08:39 -03:00
|
|
|
{
|
|
|
|
|
}
|
2022-08-28 18:51:28 -03:00
|
|
|
|
2026-04-10 12:08:14 -03:00
|
|
|
mutable DeferredKind m_deferred_kind { DeferredKind::None };
|
2025-03-29 21:08:39 -03:00
|
|
|
|
|
|
|
|
mutable Optional<Utf16String> m_utf16_string;
|
2020-03-11 14:55:01 -03:00
|
|
|
|
2026-04-10 12:08:14 -03:00
|
|
|
bool m_utf16_string_is_in_cache { false };
|
|
|
|
|
|
2025-03-29 21:08:39 -03:00
|
|
|
private:
|
|
|
|
|
friend class RopeString;
|
2026-04-10 12:08:14 -03:00
|
|
|
friend class Substring;
|
2025-03-29 21:08:39 -03:00
|
|
|
|
2026-01-29 08:50:29 -03:00
|
|
|
virtual void finalize() override;
|
2026-05-05 18:59:06 -03:00
|
|
|
virtual size_t external_memory_size() const override;
|
2026-01-29 08:50:29 -03:00
|
|
|
|
2025-03-29 21:08:39 -03:00
|
|
|
explicit PrimitiveString(Utf16String);
|
|
|
|
|
|
2026-06-21 14:02:35 -03:00
|
|
|
void resolve_if_needed() const;
|
2026-04-30 10:01:13 -03:00
|
|
|
Optional<StringView> short_flat_string_storage_view() const;
|
|
|
|
|
static GC::Ptr<PrimitiveString> try_create_short_flat_concatenated_string(VM&, PrimitiveString const& lhs, PrimitiveString const& rhs);
|
2025-03-29 21:08:39 -03:00
|
|
|
};
|
2022-08-05 18:58:47 -03:00
|
|
|
|
2025-03-29 21:08:39 -03:00
|
|
|
class RopeString final : public PrimitiveString {
|
|
|
|
|
GC_CELL(RopeString, PrimitiveString);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(RopeString);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~RopeString() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend class PrimitiveString;
|
|
|
|
|
|
|
|
|
|
explicit RopeString(GC::Ref<PrimitiveString>, GC::Ref<PrimitiveString>);
|
|
|
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2026-06-21 14:02:35 -03:00
|
|
|
void resolve() const;
|
2022-08-05 18:58:47 -03:00
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
mutable GC::Ptr<PrimitiveString> m_lhs;
|
|
|
|
|
mutable GC::Ptr<PrimitiveString> m_rhs;
|
2020-03-11 14:55:01 -03:00
|
|
|
};
|
|
|
|
|
|
2026-04-10 12:08:14 -03:00
|
|
|
class Substring final : public PrimitiveString {
|
|
|
|
|
GC_CELL(Substring, PrimitiveString);
|
|
|
|
|
GC_DECLARE_ALLOCATOR(Substring);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Substring() override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend class PrimitiveString;
|
|
|
|
|
|
|
|
|
|
explicit Substring(GC::Ref<PrimitiveString>, size_t code_unit_offset, size_t code_unit_length);
|
|
|
|
|
|
|
|
|
|
virtual void visit_edges(Visitor&) override;
|
|
|
|
|
|
2026-06-21 14:02:35 -03:00
|
|
|
void resolve() const;
|
2026-04-10 12:08:14 -03:00
|
|
|
|
|
|
|
|
mutable GC::Ptr<PrimitiveString> m_source_string;
|
|
|
|
|
size_t m_code_unit_offset { 0 };
|
|
|
|
|
size_t m_code_unit_length { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-11 14:55:01 -03:00
|
|
|
}
|