1 /*
2 * Copyright (c) 2022 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_api/js_api_hashset.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/tagged_hash_array.h"
20 #include "ecmascript/tagged_node.h"
21 #include "ecmascript/tagged_queue.h"
22
23 namespace panda::ecmascript {
24 using ContainerError = containers::ContainerError;
25 using ErrorFlag = containers::ErrorFlag;
IsEmpty()26 JSTaggedValue JSAPIHashSet::IsEmpty()
27 {
28 return JSTaggedValue(GetSize() == 0);
29 }
30
Has(JSThread * thread,JSTaggedValue value)31 JSTaggedValue JSAPIHashSet::Has(JSThread *thread, JSTaggedValue value)
32 {
33 if (!TaggedHashArray::IsKey(value)) {
34 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value);
35 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
36 CString errorMsg =
37 "The type of \"value\" must be Key of JS. Received value is: " + ConvertToString(*result);
38 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
39 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
40 }
41 TaggedHashArray *hashArray = TaggedHashArray::Cast(GetTable().GetTaggedObject());
42 int hash = TaggedNode::Hash(thread, value);
43 return JSTaggedValue(!(hashArray->GetNode(thread, hash, value).IsHole()));
44 }
45
Add(JSThread * thread,JSHandle<JSAPIHashSet> hashSet,JSHandle<JSTaggedValue> value)46 void JSAPIHashSet::Add(JSThread *thread, JSHandle<JSAPIHashSet> hashSet, JSHandle<JSTaggedValue> value)
47 {
48 if (!TaggedHashArray::IsKey(value.GetTaggedValue())) {
49 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue());
50 CString errorMsg =
51 "The type of \"value\" must be Key of JS. Received value is: " + ConvertToString(*result);
52 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
53 THROW_NEW_ERROR_AND_RETURN(thread, error);
54 }
55 JSHandle<TaggedHashArray> hashArray(thread, hashSet->GetTable());
56 int hash = TaggedNode::Hash(thread, value.GetTaggedValue());
57 JSHandle<JSTaggedValue> nullHandle(thread, JSTaggedValue::Null());
58 JSTaggedValue setValue = TaggedHashArray::SetVal(thread, hashArray, hash, value, nullHandle);
59 uint32_t nodeNum = hashSet->GetSize();
60 if (!setValue.IsUndefined()) {
61 hashSet->SetSize(++nodeNum);
62 }
63 uint32_t tableLength = hashArray->GetLength() * TaggedHashArray::DEFAULT_LOAD_FACTOR;
64 if (nodeNum > tableLength) {
65 hashArray = TaggedHashArray::Resize(thread, hashArray, hashArray->GetLength());
66 }
67 hashSet->SetTable(thread, hashArray);
68 }
69
Clear(JSThread * thread)70 void JSAPIHashSet::Clear(JSThread *thread)
71 {
72 TaggedHashArray *hashArray = TaggedHashArray::Cast(GetTable().GetTaggedObject());
73 uint32_t nodeLength = GetSize();
74 if (nodeLength > 0) {
75 hashArray->Clear(thread);
76 SetSize(0);
77 }
78 }
79
Remove(JSThread * thread,JSHandle<JSAPIHashSet> hashSet,JSTaggedValue key)80 JSTaggedValue JSAPIHashSet::Remove(JSThread *thread, JSHandle<JSAPIHashSet> hashSet, JSTaggedValue key)
81 {
82 if (!TaggedHashArray::IsKey(key)) {
83 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key);
84 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
85 CString errorMsg =
86 "The type of \"key\" must be not null. Received value is: " + ConvertToString(*result);
87 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
88 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
89 }
90
91 JSHandle<TaggedHashArray> hashArray(thread, hashSet->GetTable());
92 uint32_t nodeNum = hashSet->GetSize();
93 if (nodeNum == 0) {
94 return JSTaggedValue::False();
95 }
96 int hash = TaggedNode::Hash(thread, key);
97 JSTaggedValue removeValue = hashArray->RemoveNode(thread, hash, key);
98 if (removeValue.IsHole()) {
99 return JSTaggedValue::False();
100 }
101 hashSet->SetSize(--nodeNum);
102 uint32_t length = hashArray->GetLength();
103 uint32_t index = (length - 1) & hash;
104 JSTaggedValue rootVa = hashArray->Get(index);
105 if (rootVa.IsRBTreeNode()) {
106 uint32_t numTreeNode = RBTreeNode::Count(rootVa);
107 if (numTreeNode < TaggedHashArray::UNTREEIFY_THRESHOLD) {
108 JSHandle<RBTreeNode> root(thread, rootVa);
109 JSHandle<LinkedNode> head = RBTreeNode::Detreeing(thread, root);
110 hashArray->Set(thread, index, head);
111 }
112 }
113 return JSTaggedValue::True();
114 }
115 }
116