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_list.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19
20 namespace panda::ecmascript {
21 using ContainerError = containers::ContainerError;
22 using ErrorFlag = containers::ErrorFlag;
Add(JSThread * thread,const JSHandle<JSAPIList> & list,const JSHandle<JSTaggedValue> & value)23 void JSAPIList::Add(JSThread *thread, const JSHandle<JSAPIList> &list, const JSHandle<JSTaggedValue> &value)
24 {
25 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
26 JSTaggedValue newList = TaggedSingleList::Add(thread, singleList, value);
27 list->SetSingleList(thread, newList);
28 }
29
GetFirst(const JSThread * thread)30 JSTaggedValue JSAPIList::GetFirst(const JSThread *thread)
31 {
32 JSTaggedValue res = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject())->GetFirst(thread);
33 if (res.IsHole()) {
34 return JSTaggedValue::Undefined();
35 }
36 return res;
37 }
38
GetLast(const JSThread * thread)39 JSTaggedValue JSAPIList::GetLast(const JSThread *thread)
40 {
41 JSTaggedValue res = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject())->GetLast(thread);
42 if (res.IsHole()) {
43 return JSTaggedValue::Undefined();
44 }
45 return res;
46 }
47
Insert(JSThread * thread,const JSHandle<JSAPIList> & list,const JSHandle<JSTaggedValue> & value,const int index)48 JSTaggedValue JSAPIList::Insert(JSThread *thread, const JSHandle<JSAPIList> &list, const JSHandle<JSTaggedValue> &value,
49 const int index)
50 {
51 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
52 int nodeLength = singleList->Length();
53 if (index < 0 || index > nodeLength) {
54 std::ostringstream oss;
55 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << nodeLength
56 << ". Received value is: " << index;
57 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
58 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
59 }
60 JSTaggedValue newList = TaggedSingleList::Insert(thread, singleList, value, index);
61 list->SetSingleList(thread, newList);
62 if (index != nodeLength) {
63 list->SetIsOrderedList(false);
64 }
65 return JSTaggedValue::True();
66 }
67
Set(JSThread * thread,const JSHandle<JSAPIList> & list,const int index,const JSHandle<JSTaggedValue> & value)68 JSTaggedValue JSAPIList::Set(JSThread *thread, const JSHandle<JSAPIList> &list,
69 const int index, const JSHandle<JSTaggedValue> &value)
70 {
71 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
72 int nodeLength = singleList->Length();
73 if (nodeLength <= 0) {
74 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
75 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
76 }
77 if (index < 0 || index >= nodeLength) {
78 std::ostringstream oss;
79 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (nodeLength - 1)
80 << ". Received value is: " << index;
81 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
82 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
83 }
84 TaggedSingleList::Set(thread, singleList, index, value);
85 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
86 return value.GetTaggedValue();
87 }
88
Has(const JSThread * thread,const JSTaggedValue & element)89 bool JSAPIList::Has(const JSThread *thread, const JSTaggedValue &element)
90 {
91 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
92 return singleList->Has(thread, element);
93 }
94
IsEmpty(const JSThread * thread)95 bool JSAPIList::IsEmpty(const JSThread *thread)
96 {
97 return TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject())->IsEmpty();
98 }
99
Get(const JSThread * thread,const int index)100 JSTaggedValue JSAPIList::Get(const JSThread *thread, const int index)
101 {
102 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
103 int nodeLength = singleList->Length();
104 if (index < 0 || index >= nodeLength) {
105 return JSTaggedValue::Undefined();
106 }
107 return singleList->Get(thread, index);
108 }
109
FastGet(JSThread * thread,const int index,const JSHandle<JSAPIList> & list)110 JSTaggedValue JSAPIList::FastGet(JSThread *thread, const int index, const JSHandle<JSAPIList> &list)
111 {
112 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
113 if (index < 0 || index >= singleList->Length()) {
114 return JSTaggedValue::Undefined();
115 }
116 int dataIndex = TaggedSingleList::ELEMENTS_START_INDEX + (index + 1) * TaggedSingleList::ENTRY_SIZE;
117 if (!list->IsOrderedList()) {
118 auto newSingleList = TaggedSingleList::SortByNodeOrder(thread, singleList);
119 TaggedSingleList *newList = TaggedSingleList::Cast(newSingleList.GetTaggedObject());
120 if (newList == nullptr) {
121 return JSTaggedValue::Undefined();
122 }
123 list->SetSingleList(thread, newSingleList);
124 list->SetIsOrderedList(true);
125 return newList->GetElement(thread, dataIndex);
126 }
127 return singleList->GetElement(thread, dataIndex);
128 }
129
GetIndexOf(const JSThread * thread,const JSTaggedValue & element)130 JSTaggedValue JSAPIList::GetIndexOf(const JSThread *thread, const JSTaggedValue &element)
131 {
132 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
133 return JSTaggedValue(singleList->GetIndexOf(thread, element));
134 }
135
GetLastIndexOf(const JSThread * thread,const JSTaggedValue & element)136 JSTaggedValue JSAPIList::GetLastIndexOf(const JSThread *thread, const JSTaggedValue &element)
137 {
138 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
139 return JSTaggedValue(singleList->GetLastIndexOf(thread, element));
140 }
141
Clear(JSThread * thread)142 void JSAPIList::Clear(JSThread *thread)
143 {
144 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
145 if (singleList->NumberOfNodes() > 0) {
146 singleList->Clear(thread);
147 }
148 SetIsOrderedList(true);
149 }
150
RemoveByIndex(JSThread * thread,const JSHandle<JSAPIList> & list,const int & index)151 JSTaggedValue JSAPIList::RemoveByIndex(JSThread *thread, const JSHandle<JSAPIList> &list, const int &index)
152 {
153 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
154 int nodeLength = singleList->Length();
155 if (nodeLength <= 0) {
156 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
157 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
158 }
159 if (index < 0 || index >= nodeLength) {
160 int size = (nodeLength > 0) ? (nodeLength - 1) : 0;
161 std::ostringstream oss;
162 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << size
163 << ". Received value is: " << index;
164 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
165 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
166 }
167 list->SetIsOrderedList(false);
168 return singleList->RemoveByIndex(thread, index);
169 }
170
Remove(JSThread * thread,const JSTaggedValue & element)171 JSTaggedValue JSAPIList::Remove(JSThread *thread, const JSTaggedValue &element)
172 {
173 TaggedSingleList *singleList = TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject());
174 SetIsOrderedList(false);
175 return singleList->Remove(thread, element);
176 }
177
ReplaceAllElements(JSThread * thread,const JSHandle<JSTaggedValue> & thisHandle,const JSHandle<JSTaggedValue> & callbackFn,const JSHandle<JSTaggedValue> & thisArg)178 JSTaggedValue JSAPIList::ReplaceAllElements(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle,
179 const JSHandle<JSTaggedValue> &callbackFn,
180 const JSHandle<JSTaggedValue> &thisArg)
181 {
182 JSHandle<JSAPIList> list = JSHandle<JSAPIList>::Cast(thisHandle);
183 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
184 return TaggedSingleList::ReplaceAllElements(thread, thisHandle, callbackFn, thisArg, singleList);
185 }
186
Sort(JSThread * thread,const JSHandle<JSTaggedValue> & thisHandle,const JSHandle<JSTaggedValue> & callbackFn)187 JSTaggedValue JSAPIList::Sort(JSThread *thread, const JSHandle<JSTaggedValue> &thisHandle,
188 const JSHandle<JSTaggedValue> &callbackFn)
189 {
190 JSHandle<JSAPIList> list = JSHandle<JSAPIList>::Cast(thisHandle);
191 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
192 list->SetIsOrderedList(false);
193 return TaggedSingleList::Sort(thread, callbackFn, singleList);
194 }
195
Equal(JSThread * thread,const JSHandle<JSAPIList> & list)196 JSTaggedValue JSAPIList::Equal(JSThread *thread, const JSHandle<JSAPIList> &list)
197 {
198 JSHandle<TaggedSingleList> compareList(thread, list->GetSingleList(thread));
199 return TaggedSingleList::Cast(GetSingleList(thread).GetTaggedObject())->Equal(thread, compareList);
200 }
201
ConvertToArray(const JSThread * thread,const JSHandle<JSAPIList> & list)202 JSTaggedValue JSAPIList::ConvertToArray(const JSThread *thread, const JSHandle<JSAPIList> &list)
203 {
204 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
205 return TaggedSingleList::ConvertToArray(thread, singleList);
206 }
207
GetSubList(JSThread * thread,const JSHandle<JSAPIList> & list,const int fromIndex,const int toIndex)208 JSTaggedValue JSAPIList::GetSubList(JSThread *thread, const JSHandle<JSAPIList> &list,
209 const int fromIndex, const int toIndex)
210 {
211 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
212 int nodeLength = singleList->Length();
213 if (nodeLength <= 0) {
214 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
215 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
216 }
217 int32_t size = nodeLength > toIndex ? toIndex : nodeLength;
218 if (fromIndex < 0 || fromIndex >= size) {
219 std::ostringstream oss;
220 oss << "The value of \"fromIndex\" is out of range. It must be >= 0 && <= "
221 << (size - 1) << ". Received value is: " << fromIndex;
222 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
223 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
224 }
225 if (toIndex < 0 || toIndex <= fromIndex || toIndex > nodeLength) {
226 std::ostringstream oss;
227 oss << "The value of \"toIndex\" is out of range. It must be >= 0 && <= "
228 << nodeLength << ". Received value is: " << toIndex;
229 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
230 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
231 }
232 uint32_t len = TaggedSingleList::ELEMENTS_START_INDEX + (toIndex - fromIndex + 1) * TaggedSingleList::ENTRY_SIZE;
233 JSHandle<TaggedArray> newElement = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(len);
234 JSHandle<TaggedSingleList> subSingleList = JSHandle<TaggedSingleList>::Cast(newElement);
235 JSHandle<JSAPIList> sublist = thread->GetEcmaVM()->GetFactory()->NewJSAPIList();
236 TaggedSingleList::GetSubList(thread, singleList, fromIndex, toIndex, subSingleList);
237 sublist->SetSingleList(thread, subSingleList);
238 sublist->SetIsOrderedList(true);
239 return sublist.GetTaggedValue();
240 }
241
OwnKeys(JSThread * thread,const JSHandle<JSAPIList> & list)242 JSHandle<TaggedArray> JSAPIList::OwnKeys(JSThread *thread, const JSHandle<JSAPIList> &list)
243 {
244 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
245 return TaggedSingleList::OwnKeys(thread, singleList);
246 }
247
GetOwnProperty(JSThread * thread,const JSHandle<JSAPIList> & list,const JSHandle<JSTaggedValue> & key)248 bool JSAPIList::GetOwnProperty(JSThread *thread, const JSHandle<JSAPIList> &list, const JSHandle<JSTaggedValue> &key)
249 {
250 uint32_t index = 0;
251 if (UNLIKELY(!JSTaggedValue::ToElementIndex(thread, key.GetTaggedValue(), &index))) {
252 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
253 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
254 CString errorMsg =
255 "The type of \"index\" can not obtain attributes of no-number type. Received value is: "
256 + ConvertToString(thread, *result);
257 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
258 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
259 }
260 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
261 uint32_t length = static_cast<uint32_t>(singleList->Length());
262 if (index >= length) {
263 std::ostringstream oss;
264 oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
265 << ". Received value is: " << index;
266 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
267 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
268 }
269 list->Get(thread, index);
270 return true;
271 }
272
GetProperty(JSThread * thread,const JSHandle<JSAPIList> & list,const JSHandle<JSTaggedValue> & key)273 OperationResult JSAPIList::GetProperty(JSThread *thread, const JSHandle<JSAPIList> &list,
274 const JSHandle<JSTaggedValue> &key)
275 {
276 JSHandle<TaggedSingleList> singleList(thread, list->GetSingleList(thread));
277 int nodeLength = singleList->Length();
278 JSHandle<JSTaggedValue> indexKey = key;
279 if (indexKey->IsDouble()) {
280 /* Double variables like the form of Math.floor(1) are processed as TaggedInt,
281 while integers greater than INT32_MAX are still TaggedDouble */
282 indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
283 }
284 if (!indexKey->IsInt()) {
285 CString errorMsg = "The type of \"index\" must be small integer.";
286 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
287 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
288 OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
289 }
290
291 int index = indexKey->GetInt();
292 if (index < 0 || index >= nodeLength) {
293 std::ostringstream oss;
294 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (nodeLength - 1)
295 << ". Received value is: " << index;
296 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
297 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
298 JSTaggedValue::Exception(),
299 PropertyMetaData(false)));
300 }
301
302 return OperationResult(thread, singleList->Get(thread, index), PropertyMetaData(false));
303 }
304
SetProperty(JSThread * thread,const JSHandle<JSAPIList> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)305 bool JSAPIList::SetProperty(JSThread *thread, const JSHandle<JSAPIList> &obj,
306 const JSHandle<JSTaggedValue> &key,
307 const JSHandle<JSTaggedValue> &value)
308 {
309 JSHandle<TaggedSingleList> singleList(thread, obj->GetSingleList(thread));
310 int nodeLength = singleList->Length();
311 int index = static_cast<int>(key->GetNumber());
312 if (index < 0 || index >= nodeLength) {
313 return false;
314 }
315
316 TaggedSingleList::Set(thread, singleList, index, value);
317 return true;
318 }
319 } // namespace panda::ecmascript
320