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