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