• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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/shared_objects/js_shared_set.h"
17 
18 #include "ecmascript/linked_hash_table.h"
19 #include "ecmascript/shared_objects/concurrent_api_scope.h"
20 
21 namespace panda::ecmascript {
Add(JSThread * thread,const JSHandle<JSSharedSet> & set,const JSHandle<JSTaggedValue> & value)22 void JSSharedSet::Add(JSThread *thread, const JSHandle<JSSharedSet> &set, const JSHandle<JSTaggedValue> &value)
23 {
24     if (!value->IsSharedType()) {
25         auto error = containers::ContainerError::BusinessError(thread, containers::ErrorFlag::TYPE_ERROR,
26                                                                "Parameter error. Only accept sendable value.");
27         THROW_NEW_ERROR_AND_RETURN(thread, error);
28     }
29     [[maybe_unused]] ConcurrentApiScope<JSSharedSet, ModType::WRITE> scope(thread,
30         JSHandle<JSTaggedValue>::Cast(set));
31     RETURN_IF_ABRUPT_COMPLETION(thread);
32 
33     JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
34     JSHandle<LinkedHashSet> newSet = LinkedHashSet::Add(thread, setHandle, value);
35     set->SetLinkedSet(thread, newSet);
36 }
37 
Delete(JSThread * thread,const JSHandle<JSSharedSet> & set,const JSHandle<JSTaggedValue> & value)38 bool JSSharedSet::Delete(JSThread *thread, const JSHandle<JSSharedSet> &set, const JSHandle<JSTaggedValue> &value)
39 {
40     [[maybe_unused]] ConcurrentApiScope<JSSharedSet, ModType::WRITE> scope(thread,
41         JSHandle<JSTaggedValue>::Cast(set));
42     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
43     JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
44     int entry = setHandle->FindElement(thread, value.GetTaggedValue());
45     if (entry == -1) {
46         return false;
47     }
48     setHandle->RemoveEntry(thread, entry);
49     return true;
50 }
51 
Clear(JSThread * thread,const JSHandle<JSSharedSet> & set)52 void JSSharedSet::Clear(JSThread *thread, const JSHandle<JSSharedSet> &set)
53 {
54     [[maybe_unused]] ConcurrentApiScope<JSSharedSet, ModType::WRITE> scope(thread,
55         JSHandle<JSTaggedValue>::Cast(set));
56     RETURN_IF_ABRUPT_COMPLETION(thread);
57     JSHandle<LinkedHashSet> setHandle(thread, LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject()));
58     JSHandle<LinkedHashSet> newSet = LinkedHashSet::Clear(thread, setHandle);
59     set->SetLinkedSet(thread, newSet);
60 }
61 
Has(JSThread * thread,const JSHandle<JSSharedSet> & set,JSTaggedValue value)62 bool JSSharedSet::Has(JSThread *thread, const JSHandle<JSSharedSet> &set, JSTaggedValue value)
63 {
64     [[maybe_unused]] ConcurrentApiScope<JSSharedSet> scope(thread, JSHandle<JSTaggedValue>::Cast(set));
65     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
66     return LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject())->Has(thread, value);
67 }
68 
GetSize(JSThread * thread,const JSHandle<JSSharedSet> & set)69 uint32_t JSSharedSet::GetSize(JSThread *thread, const JSHandle<JSSharedSet> &set)
70 {
71     [[maybe_unused]] ConcurrentApiScope<JSSharedSet> scope(thread, JSHandle<JSTaggedValue>::Cast(set));
72     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, 0);
73     return LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject())->NumberOfElements();
74 }
75 
GetValue(JSThread * thread,const JSHandle<JSSharedSet> & set,int entry)76 JSTaggedValue JSSharedSet::GetValue(JSThread *thread, const JSHandle<JSSharedSet> &set, int entry)
77 {
78     [[maybe_unused]] ConcurrentApiScope<JSSharedSet> scope(thread, JSHandle<JSTaggedValue>::Cast(set));
79     ASSERT_PRINT(entry >= 0 && static_cast<uint32_t>(entry) < GetSize(thread, set),
80         "entry must be non-negative integer less than capacity");
81     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
82     return LinkedHashSet::Cast(set->GetLinkedSet().GetTaggedObject())->GetValue(entry);
83 }
84 }  // namespace panda::ecmascript
85