• 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_map.h"
17 
18 #include "ecmascript/linked_hash_table.h"
19 #include "ecmascript/shared_objects/concurrent_api_scope.h"
20 
21 namespace panda::ecmascript {
Set(JSThread * thread,const JSHandle<JSSharedMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)22 void JSSharedMap::Set(JSThread *thread, const JSHandle<JSSharedMap> &map,
23                       const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value)
24 {
25     if (!key->IsSharedType() || !value->IsSharedType()) {
26         auto error = containers::ContainerError::BusinessError(thread, containers::ErrorFlag::TYPE_ERROR,
27                                                                "Parameter error. Only accept sendable value.");
28         THROW_NEW_ERROR_AND_RETURN(thread, error);
29     }
30     [[maybe_unused]] ConcurrentApiScope<JSSharedMap, ModType::WRITE> scope(thread,
31         JSHandle<JSTaggedValue>::Cast(map));
32     RETURN_IF_ABRUPT_COMPLETION(thread);
33 
34     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject()));
35     JSHandle<LinkedHashMap> newMap = LinkedHashMap::Set(thread, mapHandle, key, value);
36     map->SetLinkedMap(thread, newMap);
37 }
38 
Delete(JSThread * thread,const JSHandle<JSSharedMap> & map,const JSHandle<JSTaggedValue> & key)39 bool JSSharedMap::Delete(JSThread *thread, const JSHandle<JSSharedMap> &map, const JSHandle<JSTaggedValue> &key)
40 {
41     [[maybe_unused]] ConcurrentApiScope<JSSharedMap, ModType::WRITE> scope(thread,
42         JSHandle<JSTaggedValue>::Cast(map));
43     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
44     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject()));
45     int entry = mapHandle->FindElement(thread, key.GetTaggedValue());
46     if (entry == -1) {
47         return false;
48     }
49     mapHandle->RemoveEntry(thread, entry);
50     return true;
51 }
52 
Clear(JSThread * thread,const JSHandle<JSSharedMap> & map)53 void JSSharedMap::Clear(JSThread *thread, const JSHandle<JSSharedMap> &map)
54 {
55     [[maybe_unused]] ConcurrentApiScope<JSSharedMap, ModType::WRITE> scope(thread,
56         JSHandle<JSTaggedValue>::Cast(map));
57     RETURN_IF_ABRUPT_COMPLETION(thread);
58     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject()));
59     JSHandle<LinkedHashMap> newMap = LinkedHashMap::Clear(thread, mapHandle);
60     map->SetLinkedMap(thread, newMap);
61 }
62 
Has(JSThread * thread,const JSHandle<JSSharedMap> & map,JSTaggedValue key)63 bool JSSharedMap::Has(JSThread *thread, const JSHandle<JSSharedMap> &map, JSTaggedValue key)
64 {
65     [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(map));
66     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
67     return LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject())->Has(thread, key);
68 }
69 
Get(JSThread * thread,const JSHandle<JSSharedMap> & map,JSTaggedValue key)70 JSTaggedValue JSSharedMap::Get(JSThread *thread, const JSHandle<JSSharedMap> &map, JSTaggedValue key)
71 {
72     [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(map));
73     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
74     return LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject())->Get(thread, key);
75 }
76 
GetSize(JSThread * thread,const JSHandle<JSSharedMap> & map)77 uint32_t JSSharedMap::GetSize(JSThread *thread, const JSHandle<JSSharedMap> &map)
78 {
79     [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(map));
80     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, 0);
81     return LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject())->NumberOfElements();
82 }
83 
GetKey(JSThread * thread,const JSHandle<JSSharedMap> & map,uint32_t entry)84 JSTaggedValue JSSharedMap::GetKey(JSThread *thread, const JSHandle<JSSharedMap> &map, uint32_t entry)
85 {
86     [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(map));
87     ASSERT_PRINT(entry >= 0 && entry < GetSize(thread, map), "entry must be non-negative integer less than capacity");
88     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
89     return LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject())->GetKey(thread, entry);
90 }
91 
GetValue(JSThread * thread,const JSHandle<JSSharedMap> & map,uint32_t entry)92 JSTaggedValue JSSharedMap::GetValue(JSThread *thread, const JSHandle<JSSharedMap> &map, uint32_t entry)
93 {
94     [[maybe_unused]] ConcurrentApiScope<JSSharedMap> scope(thread, JSHandle<JSTaggedValue>::Cast(map));
95     ASSERT_PRINT(entry >= 0 && entry < GetSize(thread, map), "entry must be non-negative integer less than capacity");
96     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Undefined());
97     return LinkedHashMap::Cast(map->GetLinkedMap(thread).GetTaggedObject())->GetValue(thread, entry);
98 }
99 }  // namespace panda::ecmascript
100