• 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 "js_set.h"
17 #include "ecmascript/js_tagged_value.h"
18 #include "ecmascript/object_factory.h"
19 #include "linked_hash_table-inl.h"
20 #include "utils/bit_utils.h"
21 
22 namespace panda::ecmascript {
Add(JSThread * thread,const JSHandle<JSSet> & set,const JSHandle<JSTaggedValue> & value)23 void JSSet::Add(JSThread *thread, const JSHandle<JSSet> &set, const JSHandle<JSTaggedValue> &value)
24 {
25     if (!LinkedHashSet::IsKey(value.GetTaggedValue())) {
26         //  throw error
27         THROW_TYPE_ERROR(thread, "the value must be Key of JSSet");
28     }
29     JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
30 
31     JSHandle<LinkedHashSet> newSet = LinkedHashSet::Add(thread, setHandle, value);
32     set->SetLinkedSet(thread, newSet);
33 }
34 
Delete(const JSThread * thread,const JSHandle<JSSet> & set,const JSHandle<JSTaggedValue> & value)35 bool JSSet::Delete(const JSThread *thread, const JSHandle<JSSet> &set, const JSHandle<JSTaggedValue> &value)
36 {
37     JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
38     int entry = setHandle->FindElement(value.GetTaggedValue());
39     if (entry == -1) {
40         return false;
41     }
42     setHandle->RemoveEntry(thread, entry);
43     JSHandle<LinkedHashSet> newSet = LinkedHashSet::Shrink(thread, setHandle);
44     set->SetLinkedSet(thread, newSet);
45     return true;
46 }
47 
Clear(const JSThread * thread,const JSHandle<JSSet> & set)48 void JSSet::Clear(const JSThread *thread, const JSHandle<JSSet> &set)
49 {
50     LinkedHashSet *linkedSet = LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject());
51     linkedSet->Clear(thread);
52 }
53 
Has(JSTaggedValue value) const54 bool JSSet::Has(JSTaggedValue value) const
55 {
56     return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->Has(value);
57 }
58 
GetSize() const59 int JSSet::GetSize() const
60 {
61     return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->NumberOfElements();
62 }
63 
GetValue(int entry) const64 JSTaggedValue JSSet::GetValue(int entry) const
65 {
66     ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
67     return LinkedHashSet::Cast(GetLinkedSet().GetTaggedObject())->GetValue(entry);
68 }
69 }  // namespace panda::ecmascript
70