• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         CString errorMsg =
36             "The type of \"value\" must be Key of JS. Received value is: " + ConvertToString(*result);
37         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
38         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
39     }
40     TaggedHashArray *hashArray = TaggedHashArray::Cast(GetTable().GetTaggedObject());
41     int hash = TaggedNode::Hash(value);
42     return JSTaggedValue(!(hashArray->GetNode(thread, hash, value).IsHole()));
43 }
44 
Add(JSThread * thread,JSHandle<JSAPIHashSet> hashSet,JSHandle<JSTaggedValue> value)45 void JSAPIHashSet::Add(JSThread *thread, JSHandle<JSAPIHashSet> hashSet, JSHandle<JSTaggedValue> value)
46 {
47     if (!TaggedHashArray::IsKey(value.GetTaggedValue())) {
48         JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue());
49         CString errorMsg =
50             "The type of \"value\" must be Key of JS. Received value is: " + ConvertToString(*result);
51         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
52         THROW_NEW_ERROR_AND_RETURN(thread, error);
53     }
54     JSHandle<TaggedHashArray> hashArray(thread, hashSet->GetTable());
55     int hash = TaggedNode::Hash(value.GetTaggedValue());
56     JSHandle<JSTaggedValue> nullHandle(thread, JSTaggedValue::Null());
57     JSTaggedValue setValue = TaggedHashArray::SetVal(thread, hashArray, hash, value, nullHandle);
58     uint32_t nodeNum = hashSet->GetSize();
59     if (!setValue.IsUndefined()) {
60         hashSet->SetSize(++nodeNum);
61     }
62     uint32_t tableLength = hashArray->GetLength() * TaggedHashArray::DEFAULT_LOAD_FACTOR;
63     if (nodeNum > tableLength) {
64         hashArray = TaggedHashArray::Resize(thread, hashArray, hashArray->GetLength());
65     }
66     hashSet->SetTable(thread, hashArray);
67 }
68 
Clear(JSThread * thread)69 void JSAPIHashSet::Clear(JSThread *thread)
70 {
71     TaggedHashArray *hashArray = TaggedHashArray::Cast(GetTable().GetTaggedObject());
72     uint32_t nodeLength = GetSize();
73     if (nodeLength > 0) {
74         hashArray->Clear(thread);
75         SetSize(0);
76     }
77 }
78 
Remove(JSThread * thread,JSHandle<JSAPIHashSet> hashSet,JSTaggedValue key)79 JSTaggedValue JSAPIHashSet::Remove(JSThread *thread, JSHandle<JSAPIHashSet> hashSet, JSTaggedValue key)
80 {
81     if (!TaggedHashArray::IsKey(key)) {
82         JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key);
83         CString errorMsg =
84             "The type of \"key\" must be not null. Received value is: " + ConvertToString(*result);
85         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
86         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
87     }
88 
89     JSHandle<TaggedHashArray> hashArray(thread, hashSet->GetTable());
90     uint32_t nodeNum = hashSet->GetSize();
91     if (nodeNum == 0) {
92         return JSTaggedValue::False();
93     }
94     int hash = TaggedNode::Hash(key);
95     JSTaggedValue removeValue = hashArray->RemoveNode(thread, hash, key);
96     if (removeValue.IsHole()) {
97         return JSTaggedValue::False();
98     }
99     hashSet->SetSize(--nodeNum);
100     uint32_t length = hashArray->GetLength();
101     uint32_t index = (length - 1) & hash;
102     JSTaggedValue rootVa = hashArray->Get(index);
103     if (rootVa.IsRBTreeNode()) {
104         uint32_t numTreeNode = RBTreeNode::Count(rootVa);
105         if (numTreeNode < TaggedHashArray::UNTREEIFY_THRESHOLD) {
106             JSHandle<RBTreeNode> root(thread, rootVa);
107             JSHandle<LinkedNode> head = RBTreeNode::Detreeing(thread, root);
108             hashArray->Set(thread, index, head);
109         }
110     }
111     return JSTaggedValue::True();
112 }
113 }
114