mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:02:44 +00:00 
			
		
		
		
	 a59ebdac2d
			
		
	
	
		a59ebdac2d
		
	
	
	
	
		
			
			It turns out return a ThrowCompletionOr<T const&> is flawed, as the GCC expansion trick used with TRY will always make a copy. PrimitiveString is luckily the only such use case.
		
			
				
	
	
		
			64 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 | |
|  * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/DeprecatedString.h>
 | |
| #include <AK/Optional.h>
 | |
| #include <AK/StringView.h>
 | |
| #include <LibJS/Forward.h>
 | |
| #include <LibJS/Heap/Cell.h>
 | |
| #include <LibJS/Runtime/Completion.h>
 | |
| #include <LibJS/Runtime/Utf16String.h>
 | |
| #include <LibJS/Runtime/Value.h>
 | |
| 
 | |
| namespace JS {
 | |
| 
 | |
| class PrimitiveString final : public Cell {
 | |
|     JS_CELL(PrimitiveString, Cell);
 | |
| 
 | |
| public:
 | |
|     [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, Utf16String);
 | |
|     [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, DeprecatedString);
 | |
|     [[nodiscard]] static NonnullGCPtr<PrimitiveString> create(VM&, PrimitiveString&, PrimitiveString&);
 | |
| 
 | |
|     virtual ~PrimitiveString();
 | |
| 
 | |
|     PrimitiveString(PrimitiveString const&) = delete;
 | |
|     PrimitiveString& operator=(PrimitiveString const&) = delete;
 | |
| 
 | |
|     bool is_empty() const;
 | |
|     u32 hash() const;
 | |
| 
 | |
|     ThrowCompletionOr<DeprecatedString> deprecated_string() const;
 | |
|     bool has_utf8_string() const { return m_utf8_string.has_value(); }
 | |
| 
 | |
|     ThrowCompletionOr<Utf16String> utf16_string() const;
 | |
|     ThrowCompletionOr<Utf16View> utf16_string_view() const;
 | |
|     bool has_utf16_string() const { return m_utf16_string.has_value(); }
 | |
| 
 | |
|     ThrowCompletionOr<Optional<Value>> get(VM&, PropertyKey const&) const;
 | |
| 
 | |
| private:
 | |
|     explicit PrimitiveString(PrimitiveString&, PrimitiveString&);
 | |
|     explicit PrimitiveString(DeprecatedString);
 | |
|     explicit PrimitiveString(Utf16String);
 | |
| 
 | |
|     virtual void visit_edges(Cell::Visitor&) override;
 | |
| 
 | |
|     ThrowCompletionOr<void> resolve_rope_if_needed() const;
 | |
| 
 | |
|     mutable bool m_is_rope { false };
 | |
| 
 | |
|     mutable PrimitiveString* m_lhs { nullptr };
 | |
|     mutable PrimitiveString* m_rhs { nullptr };
 | |
| 
 | |
|     mutable Optional<DeprecatedString> m_utf8_string;
 | |
|     mutable Optional<Utf16String> m_utf16_string;
 | |
| };
 | |
| 
 | |
| }
 |