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_GLOBAL_DICTIONARY_H
17#define ECMASCRIPT_GLOBAL_DICTIONARY_H
18
19#include "ecmascript/ecma_string.h"
20#include "ecmascript/ic/property_box.h"
21#include "ecmascript/js_handle.h"
22#include "ecmascript/js_tagged_value.h"
23#include "ecmascript/property_attributes.h"
24#include "ecmascript/ecma_vm.h"
25#include "ecmascript/object_factory.h"
26#include "ecmascript/tagged_hash_table.h"
27
28namespace panda {
29namespace ecmascript {
30class GlobalDictionary : public OrderTaggedHashTable<GlobalDictionary> {
31public:
32    using OrderHashTableT = OrderTaggedHashTable<GlobalDictionary>;
33    inline static int GetKeyIndex(int entry)
34    {
35        return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_KEY_INDEX;
36    }
37    inline static int GetValueIndex(int entry)
38    {
39        return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize() + ENTRY_VALUE_INDEX;
40    }
41    inline static int GetEntryIndex(int entry)
42    {
43        return OrderHashTableT::TABLE_HEADER_SIZE + entry * GetEntrySize();
44    }
45    inline static int GetEntrySize()
46    {
47        return ENTRY_SIZE;
48    }
49    static inline bool IsMatch(const JSTaggedValue &key, const JSTaggedValue &other);
50
51    static inline int Hash(const JSTaggedValue &key);
52    inline static void InvalidatePropertyBox(JSThread *thread, const JSHandle<GlobalDictionary> &dictHandle, int entry);
53
54    inline static void InvalidateAndReplaceEntry(JSThread *thread, const JSHandle<GlobalDictionary> &dictHandle,
55                                                 int entry, const JSHandle<JSTaggedValue> &oldValue);
56
57    inline bool IsValidateBox(int entry) const;
58
59    inline PropertyBox *GetBox(int entry) const;
60
61    inline JSTaggedValue GetValue(int entry) const;
62
63    inline PropertyAttributes GetAttributes(int entry) const;
64
65    inline void SetEntry(const JSThread *thread, int entry, const JSTaggedValue &key, const JSTaggedValue &value,
66                         const PropertyAttributes &detail);
67
68    inline void ClearEntry(const JSThread *thread, int entry);
69
70    inline void UpdateValueAndAttributes(const JSThread *thread, int entry, const JSTaggedValue &value,
71                                         const PropertyAttributes &metaData);
72
73    inline void UpdateValue(const JSThread *thread, int entry, const JSTaggedValue &value);
74
75    inline void SetAttributes(const JSThread *thread, int entry, const PropertyAttributes &metaData);
76
77    inline void GetAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray) const;
78    inline void GetAllKeysByFilter(const JSThread *thread,
79        uint32_t &keyArrayEffectivelength, TaggedArray *keyArray, uint32_t filter) const;
80
81    inline void GetEnumAllKeys(const JSThread *thread, int offset, TaggedArray *keyArray, uint32_t *keys) const;
82
83    inline std::pair<uint32_t, uint32_t> GetNumOfEnumKeys() const;
84
85    static bool inline CompKey(const std::pair<JSTaggedValue, uint32_t> &a,
86                               const std::pair<JSTaggedValue, uint32_t> &b);
87
88    DECL_DUMP()
89
90public:
91    static constexpr int ENTRY_KEY_INDEX = 0;
92    static constexpr int ENTRY_VALUE_INDEX = 1;
93    static constexpr int ENTRY_DETAILS_INDEX = 2;
94    static constexpr int ENTRY_SIZE = 3;
95};
96}  // namespace ecmascript
97}  // namespace panda
98#endif
99