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