• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_api_tree_set.h"
17 
18 #include "ecmascript/js_tagged_value.h"
19 #include "ecmascript/tagged_tree-inl.h"
20 
21 namespace panda::ecmascript {
Add(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & value)22 void JSAPITreeSet::Add(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &value)
23 {
24     if (!TaggedTreeSet::IsKey(value.GetTaggedValue())) {
25         THROW_TYPE_ERROR(thread, "the value must be Key of JS");
26     }
27     JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
28 
29     JSTaggedValue newSet = TaggedTreeSet::Add(thread, setHandle, value);
30     RETURN_IF_ABRUPT_COMPLETION(thread);
31     set->SetTreeSet(thread, newSet);
32 }
33 
GetSize() const34 int JSAPITreeSet::GetSize() const
35 {
36     return TaggedTreeSet::Cast(GetTreeSet().GetTaggedObject())->NumberOfElements();
37 }
38 
GetKey(int entry) const39 JSTaggedValue JSAPITreeSet::GetKey(int entry) const
40 {
41     ASSERT_PRINT(entry < GetSize(), "entry must less than capacity");
42     JSTaggedValue key = TaggedTreeSet::Cast(GetTreeSet().GetTaggedObject())->GetKey(entry);
43     return key.IsHole() ? JSTaggedValue::Undefined() : key;
44 }
45 
Delete(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & key)46 bool JSAPITreeSet::Delete(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &key)
47 {
48     JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
49 
50     int entry = TaggedTreeSet::FindEntry(thread, setHandle, key);
51     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
52     if (entry < 0) {
53         return false;
54     }
55     JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
56     set->SetTreeSet(thread, newSet);
57     return true;
58 }
59 
Has(JSThread * thread,const JSHandle<JSAPITreeSet> & set,const JSHandle<JSTaggedValue> & key)60 bool JSAPITreeSet::Has(JSThread *thread, const JSHandle<JSAPITreeSet> &set, const JSHandle<JSTaggedValue> &key)
61 {
62     JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
63     return TaggedTreeSet::FindEntry(thread, setHandle, key) >= 0;
64 }
65 
Clear(const JSThread * thread,const JSHandle<JSAPITreeSet> & set)66 void JSAPITreeSet::Clear(const JSThread *thread, const JSHandle<JSAPITreeSet> &set)
67 {
68     int cap = set->GetSize();
69     JSTaggedValue internal = TaggedTreeSet::Create(thread, cap);
70     set->SetTreeSet(thread, internal);
71 }
72 
PopFirst(JSThread * thread,const JSHandle<JSAPITreeSet> & set)73 JSTaggedValue JSAPITreeSet::PopFirst(JSThread *thread, const JSHandle<JSAPITreeSet> &set)
74 {
75     JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
76     int entry = setHandle->GetMinimum(setHandle->GetRootEntries());
77     if (entry < 0) {
78         return JSTaggedValue::Undefined();
79     }
80     JSHandle<JSTaggedValue> value(thread, setHandle->GetKey(entry));
81     JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
82     set->SetTreeSet(thread, newSet);
83     return value.GetTaggedValue();
84 }
85 
PopLast(JSThread * thread,const JSHandle<JSAPITreeSet> & set)86 JSTaggedValue JSAPITreeSet::PopLast(JSThread *thread, const JSHandle<JSAPITreeSet> &set)
87 {
88     JSHandle<TaggedTreeSet> setHandle(thread, TaggedTreeSet::Cast(set->GetTreeSet().GetTaggedObject()));
89     int entry = setHandle->GetMaximum(setHandle->GetRootEntries());
90     if (entry < 0) {
91         return JSTaggedValue::Undefined();
92     }
93     JSHandle<JSTaggedValue> value(thread, setHandle->GetKey(entry));
94     JSTaggedValue newSet = TaggedTreeSet::Delete(thread, setHandle, entry);
95     set->SetTreeSet(thread, newSet);
96     return value.GetTaggedValue();
97 }
98 }  // namespace panda::ecmascript
99