1 /*
2 * Copyright (c) 2022 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_api/js_api_tree_map.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/tagged_tree.h"
20
21 namespace panda::ecmascript {
22 using ContainerError = containers::ContainerError;
23 using ErrorFlag = containers::ErrorFlag;
Set(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)24 void JSAPITreeMap::Set(JSThread *thread, const JSHandle<JSAPITreeMap> &map, const JSHandle<JSTaggedValue> &key,
25 const JSHandle<JSTaggedValue> &value)
26 {
27 if (!TaggedTreeMap::IsKey(key.GetTaggedValue())) {
28 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
29 RETURN_IF_ABRUPT_COMPLETION(thread);
30 CString errorMsg =
31 "The type of \"key\" must be not null. Received value is: " + ConvertToString(thread, *result);
32 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
33 THROW_NEW_ERROR_AND_RETURN(thread, error);
34 }
35 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject()));
36
37 JSTaggedValue newMap = TaggedTreeMap::Set(thread, mapHandle, key, value);
38 RETURN_IF_ABRUPT_COMPLETION(thread);
39 map->SetTreeMap(thread, newMap);
40 }
41
Get(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key)42 JSTaggedValue JSAPITreeMap::Get(JSThread *thread, const JSHandle<JSAPITreeMap> &map, const JSHandle<JSTaggedValue> &key)
43 {
44 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject()));
45 return TaggedTreeMap::Get(thread, mapHandle, key);
46 }
47
GetSize(const JSThread * thread) const48 int JSAPITreeMap::GetSize(const JSThread *thread) const
49 {
50 return TaggedTreeMap::Cast(GetTreeMap(thread).GetTaggedObject())->NumberOfElements();
51 }
52
GetKey(const JSThread * thread,int entry) const53 JSTaggedValue JSAPITreeMap::GetKey(const JSThread *thread, int entry) const
54 {
55 ASSERT_PRINT(entry < GetSize(thread), "entry must less than capacity");
56 JSTaggedValue key = TaggedTreeMap::Cast(GetTreeMap(thread).GetTaggedObject())->GetKey(thread, entry);
57 return key.IsHole() ? JSTaggedValue::Undefined() : key;
58 }
59
GetValue(const JSThread * thread,int entry) const60 JSTaggedValue JSAPITreeMap::GetValue(const JSThread *thread, int entry) const
61 {
62 ASSERT_PRINT(entry < GetSize(thread), "entry must less than capacity");
63 JSTaggedValue value = TaggedTreeMap::Cast(GetTreeMap(thread).GetTaggedObject())->GetValue(thread, entry);
64 return value.IsHole() ? JSTaggedValue::Undefined() : value;
65 }
66
Delete(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key)67 JSTaggedValue JSAPITreeMap::Delete(JSThread *thread, const JSHandle<JSAPITreeMap> &map,
68 const JSHandle<JSTaggedValue> &key)
69 {
70 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject()));
71 int entry = TaggedTreeMap::FindEntry(thread, mapHandle, key);
72 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
73 if (entry < 0) {
74 return JSTaggedValue::Undefined();
75 }
76 JSHandle<JSTaggedValue> value(thread, mapHandle->GetValue(thread, entry));
77 JSTaggedValue newMap = TaggedTreeMap::Delete(thread, mapHandle, entry);
78 map->SetTreeMap(thread, newMap);
79 return value.GetTaggedValue();
80 }
81
HasKey(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key)82 bool JSAPITreeMap::HasKey(JSThread *thread, const JSHandle<JSAPITreeMap> &map, const JSHandle<JSTaggedValue> &key)
83 {
84 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject()));
85 return TaggedTreeMap::FindEntry(thread, mapHandle, key) >= 0;
86 }
87
HasValue(JSThread * thread,const JSHandle<JSTaggedValue> & value) const88 bool JSAPITreeMap::HasValue(JSThread *thread, const JSHandle<JSTaggedValue> &value) const
89 {
90 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(GetTreeMap(thread).GetTaggedObject()));
91 return mapHandle->HasValue(thread, value.GetTaggedValue());
92 }
93
Clear(const JSThread * thread,const JSHandle<JSAPITreeMap> & map)94 void JSAPITreeMap::Clear(const JSThread *thread, const JSHandle<JSAPITreeMap> &map)
95 {
96 int cap = map->GetSize(thread);
97 if (cap == 0) {
98 return;
99 }
100 JSTaggedValue fn = TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject())->GetCompare(thread);
101 JSHandle<JSTaggedValue> compareFn = JSHandle<JSTaggedValue>(thread, fn);
102 cap = std::max(cap, TaggedTreeMap::MIN_CAPACITY);
103 JSTaggedValue internal = TaggedTreeMap::Create(thread, cap);
104 if (!compareFn->IsUndefined() && !compareFn->IsNull()) {
105 TaggedTreeMap::Cast(internal.GetTaggedObject())->SetCompare(thread, compareFn.GetTaggedValue());
106 }
107 map->SetTreeMap(thread, internal);
108 }
109
Replace(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)110 bool JSAPITreeMap::Replace(JSThread *thread, const JSHandle<JSAPITreeMap> &map, const JSHandle<JSTaggedValue> &key,
111 const JSHandle<JSTaggedValue> &value)
112 {
113 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap(thread).GetTaggedObject()));
114 int index = TaggedTreeMap::FindEntry(thread, mapHandle, key);
115 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
116 if (index < 0) {
117 return false;
118 }
119 mapHandle->SetValue(thread, index, value.GetTaggedValue());
120 return true;
121 }
122 } // namespace panda::ecmascript
123