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_SYMBOL_TABLE_INL_H
17 #define ECMASCRIPT_SYMBOL_TABLE_INL_H
18
19 #include "symbol_table.h"
20 #include "js_symbol.h"
21 #include "tagged_hash_table-inl.h"
22 #include "libpandabase/utils/hash.h"
23
24 namespace panda::ecmascript {
Hash(const JSTaggedValue & obj)25 uint32_t SymbolTable::Hash(const JSTaggedValue &obj)
26 {
27 if (obj.IsHeapObject()) {
28 if (obj.IsString()) {
29 auto *nameString = static_cast<EcmaString *>(obj.GetTaggedObject());
30 return nameString->GetHashcode();
31 }
32 return JSSymbol::ComputeHash();
33 }
34 UNREACHABLE();
35 }
36
IsMatch(const JSTaggedValue & name,const JSTaggedValue & other)37 bool SymbolTable::IsMatch(const JSTaggedValue &name, const JSTaggedValue &other)
38 {
39 if (name.IsHole() || name.IsUndefined()) {
40 return false;
41 }
42
43 auto *nameString = static_cast<EcmaString *>(name.GetTaggedObject());
44 auto *otherString = static_cast<EcmaString *>(other.GetTaggedObject());
45 return EcmaString::StringsAreEqual(nameString, otherString);
46 }
47
ContainsKey(JSThread * thread,const JSTaggedValue & key)48 bool SymbolTable::ContainsKey(JSThread *thread, const JSTaggedValue &key)
49 {
50 int entry = FindEntry(key);
51 return entry != -1;
52 }
53
GetSymbol(const JSTaggedValue & key)54 JSTaggedValue SymbolTable::GetSymbol(const JSTaggedValue &key)
55 {
56 int entry = FindEntry(key);
57 ASSERT(entry != -1);
58 return GetValue(entry);
59 }
60
FindSymbol(JSThread * thread,const JSTaggedValue & value)61 JSTaggedValue SymbolTable::FindSymbol(JSThread *thread, const JSTaggedValue &value)
62 {
63 JSSymbol *symbol = JSSymbol::Cast(value.GetTaggedObject());
64 JSTaggedValue des = symbol->GetDescription();
65 if (!des.IsUndefined()) {
66 if (ContainsKey(thread, des)) {
67 return des;
68 }
69 }
70 return JSTaggedValue::Undefined();
71 }
72 } // namespace panda::ecmascript
73 #endif // ECMASCRIPT_SYMBOL_TABLE_INL_H