| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #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: | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     static StringOrSymbol from_value(GlobalObject& global_object, Value value) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-06 18:54:45 +00:00
										 |  |  |         if (value.is_empty()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         if (value.is_symbol()) | 
					
						
							|  |  |  |             return &value.as_symbol(); | 
					
						
							| 
									
										
										
										
											2020-11-06 18:54:45 +00:00
										 |  |  |         auto string = value.to_string(global_object); | 
					
						
							|  |  |  |         if (string.is_null()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							|  |  |  |         return string; | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol(const char* chars) | 
					
						
							|  |  |  |         : m_ptr(StringImpl::create(chars).leak_ref()) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol(const String& string) | 
					
						
							| 
									
										
										
										
											2020-10-04 17:21:46 +02:00
										 |  |  |         : m_ptr(string.impl()) | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											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-10-04 17:36:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol(const FlyString& string) | 
					
						
							|  |  |  |         : 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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     StringOrSymbol(const Symbol* symbol) | 
					
						
							|  |  |  |         : m_ptr(symbol) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         set_symbol_flag(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol(const StringOrSymbol& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         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); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE String as_string() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_string()); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |         return as_string_impl(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE const Symbol* as_symbol() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_symbol()); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |         return reinterpret_cast<const Symbol*>(bits() & ~1ul); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE bool operator==(const StringOrSymbol& other) const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |         if (is_string()) | 
					
						
							|  |  |  |             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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     StringOrSymbol& operator=(const StringOrSymbol& other) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         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() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_ptr = reinterpret_cast<const void*>(bits() | 1ul); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |     ALWAYS_INLINE const StringImpl& as_string_impl() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(is_string()); | 
					
						
							| 
									
										
										
										
											2020-10-05 11:25:11 -04:00
										 |  |  |         return *reinterpret_cast<const StringImpl*>(m_ptr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     const void* m_ptr { nullptr }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Traits<JS::StringOrSymbol> : public GenericTraits<JS::StringOrSymbol> { | 
					
						
							|  |  |  |     static unsigned hash(const JS::StringOrSymbol& key) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-04 17:27:38 +02:00
										 |  |  |         return key.hash(); | 
					
						
							| 
									
										
										
										
											2020-07-07 21:38:46 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |