2020-03-11 17:55:01 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-12-06 22:17:27 +00:00
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
2020-03-11 17:55:01 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-11 17:55:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2023-01-07 17:24:05 +00:00
|
|
|
#include <AK/Optional.h>
|
2023-01-13 17:24:02 +00:00
|
|
|
#include <AK/String.h>
|
2022-03-17 00:26:49 +00:00
|
|
|
#include <AK/StringView.h>
|
2024-11-14 15:01:23 +00:00
|
|
|
#include <LibGC/CellAllocator.h>
|
2022-02-12 08:48:23 +00:00
|
|
|
#include <LibJS/Forward.h>
|
2021-05-17 17:50:20 +00:00
|
|
|
#include <LibJS/Heap/Cell.h>
|
2023-01-07 17:24:05 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-08-09 13:06:45 +00:00
|
|
|
#include <LibJS/Runtime/Utf16String.h>
|
2022-02-13 13:55:23 +00:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-11 17:55:01 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
class PrimitiveString final : public Cell {
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_CELL(PrimitiveString, Cell);
|
|
|
|
GC_DECLARE_ALLOCATOR(PrimitiveString);
|
2022-08-28 20:11:20 +00:00
|
|
|
|
2020-03-11 17:55:01 +00:00
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, Utf16String);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, String);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, FlyString const&);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, ByteString);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, DeprecatedFlyString const&);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, PrimitiveString&, PrimitiveString&);
|
|
|
|
[[nodiscard]] static GC::Ref<PrimitiveString> create(VM&, StringView);
|
2022-12-06 22:17:27 +00:00
|
|
|
|
2020-03-11 17:55:01 +00:00
|
|
|
virtual ~PrimitiveString();
|
|
|
|
|
2021-08-01 23:02:19 +00:00
|
|
|
PrimitiveString(PrimitiveString const&) = delete;
|
|
|
|
PrimitiveString& operator=(PrimitiveString const&) = delete;
|
|
|
|
|
2022-07-17 19:08:53 +00:00
|
|
|
bool is_empty() const;
|
|
|
|
|
2023-08-08 17:17:55 +00:00
|
|
|
[[nodiscard]] String utf8_string() const;
|
|
|
|
[[nodiscard]] StringView utf8_string_view() const;
|
2023-01-13 17:24:02 +00:00
|
|
|
bool has_utf8_string() const { return m_utf8_string.has_value(); }
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
[[nodiscard]] ByteString byte_string() const;
|
|
|
|
bool has_byte_string() const { return m_byte_string.has_value(); }
|
2021-07-20 14:46:53 +00:00
|
|
|
|
2023-08-08 16:54:20 +00:00
|
|
|
[[nodiscard]] Utf16String utf16_string() const;
|
|
|
|
[[nodiscard]] Utf16View utf16_string_view() const;
|
2023-01-07 17:24:05 +00:00
|
|
|
bool has_utf16_string() const { return m_utf16_string.has_value(); }
|
2020-03-11 17:55:01 +00:00
|
|
|
|
2023-01-07 17:24:05 +00:00
|
|
|
ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
|
2022-02-13 13:55:23 +00:00
|
|
|
|
2020-03-11 17:55:01 +00:00
|
|
|
private:
|
2022-08-28 21:51:28 +00:00
|
|
|
explicit PrimitiveString(PrimitiveString&, PrimitiveString&);
|
2023-01-13 17:24:02 +00:00
|
|
|
explicit PrimitiveString(String);
|
2023-12-16 14:19:34 +00:00
|
|
|
explicit PrimitiveString(ByteString);
|
2022-08-28 21:51:28 +00:00
|
|
|
explicit PrimitiveString(Utf16String);
|
|
|
|
|
2022-08-05 21:58:47 +00:00
|
|
|
virtual void visit_edges(Cell::Visitor&) override;
|
2020-03-11 17:55:01 +00:00
|
|
|
|
2023-07-13 13:10:57 +00:00
|
|
|
enum class EncodingPreference {
|
|
|
|
UTF8,
|
|
|
|
UTF16,
|
|
|
|
};
|
2023-08-08 16:34:19 +00:00
|
|
|
void resolve_rope_if_needed(EncodingPreference) const;
|
2022-08-05 21:58:47 +00:00
|
|
|
|
|
|
|
mutable bool m_is_rope { false };
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
mutable GC::Ptr<PrimitiveString> m_lhs;
|
|
|
|
mutable GC::Ptr<PrimitiveString> m_rhs;
|
2022-08-05 21:58:47 +00:00
|
|
|
|
2023-01-13 17:24:02 +00:00
|
|
|
mutable Optional<String> m_utf8_string;
|
2023-12-16 14:19:34 +00:00
|
|
|
mutable Optional<ByteString> m_byte_string;
|
2023-01-07 17:24:05 +00:00
|
|
|
mutable Optional<Utf16String> m_utf16_string;
|
2020-03-11 17:55:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|