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