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 JSMap");
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(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(JSTaggedValue key) const50 bool JSWeakMap::Has(JSTaggedValue key) const
51 {
52 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Has(key);
53 }
54
Get(JSTaggedValue key) const55 JSTaggedValue JSWeakMap::Get(JSTaggedValue key) const
56 {
57 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Get(key);
58 }
59
GetSize() const60 int JSWeakMap::GetSize() const
61 {
62 return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->NumberOfElements();
63 }
64
Add(JSThread * thread,const JSHandle<JSWeakSet> & weakSet,const JSHandle<JSTaggedValue> & value)65 void JSWeakSet::Add(JSThread *thread, const JSHandle<JSWeakSet> &weakSet, const JSHandle<JSTaggedValue> &value)
66 {
67 if (!LinkedHashSet::IsKey(value.GetTaggedValue())) {
68 THROW_TYPE_ERROR(thread, "the value must be Key of JSWeakSet");
69 }
70 JSHandle<LinkedHashSet> weakSetHandle(thread, LinkedHashSet::Cast(weakSet->GetLinkedSet().GetTaggedObject()));
71
72 JSHandle<LinkedHashSet> newSet = LinkedHashSet::AddWeakRef(thread, weakSetHandle, value);
73 weakSet->SetLinkedSet(thread, newSet);
74 }
75
Delete(JSThread * thread,const JSHandle<JSWeakSet> & weakSet,const JSHandle<JSTaggedValue> & value)76 bool JSWeakSet::Delete(JSThread *thread, const JSHandle<JSWeakSet> &weakSet, const JSHandle<JSTaggedValue> &value)
77 {
78 JSHandle<LinkedHashSet> weakSetHandle(thread, LinkedHashSet::Cast(weakSet->GetLinkedSet().GetTaggedObject()));
79 int entry = weakSetHandle->FindElement(value.GetTaggedValue());
80 if (entry == -1) {
81 return false;
82 }
83 weakSetHandle->RemoveEntry(thread, entry);
84 JSHandle<LinkedHashSet> newSet = LinkedHashSet::Shrink(thread, weakSetHandle);
85 weakSet->SetLinkedSet(thread, newSet);
86 return true;
87 }
88
Has(JSTaggedValue value) const89 bool JSWeakSet::Has(JSTaggedValue value) const
90 {
91 return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->Has(value);
92 }
93
GetSize() const94 int JSWeakSet::GetSize() const
95 {
96 return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->NumberOfElements();
97 }
98 } // namespace panda::ecmascript
99