1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_JSSYMBOL_H 17 #define ECMASCRIPT_JSSYMBOL_H 18 19 #include "ecmascript/ecma_string.h" 20 #include "ecmascript/js_object.h" 21 22 namespace panda { 23 namespace ecmascript { 24 class JSSymbol : public TaggedObject { 25 public: 26 static constexpr uint32_t IS_PRIVATE = 1U << 0U; 27 static constexpr uint32_t IS_WELL_KNOWN_SYMBOL = 1U << 1U; 28 static constexpr uint32_t IS_IN_PUBLIC_SYMBOL_TABLE = 1U << 2U; 29 static constexpr uint32_t IS_INTERESTING_SYMBOL = 1U << 3U; 30 static constexpr uint32_t IS_PRIVATE_NAME = 1U << 4U; 31 static constexpr uint32_t IS_PRIVATE_BRAND = 1U << 5U; 32 33 static constexpr int SYMBOL_HAS_INSTANCE_TYPE = 0; 34 static constexpr int SYMBOL_TO_PRIMITIVE_TYPE = 1; 35 static constexpr int SYMBOL_DEFAULT_TYPE = 2; 36 37 static constexpr const uint32_t LINEAR_X = 1103515245U; 38 static constexpr const uint32_t LINEAR_Y = 12345U; 39 static constexpr const uint32_t LINEAR_SEED = 987654321U; 40 41 public: 42 CAST_CHECK(JSSymbol, IsSymbol); 43 ComputeHash()44 static inline uint32_t ComputeHash() 45 { 46 uint32_t hashSeed = LINEAR_SEED + std::time(nullptr); 47 uint32_t hash = hashSeed * LINEAR_X + LINEAR_Y; 48 return hash; 49 } 50 IsPrivate()51 bool IsPrivate() const 52 { 53 return (GetFlags() & IS_PRIVATE) != 0U; 54 } 55 SetPrivate(const JSThread * thread)56 void SetPrivate(const JSThread *thread) 57 { 58 SetFlags(GetFlags() | IS_PRIVATE); 59 } 60 IsWellKnownSymbol()61 bool IsWellKnownSymbol() const 62 { 63 return (GetFlags() & IS_WELL_KNOWN_SYMBOL) != 0U; 64 } 65 SetWellKnownSymbol(const JSThread * thread)66 void SetWellKnownSymbol(const JSThread *thread) 67 { 68 SetFlags(GetFlags() | IS_WELL_KNOWN_SYMBOL); 69 } 70 IsInPublicSymbolTable()71 bool IsInPublicSymbolTable() const 72 { 73 return (GetFlags() & IS_IN_PUBLIC_SYMBOL_TABLE) != 0U; 74 } 75 SetInPublicSymbolTable(const JSThread * thread)76 void SetInPublicSymbolTable(const JSThread *thread) 77 { 78 SetFlags(GetFlags() | IS_IN_PUBLIC_SYMBOL_TABLE); 79 } 80 IsInterestingSymbol()81 bool IsInterestingSymbol() const 82 { 83 return (GetFlags() & IS_INTERESTING_SYMBOL) != 0U; 84 } 85 SetInterestingSymbol(const JSThread * thread)86 void SetInterestingSymbol(const JSThread *thread) 87 { 88 SetFlags(GetFlags() | IS_INTERESTING_SYMBOL); 89 } 90 IsPrivateNameSymbol()91 bool IsPrivateNameSymbol() const 92 { 93 return (GetFlags() & IS_PRIVATE_NAME) != 0U; 94 } 95 SetPrivateNameSymbol(const JSThread * thread)96 void SetPrivateNameSymbol(const JSThread *thread) 97 { 98 SetFlags(GetFlags() | IS_PRIVATE_NAME); 99 } 100 Equal(const JSSymbol & src,const JSSymbol & dst)101 static bool Equal(const JSSymbol &src, const JSSymbol &dst) 102 { 103 if (src.GetFlags() != dst.GetFlags()) { 104 return false; 105 } 106 EcmaString *srcString = EcmaString::Cast(src.GetDescription().GetTaggedObject()); 107 EcmaString *dstString = EcmaString::Cast(dst.GetDescription().GetTaggedObject()); 108 return EcmaString::StringsAreEqual(srcString, dstString); 109 } 110 111 public: 112 static constexpr size_t DESCRIPTION_OFFSET = TaggedObjectSize(); 113 ACCESSORS(Description, DESCRIPTION_OFFSET, HASHFIELD_OFFSET) 114 ACCESSORS_PRIMITIVE_FIELD(HashField, uint32_t, HASHFIELD_OFFSET, FLAGS_OFFSET) 115 ACCESSORS_PRIMITIVE_FIELD(Flags, uint32_t, FLAGS_OFFSET, LAST_OFFSET) 116 DEFINE_ALIGN_SIZE(LAST_OFFSET); 117 118 DECL_DUMP() 119 120 DECL_VISIT_OBJECT(DESCRIPTION_OFFSET, HASHFIELD_OFFSET) 121 }; 122 } // namespace ecmascript 123 } // namespace panda 124 #endif // ECMASCRIPT_NAME_H 125