| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 16:53:07 -07:00
										 |  |  |  * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 17:36:12 +02:00
										 |  |  | #include <AK/FlyString.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | #include <LibJS/Runtime/PrimitiveString.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Symbol.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StringOrSymbol { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     StringOrSymbol() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol(char const* chars) | 
					
						
							| 
									
										
										
										
											2021-06-13 12:00:27 +02:00
										 |  |  |         : StringOrSymbol(FlyString(chars)) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol(String const& string) | 
					
						
							| 
									
										
										
										
											2021-06-13 12:00:27 +02:00
										 |  |  |         : StringOrSymbol(FlyString(string)) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-04 17:36:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol(FlyString const& string) | 
					
						
							| 
									
										
										
										
											2020-10-04 17:36:12 +02:00
										 |  |  |         : m_ptr(string.impl()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(!string.is_null()); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |         as_string_impl().ref(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 20:31:05 +02:00
										 |  |  |     ~StringOrSymbol() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |             as_string_impl().unref(); | 
					
						
							| 
									
										
										
										
											2020-08-16 20:31:05 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol(Symbol const* symbol) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         : m_ptr(symbol) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         set_symbol_flag(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol(StringOrSymbol const& other) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         m_ptr = other.m_ptr; | 
					
						
							| 
									
										
										
										
											2020-08-16 20:31:05 +02:00
										 |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |             as_string_impl().ref(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 23:32:54 +02:00
										 |  |  |     StringOrSymbol(StringOrSymbol&& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_ptr = exchange(other.m_ptr, nullptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     ALWAYS_INLINE bool is_valid() const { return m_ptr != nullptr; } | 
					
						
							|  |  |  |     ALWAYS_INLINE bool is_symbol() const { return is_valid() && (bits() & 1ul); } | 
					
						
							|  |  |  |     ALWAYS_INLINE bool is_string() const { return is_valid() && !(bits() & 1ul); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 12:00:27 +02:00
										 |  |  |     ALWAYS_INLINE FlyString as_string() const | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_string()); | 
					
						
							| 
									
										
										
										
											2021-06-13 12:00:27 +02:00
										 |  |  |         return FlyString::from_fly_impl(as_string_impl()); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     ALWAYS_INLINE Symbol const* as_symbol() const | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_symbol()); | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |         return reinterpret_cast<Symbol const*>(bits() & ~1ul); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String to_display_string() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_string()) | 
					
						
							|  |  |  |             return as_string(); | 
					
						
							|  |  |  |         if (is_symbol()) | 
					
						
							|  |  |  |             return as_symbol()->to_string(); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 19:55:21 +02:00
										 |  |  |     Value to_value(VM& vm) const | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2020-09-27 19:55:21 +02:00
										 |  |  |             return js_string(vm, as_string()); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         if (is_symbol()) | 
					
						
							|  |  |  |             return const_cast<Symbol*>(as_symbol()); | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-28 14:33:36 +01:00
										 |  |  |     void visit_edges(Cell::Visitor& visitor) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (is_symbol()) | 
					
						
							|  |  |  |             visitor.visit(const_cast<Symbol*>(as_symbol())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     ALWAYS_INLINE bool operator==(StringOrSymbol const& other) const | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2021-06-13 12:00:27 +02:00
										 |  |  |             return other.is_string() && &as_string_impl() == &other.as_string_impl(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         if (is_symbol()) | 
					
						
							|  |  |  |             return other.is_symbol() && as_symbol() == other.as_symbol(); | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     StringOrSymbol& operator=(StringOrSymbol const& other) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         if (this == &other) | 
					
						
							|  |  |  |             return *this; | 
					
						
							|  |  |  |         m_ptr = other.m_ptr; | 
					
						
							| 
									
										
										
										
											2020-08-16 20:31:05 +02:00
										 |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |             as_string_impl().ref(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 23:32:54 +02:00
										 |  |  |     StringOrSymbol& operator=(StringOrSymbol&& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (this != &other) | 
					
						
							|  |  |  |             m_ptr = exchange(other.m_ptr, nullptr); | 
					
						
							|  |  |  |         return *this; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 17:27:38 +02:00
										 |  |  |     unsigned hash() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_string()) | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |             return as_string_impl().hash(); | 
					
						
							| 
									
										
										
										
											2020-10-04 17:27:38 +02:00
										 |  |  |         return ptr_hash(as_symbol()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | private: | 
					
						
							|  |  |  |     ALWAYS_INLINE u64 bits() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return reinterpret_cast<uintptr_t>(m_ptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE void set_symbol_flag() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |         m_ptr = reinterpret_cast<void const*>(bits() | 1ul); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     ALWAYS_INLINE StringImpl const& as_string_impl() const | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_string()); | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |         return *reinterpret_cast<StringImpl const*>(m_ptr); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     void const* m_ptr { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> { | 
					
						
							| 
									
										
										
										
											2021-06-13 11:49:24 +02:00
										 |  |  |     static unsigned hash(JS::StringOrSymbol const& key) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-04 17:27:38 +02:00
										 |  |  |         return key.hash(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |