/src/serenity/Userland/Libraries/LibJS/Runtime/PrimitiveString.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/ByteString.h> |
11 | | #include <AK/Optional.h> |
12 | | #include <AK/String.h> |
13 | | #include <AK/StringView.h> |
14 | | #include <LibJS/Forward.h> |
15 | | #include <LibJS/Heap/Cell.h> |
16 | | #include <LibJS/Heap/CellAllocator.h> |
17 | | #include <LibJS/Runtime/Completion.h> |
18 | | #include <LibJS/Runtime/Utf16String.h> |
19 | | #include <LibJS/Runtime/Value.h> |
20 | | |
21 | | namespace JS { |
22 | | |
23 | | class PrimitiveString final : public Cell { |
24 | | JS_CELL(PrimitiveString, Cell); |
25 | | JS_DECLARE_ALLOCATOR(PrimitiveString); |
26 | | |
27 | | public: |
28 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, Utf16String); |
29 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, String); |
30 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, FlyString const&); |
31 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, ByteString); |
32 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, DeprecatedFlyString const&); |
33 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, PrimitiveString&, PrimitiveString&); |
34 | | [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, StringView); |
35 | | |
36 | | virtual ~PrimitiveString(); |
37 | | |
38 | | PrimitiveString(PrimitiveString const&) = delete; |
39 | | PrimitiveString& operator=(PrimitiveString const&) = delete; |
40 | | |
41 | | bool is_empty() const; |
42 | | |
43 | | [[nodiscard]] String utf8_string() const; |
44 | | [[nodiscard]] StringView utf8_string_view() const; |
45 | 20.4k | bool has_utf8_string() const { return m_utf8_string.has_value(); } |
46 | | |
47 | | [[nodiscard]] ByteString byte_string() const; |
48 | 20.4k | bool has_byte_string() const { return m_byte_string.has_value(); } |
49 | | |
50 | | [[nodiscard]] Utf16String utf16_string() const; |
51 | | [[nodiscard]] Utf16View utf16_string_view() const; |
52 | 20.4k | bool has_utf16_string() const { return m_utf16_string.has_value(); } |
53 | | |
54 | | ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const; |
55 | | |
56 | | private: |
57 | | explicit PrimitiveString(PrimitiveString&, PrimitiveString&); |
58 | | explicit PrimitiveString(String); |
59 | | explicit PrimitiveString(ByteString); |
60 | | explicit PrimitiveString(Utf16String); |
61 | | |
62 | | virtual void visit_edges(Cell::Visitor&) override; |
63 | | |
64 | | enum class EncodingPreference { |
65 | | UTF8, |
66 | | UTF16, |
67 | | }; |
68 | | void resolve_rope_if_needed(EncodingPreference) const; |
69 | | |
70 | | mutable bool m_is_rope { false }; |
71 | | |
72 | | mutable GCPtr<PrimitiveString> m_lhs; |
73 | | mutable GCPtr<PrimitiveString> m_rhs; |
74 | | |
75 | | mutable Optional<String> m_utf8_string; |
76 | | mutable Optional<ByteString> m_byte_string; |
77 | | mutable Optional<Utf16String> m_utf16_string; |
78 | | }; |
79 | | |
80 | | } |