• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TAGGED_DICTIONARY_H
17 #define ECMASCRIPT_TAGGED_DICTIONARY_H
18 
19 #include "ecmascript/js_tagged_value-inl.h"
20 #include "ecmascript/tagged_array-inl.h"
21 #include "ecmascript/tagged_hash_table.h"
22 
23 namespace panda::ecmascript {
24 class NameDictionary : public OrderTaggedHashTable<NameDictionary> {
25 public:
26     using OrderHashTableT = OrderTaggedHashTable<NameDictionary>;
GetKeyIndex(int entry)27     inline static int GetKeyIndex(int entry)
28     {
29         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
30     }
GetValueIndex(int entry)31     inline static int GetValueIndex(int entry)
32     {
33         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
34     }
GetEntryIndex(int entry)35     inline static int GetEntryIndex(int entry)
36     {
37         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize();
38     }
GetEntrySize()39     inline static int GetEntrySize()
40     {
41         return ENTRY_SIZE;
42     }
43     static int Hash(const JSTaggedValue &key);
44     static int Hash(const uint8_t* str, int strSize);
45 
46     static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other);
47     static bool IsMatch(const uint8_t* str, int size, const JSTaggedValue &other);
48 
49     static JSHandle<NameDictionary> Create(const JSThread *thread,
50                                   int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER);
51     // Returns the property metaData for the property at entry.
52     PropertyAttributes GetAttributes(int entry) const;
53     void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData);
54     void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value,
55                   const PropertyAttributes &metaData);
56     void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value,
57                                   const PropertyAttributes &metaData);
58     void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value);
59     void UpdateAttributes(int entry, const PropertyAttributes &metaData);
60     void ClearEntry(const JSThread *thread, int entry);
61     void GetAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray) const;
62     void GetAllKeysByFilter(const JSThread *thread, uint32_t &keyArrayEffectivelength,
63         TaggedArray *keyArray, uint32_t filter) const;
64     std::pair<uint32_t, uint32_t> GetNumOfEnumKeys() const;
65     void GetAllEnumKeys(JSThread *thread, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys,
66                         JSHandle<TaggedQueue> shadowQueue, int32_t lastLength) const;
67     void GetAllEnumKeys(JSThread *thread, int offset, JSHandle<TaggedArray> keyArray, uint32_t *keys) const;
CompHandleKey(const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & a,const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & b)68     static inline bool CompHandleKey(const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &a,
69                                      const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &b)
70     {
71         return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder();
72     }
CompKey(const std::pair<JSTaggedValue,PropertyAttributes> & a,const std::pair<JSTaggedValue,PropertyAttributes> & b)73     static inline bool CompKey(const std::pair<JSTaggedValue, PropertyAttributes> &a,
74                                const std::pair<JSTaggedValue, PropertyAttributes> &b)
75     {
76         return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder();
77     }
CompIndex(const PropertyAttributes & a,const PropertyAttributes & b)78     static inline bool CompIndex(const PropertyAttributes &a,
79                                  const PropertyAttributes &b)
80     {
81         return a.GetDictionaryOrder() < b.GetDictionaryOrder();
82     }
83     DECL_DUMP()
84 
85     static constexpr int ENTRY_KEY_INDEX = 0;
86     static constexpr int ENTRY_VALUE_INDEX = 1;
87     static constexpr int ENTRY_DETAILS_INDEX = 2;
88     static constexpr int ENTRY_SIZE = 3;
89 };
90 
91 class NumberDictionary : public OrderTaggedHashTable<NumberDictionary> {
92 public:
93     using OrderHashTableT = OrderTaggedHashTable<NumberDictionary>;
GetKeyIndex(int entry)94     inline static int GetKeyIndex(int entry)
95     {
96         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
97     }
GetValueIndex(int entry)98     inline static int GetValueIndex(int entry)
99     {
100         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
101     }
GetEntryIndex(int entry)102     inline static int GetEntryIndex(int entry)
103     {
104         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize();
105     }
GetEntrySize()106     inline static int GetEntrySize()
107     {
108         return ENTRY_SIZE;
109     }
110     static int Hash(const JSTaggedValue &key);
111     static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other);
112     static JSHandle<NumberDictionary> Create(const JSThread *thread,
113                                     int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER);
114     // Returns the property metaData for the property at entry.
115     PropertyAttributes GetAttributes(int entry) const;
116     void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData);
117     void SetEntry([[maybe_unused]] const JSThread *thread, int entry, const JSTaggedValue &key,
118                   const JSTaggedValue &value, const PropertyAttributes &metaData);
119     void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value,
120                                   const PropertyAttributes &metaData);
121     void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value);
122     void UpdateAttributes(int entry, const PropertyAttributes &metaData);
123     void ClearEntry(const JSThread *thread, int entry);
124 
125     static void GetAllKeys(const JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset,
126                            const JSHandle<TaggedArray> &keyArray);
127     static void GetAllKeysByFilter(const JSThread *thread, const JSHandle<NumberDictionary> &obj,
128         uint32_t &keyArrayEffectivelength, const JSHandle<TaggedArray> &keyArray, uint32_t filter);
129     static void GetAllEnumKeys(JSThread *thread, const JSHandle<NumberDictionary> &obj, int offset,
130                                const JSHandle<TaggedArray> &keyArray, uint32_t *keys, int32_t lastLength = -1);
CompKey(const JSTaggedValue & a,const JSTaggedValue & b)131     static inline bool CompKey(const JSTaggedValue &a, const JSTaggedValue &b)
132     {
133         ASSERT(a.IsNumber() && b.IsNumber());
134         return a.GetNumber() < b.GetNumber();
135     }
136     DECL_DUMP()
137 
138     static constexpr int ENTRY_KEY_INDEX = 0;
139     static constexpr int ENTRY_VALUE_INDEX = 1;
140     static constexpr int ENTRY_DETAILS_INDEX = 2;
141     static constexpr int ENTRY_SIZE = 3;
142 };
143 
144 class PointerToIndexDictionary : public OrderTaggedHashTable<PointerToIndexDictionary> {
145 public:
146     using OrderHashTableT = OrderTaggedHashTable<PointerToIndexDictionary>;
GetKeyIndex(int entry)147     inline static int GetKeyIndex(int entry)
148     {
149         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
150     }
151 
GetValueIndex(int entry)152     inline static int GetValueIndex(int entry)
153     {
154         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
155     }
156 
GetEntryIndex(int entry)157     inline static int GetEntryIndex(int entry)
158     {
159         return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize();
160     }
161 
GetEntrySize()162     inline static int GetEntrySize()
163     {
164         return ENTRY_SIZE;
165     }
166 
SetEntry(const JSThread * thread,int entry,const JSTaggedValue & key,const JSTaggedValue & value)167     inline void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value)
168     {
169         SetKey(thread, entry, key);
170         SetValue(thread, entry, value);
171     }
172 
Hash(const JSTaggedValue & key)173     static int32_t Hash(const JSTaggedValue &key)
174     {
175         return static_cast<int32_t>(key.GetRawData());
176     }
177 
IsMatch(const JSTaggedValue & key,const JSTaggedValue & other)178     static bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other)
179     {
180         return key == other;
181     }
182     static JSHandle<PointerToIndexDictionary> Create(const JSThread *thread,
183                                         int numberOfElements = OrderHashTableT::DEFAULT_ELEMENTS_NUMBER);
184     static JSHandle<PointerToIndexDictionary> PutIfAbsent(
185                                         const JSThread *thread, const JSHandle<PointerToIndexDictionary> &dictionary,
186                                         const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value);
187 
188     // DECL_DUMP()
189 
190     static constexpr int ENTRY_KEY_INDEX = 0;
191     static constexpr int ENTRY_VALUE_INDEX = 1;
192     static constexpr int ENTRY_SIZE = 2;
193 };
194 }  // namespace panda::ecmascript
195 #endif  // ECMASCRIPT_NEW_DICTIONARY_H
196