• 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_map.h"
17 
18 #include "ecmascript/linked_hash_table.h"
19 
20 namespace panda::ecmascript {
Set(JSThread * thread,const JSHandle<JSMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)21 void JSMap::Set(JSThread *thread, const JSHandle<JSMap> &map, const JSHandle<JSTaggedValue> &key,
22                 const JSHandle<JSTaggedValue> &value)
23 {
24     if (!LinkedHashMap::IsKey(key.GetTaggedValue())) {
25         THROW_TYPE_ERROR(thread, "the value must be Key of JSSet");
26     }
27     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
28 
29     JSHandle<LinkedHashMap> newMap = LinkedHashMap::Set(thread, mapHandle, key, value);
30     map->SetLinkedMap(thread, newMap);
31 }
32 
Delete(const JSThread * thread,const JSHandle<JSMap> & map,const JSHandle<JSTaggedValue> & key)33 bool JSMap::Delete(const JSThread *thread, const JSHandle<JSMap> &map, const JSHandle<JSTaggedValue> &key)
34 {
35     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
36     int entry = mapHandle->FindElement(thread, key.GetTaggedValue());
37     if (entry == -1) {
38         return false;
39     }
40     mapHandle->RemoveEntry(thread, entry);
41     return true;
42 }
43 
Clear(const JSThread * thread,const JSHandle<JSMap> & map)44 void JSMap::Clear(const JSThread *thread, const JSHandle<JSMap> &map)
45 {
46     LinkedHashMap *linkedMap = LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject());
47     JSHandle<LinkedHashMap> mapHandle(thread, LinkedHashMap::Cast(map->GetLinkedMap().GetTaggedObject()));
48     JSHandle<LinkedHashMap> newMap = linkedMap->Clear(thread, mapHandle);
49     map->SetLinkedMap(thread, newMap);
50 }
51 
Has(JSThread * thread,JSTaggedValue key) const52 bool JSMap::Has(JSThread *thread, JSTaggedValue key) const
53 {
54     return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Has(thread, key);
55 }
56 
Get(JSThread * thread,JSTaggedValue key) const57 JSTaggedValue JSMap::Get(JSThread *thread, JSTaggedValue key) const
58 {
59     return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->Get(thread, key);
60 }
61 
GetSize() const62 uint32_t JSMap::GetSize() const
63 {
64     return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->NumberOfElements();
65 }
66 
GetKey(uint32_t entry) const67 JSTaggedValue JSMap::GetKey(uint32_t entry) const
68 {
69     ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
70     return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->GetKey(entry);
71 }
72 
GetValue(uint32_t entry) const73 JSTaggedValue JSMap::GetValue(uint32_t entry) const
74 {
75     ASSERT_PRINT(entry >= 0 && entry < GetSize(), "entry must be non-negative integer less than capacity");
76     return LinkedHashMap::Cast(GetLinkedMap().GetTaggedObject())->GetValue(entry);
77 }
78 }  // namespace panda::ecmascript
79