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 CString errorMsg =
31 "The type of \"key\" must be not null. Received value is: " + ConvertToString(*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().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().GetTaggedObject()));
45 return TaggedTreeMap::Get(thread, mapHandle, key);
46 }
47
GetSize() const48 int JSAPITreeMap::GetSize() const
49 {
50 return TaggedTreeMap::Cast(GetTreeMap().GetTaggedObject())->NumberOfElements();
51 }
52
GetKey(int entry) const53 JSTaggedValue JSAPITreeMap::GetKey(int entry) const
54 {
55 ASSERT_PRINT(entry < GetSize(), "entry must less than capacity");
56 JSTaggedValue key = TaggedTreeMap::Cast(GetTreeMap().GetTaggedObject())->GetKey(entry);
57 return key.IsHole() ? JSTaggedValue::Undefined() : key;
58 }
59
GetValue(int entry) const60 JSTaggedValue JSAPITreeMap::GetValue(int entry) const
61 {
62 ASSERT_PRINT(entry < GetSize(), "entry must less than capacity");
63 JSTaggedValue value = TaggedTreeMap::Cast(GetTreeMap().GetTaggedObject())->GetValue(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().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(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().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().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();
97 JSTaggedValue internal = TaggedTreeMap::Create(thread, cap);
98 map->SetTreeMap(thread, internal);
99 }
100
Replace(JSThread * thread,const JSHandle<JSAPITreeMap> & map,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)101 bool JSAPITreeMap::Replace(JSThread *thread, const JSHandle<JSAPITreeMap> &map, const JSHandle<JSTaggedValue> &key,
102 const JSHandle<JSTaggedValue> &value)
103 {
104 JSHandle<TaggedTreeMap> mapHandle(thread, TaggedTreeMap::Cast(map->GetTreeMap().GetTaggedObject()));
105 int index = TaggedTreeMap::FindEntry(thread, mapHandle, key);
106 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
107 if (index < 0) {
108 return false;
109 }
110 mapHandle->SetValue(thread, index, value.GetTaggedValue());
111 return true;
112 }
113 } // namespace panda::ecmascript
114