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_tree_set.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/tagged_tree.h"
21
22 namespace panda::ecmascript {
23 using ContainerError = containers::ContainerError;
24 using ErrorFlag = containers::ErrorFlag;
Add(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & value)25 void JSAPITreeSet::Add(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &value)
26 {
27 if (!TaggedTreeSet::IsKey(value.GetTaggedValue())) {
28 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, value.GetTaggedValue());
29 CString errorMsg =
30 "The type of \"value\" must be Key of JS. Received value is: " + ConvertToString(*result);
31 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
32 THROW_NEW_ERROR_AND_RETURN(thread, error);
33 }
34 JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
35
36 JSTaggedValue newSet = TaggedTreeSet::Add(thread, setHandle, value);
37 RETURN_IF_ABRUPT_COMPLETION(thread);
38 set->SetTreeSet(thread, newSet);
39 }
40
GetSize() const41 int JSAPITreeSet::GetSize() const
42 {
43 return TaggedTreeSet::Cast(GetTreeSet().GetTaggedObject())->NumberOfElements();
44 }
45
GetKey(int entry) const46 JSTaggedValue JSAPITreeSet::GetKey(int entry) const
47 {
48 ASSERT_PRINT(entry < GetSize(), "entry must less than capacity");
49 JSTaggedValue key = TaggedTreeSet::Cast(GetTreeSet().GetTaggedObject())->GetKey(entry);
50 return key.IsHole() ? JSTaggedValue::Undefined() : key;
51 }
52
Delete(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & key)53 bool JSAPITreeSet::Delete(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &key)
54 {
55 JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
56
57 int entry = TaggedTreeSet::FindEntry(thread, setHandle, key);
58 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
59 if (entry < 0) {
60 return false;
61 }
62 JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
63 set->SetTreeSet(thread, newSet);
64 return true;
65 }
66
Has(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & key)67 bool JSAPITreeSet::Has(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &key)
68 {
69 JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
70 return TaggedTreeSet::FindEntry(thread, setHandle, key) >= 0;
71 }
72
Clear(const JSThread * thread,const JSHandle<JSAPITreeSet> & set)73 void JSAPITreeSet::Clear(const JSThread *thread, const JSHandle<JSAPITreeSet> &set)
74 {
75 int cap = set->GetSize();
76 JSTaggedValue internal = TaggedTreeSet::Create(thread, cap);
77 set->SetTreeSet(thread, internal);
78 }
79
PopFirst(JSThread * thread,const JSHandle<JSAPITreeSet> & set)80 JSTaggedValue JSAPITreeSet::PopFirst(JSThread *thread, const JSHandle<JSAPITreeSet> &set)
81 {
82 JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
83 int entry = setHandle->GetMinimum(setHandle->GetRootEntries());
84 if (entry < 0) {
85 return JSTaggedValue::Undefined();
86 }
87 JSHandle<JSTaggedValue> value(thread, setHandle->GetKey(entry));
88 JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
89 set->SetTreeSet(thread, newSet);
90 return value.GetTaggedValue();
91 }
92
PopLast(JSThread * thread,const JSHandle<JSAPITreeSet> & set)93 JSTaggedValue JSAPITreeSet::PopLast(JSThread *thread, const JSHandle<JSAPITreeSet> &set)
94 {
95 JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
96 int entry = setHandle->GetMaximum(setHandle->GetRootEntries());
97 if (entry < 0) {
98 return JSTaggedValue::Undefined();
99 }
100 JSHandle<JSTaggedValue> value(thread, setHandle->GetKey(entry));
101 JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
102 set->SetTreeSet(thread, newSet);
103 return value.GetTaggedValue();
104 }
105 } // namespace panda::ecmascript
106