1 /*
2 * Copyright (c) 2021-2024 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 #include "ecmascript/js_weak_container.h"
17
18 #include "ecmascript/js_tagged_value-inl.h"
19 #include "ecmascript/linked_hash_table.h"
20
21 namespace panda::ecmascript {
Set(JSThread * thread,const JSHandle<JSWeakMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)22 void JSWeakMap::Set(JSThread *thread, const JSHandle<JSWeakMap> &map, const JSHandle<JSTaggedValue> &key,
23 const JSHandle<JSTaggedValue> &value)
24 {
25 [[maybe_unused]] EcmaHandleScope handleScope(thread);
26 if (!LinkedHashMap::IsKey(JSTaggedValue(key.GetTaggedValue().CreateAndGetWeakRef()))) {
27 THROW_TYPE_ERROR(thread, "the value must be Key of JSWeakMap");
28 }
29 JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
30
31 JSHandle<LinkedHashMap> newMap = LinkedHashMap::SetWeakRef(thread, mapHandle, key, value);
32 map->SetLinkedMap(thread, newMap);
33 }
34
Delete(JSThread * thread,const JSHandle<JSWeakMap> & map,const JSHandle<JSTaggedValue> & key)35 bool JSWeakMap::Delete(JSThread *thread, const JSHandle<JSWeakMap> &map, const JSHandle<JSTaggedValue> &key)
36 {
37 JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
38 int entry = mapHandle->FindElement(thread, key.GetTaggedValue());
39 if (entry == -1) {
40 return false;
41 }
42 mapHandle->RemoveEntry(thread, entry);
43
44 JSHandle<LinkedHashMap> newMap = LinkedHashMap::Shrink(thread, mapHandle);
45 map->SetLinkedMap(thread, newMap);
46 return true;
47 }
48
Has(JSThread * thread,JSTaggedValue key) const49 bool JSWeakMap::Has(JSThread *thread, JSTaggedValue key) const
50 {
51 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Has(thread, key);
52 }
53
Get(JSThread * thread,JSTaggedValue key) const54 JSTaggedValue JSWeakMap::Get(JSThread *thread, JSTaggedValue key) const
55 {
56 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Get(thread, key);
57 }
58
GetSize() const59 int JSWeakMap::GetSize() const
60 {
61 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->NumberOfElements();
62 }
63
GetKey(int entry) const64 JSTaggedValue JSWeakMap::GetKey(int entry) const
65 {
66 ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
67 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->GetKey(entry);
68 }
69
GetValue(int entry) const70 JSTaggedValue JSWeakMap::GetValue(int entry) const
71 {
72 ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
73 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->GetValue(entry);
74 }
75
Add(JSThread * thread,const JSHandle<JSWeakSet> & weakSet,const JSHandle<JSTaggedValue> & value)76 void JSWeakSet::Add(JSThread *thread, const JSHandle<JSWeakSet> &weakSet, const JSHandle<JSTaggedValue> &value)
77 {
78 if (!LinkedHashSet::IsKey(value.GetTaggedValue())) {
79 THROW_TYPE_ERROR(thread, "the value must be Key of JSWeakSet");
80 }
81 JSHandle<LinkedHashSet> weakSetHandle(thread, LinkedHashSet::Cast(weakSet->GetLinkedSet().GetTaggedObject()));
82
83 JSHandle<LinkedHashSet> newSet = LinkedHashSet::AddWeakRef(thread, weakSetHandle, value);
84 weakSet->SetLinkedSet(thread, newSet);
85 }
86
Delete(JSThread * thread,const JSHandle<JSWeakSet> & weakSet,const JSHandle<JSTaggedValue> & value)87 bool JSWeakSet::Delete(JSThread *thread, const JSHandle<JSWeakSet> &weakSet, const JSHandle<JSTaggedValue> &value)
88 {
89 JSHandle<LinkedHashSet> weakSetHandle(thread, LinkedHashSet::Cast(weakSet->GetLinkedSet().GetTaggedObject()));
90 int entry = weakSetHandle->FindElement(thread, value.GetTaggedValue());
91 if (entry == -1) {
92 return false;
93 }
94 weakSetHandle->RemoveEntry(thread, entry);
95 JSHandle<LinkedHashSet> newSet = LinkedHashSet::Shrink(thread, weakSetHandle);
96 weakSet->SetLinkedSet(thread, newSet);
97 return true;
98 }
99
Has(JSThread * thread,JSTaggedValue value) const100 bool JSWeakSet::Has(JSThread *thread, JSTaggedValue value) const
101 {
102 return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->Has(thread, value);
103 }
104
GetSize() const105 int JSWeakSet::GetSize() const
106 {
107 return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->NumberOfElements();
108 }
109
GetValue(int entry) const110 JSTaggedValue JSWeakSet::GetValue(int entry) const
111 {
112 ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
113 return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->GetValue(entry);
114 }
115 } // namespace panda::ecmascript
116