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_deque.h"
17
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/js_tagged_value-inl.h"
20 #include "ecmascript/tagged_array-inl.h"
21
22 namespace panda::ecmascript {
23 using ContainerError = containers::ContainerError;
24 using ErrorFlag = containers::ErrorFlag;
25 class JSAPIDequelterator;
26
InsertFront(JSThread * thread,const JSHandle<JSAPIDeque> & deque,const JSHandle<JSTaggedValue> & value)27 void JSAPIDeque::InsertFront(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value)
28 {
29 JSHandle<TaggedArray> elements(thread, deque->GetElements(thread));
30 ASSERT(!elements->IsDictionaryMode());
31 uint32_t capacity = elements->GetLength();
32 uint32_t first = deque->GetFirst();
33 uint32_t last = deque->GetLast();
34 ASSERT(capacity != 0);
35 if ((first + capacity - 1) % capacity == last) {
36 elements = GrowCapacity(thread, deque, capacity, first, last);
37 ASSERT(!elements->IsDictionaryMode());
38 deque->SetLast(capacity - 1);
39 first = 0;
40 }
41 capacity = elements->GetLength();
42 ASSERT(capacity != 0);
43 first = (first + capacity - 1) % capacity;
44 elements->Set(thread, first, value);
45 deque->SetFirst(first);
46 }
47
InsertEnd(JSThread * thread,const JSHandle<JSAPIDeque> & deque,const JSHandle<JSTaggedValue> & value)48 void JSAPIDeque::InsertEnd(JSThread *thread, const JSHandle<JSAPIDeque> &deque, const JSHandle<JSTaggedValue> &value)
49 {
50 JSHandle<TaggedArray> elements(thread, deque->GetElements(thread));
51 ASSERT(!elements->IsDictionaryMode());
52 uint32_t capacity = elements->GetLength();
53 uint32_t first = deque->GetFirst();
54 uint32_t last = deque->GetLast();
55 ASSERT(capacity != 0);
56 if (first == (last + 1) % capacity) {
57 elements = GrowCapacity(thread, deque, capacity, first, last);
58 ASSERT(!elements->IsDictionaryMode());
59 deque->SetFirst(0);
60 last = capacity - 1;
61 }
62 elements->Set(thread, last, value);
63 capacity = elements->GetLength();
64 ASSERT(capacity != 0);
65 last = (last + 1) % capacity;
66 deque->SetLast(last);
67 }
68
GetFront(const JSThread * thread)69 JSTaggedValue JSAPIDeque::GetFront(const JSThread *thread)
70 {
71 if (JSAPIDeque::IsEmpty()) {
72 return JSTaggedValue::Undefined();
73 }
74
75 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
76 ASSERT(!elements->IsDictionaryMode());
77 return elements->Get(thread, GetFirst());
78 }
79
GetTail(const JSThread * thread)80 JSTaggedValue JSAPIDeque::GetTail(const JSThread *thread)
81 {
82 if (JSAPIDeque::IsEmpty()) {
83 return JSTaggedValue::Undefined();
84 }
85 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
86 ASSERT(!elements->IsDictionaryMode());
87 uint32_t capacity = elements->GetLength();
88 uint32_t last = GetLast();
89 ASSERT(capacity != 0);
90 return elements->Get(thread, (last + capacity - 1) % capacity);
91 }
92
GrowCapacity(JSThread * thread,const JSHandle<JSAPIDeque> & deque,uint32_t oldCapacity,uint32_t first,uint32_t last)93 JSHandle<TaggedArray> JSAPIDeque::GrowCapacity(JSThread *thread, const JSHandle<JSAPIDeque> &deque,
94 uint32_t oldCapacity, uint32_t first, uint32_t last)
95 {
96 JSHandle<TaggedArray> oldElements(thread, deque->GetElements(thread));
97 ASSERT(!oldElements->IsDictionaryMode());
98 uint32_t newCapacity = ComputeCapacity(oldCapacity);
99 uint32_t size = deque->GetSize(thread);
100 JSHandle<TaggedArray> newElements =
101 thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, size, first, last);
102 deque->SetElements(thread, newElements);
103 return newElements;
104 }
105
PopFirst(JSThread * thread)106 JSTaggedValue JSAPIDeque::PopFirst(JSThread *thread)
107 {
108 if (JSAPIDeque::IsEmpty()) {
109 return JSTaggedValue::Undefined();
110 }
111 uint32_t first = GetFirst();
112 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
113 ASSERT(!elements->IsDictionaryMode());
114 uint32_t capacity = elements->GetLength();
115 JSTaggedValue firstElement = elements->Get(thread, first);
116 ASSERT(capacity != 0);
117 first = (first + 1) % capacity;
118 SetFirst(first);
119 return firstElement;
120 }
121
PopLast(JSThread * thread)122 JSTaggedValue JSAPIDeque::PopLast(JSThread *thread)
123 {
124 if (JSAPIDeque::IsEmpty()) {
125 return JSTaggedValue::Undefined();
126 }
127 uint32_t last = GetLast();
128 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
129 ASSERT(!elements->IsDictionaryMode());
130 uint32_t capacity = elements->GetLength();
131 ASSERT(capacity != 0);
132 last = (last + capacity - 1) % capacity;
133 JSTaggedValue lastElement = elements->Get(thread, last);
134 SetLast(last);
135 return lastElement;
136 }
137
IsEmpty()138 bool JSAPIDeque::IsEmpty()
139 {
140 uint32_t first = GetFirst();
141 uint32_t last = GetLast();
142 return first == last;
143 }
144
GetSize(const JSThread * thread) const145 uint32_t JSAPIDeque::GetSize(const JSThread *thread) const
146 {
147 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
148 uint32_t capacity = elements->GetLength();
149 uint32_t first = GetFirst();
150 uint32_t last = GetLast();
151 ASSERT(capacity != 0);
152 return (last - first + capacity) % capacity;
153 }
154
Get(const JSThread * thread,const uint32_t index)155 JSTaggedValue JSAPIDeque::Get(const JSThread *thread, const uint32_t index)
156 {
157 ASSERT(index < GetSize(thread));
158 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
159 uint32_t capacity = elements->GetLength();
160 uint32_t first = GetFirst();
161 ASSERT(capacity != 0);
162 uint32_t curIndex = (first + index) % capacity;
163 return elements->Get(thread, curIndex);
164 }
165
Set(JSThread * thread,const uint32_t index,JSTaggedValue value)166 JSTaggedValue JSAPIDeque::Set(JSThread *thread, const uint32_t index, JSTaggedValue value)
167 {
168 uint32_t length = static_cast<uint32_t>(GetSize(thread));
169 if (index < 0 || index >= length) {
170 return JSTaggedValue::False();
171 }
172 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
173 uint32_t capacity = elements->GetLength();
174 uint32_t first = GetFirst();
175 ASSERT(capacity != 0);
176 uint32_t curIndex = (first + index) % capacity;
177 elements->Set(thread, curIndex, value);
178 return JSTaggedValue::True();
179 }
180
Has(const JSThread * thread,JSTaggedValue value) const181 bool JSAPIDeque::Has(const JSThread *thread, JSTaggedValue value) const
182 {
183 uint32_t first = GetFirst();
184 uint32_t last = GetLast();
185 TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
186 uint32_t capacity = elements->GetLength();
187 uint32_t index = first;
188 while (index != last) {
189 if (JSTaggedValue::SameValue(thread, elements->Get(thread, index), value)) {
190 return true;
191 }
192 ASSERT(capacity != 0);
193 index = (index + 1) % capacity;
194 }
195 return false;
196 }
197
OwnKeys(JSThread * thread,const JSHandle<JSAPIDeque> & deque)198 JSHandle<TaggedArray> JSAPIDeque::OwnKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
199 {
200 uint32_t length = deque->GetSize(thread);
201
202 JSHandle<TaggedArray> oldElements(thread, deque->GetElements(thread));
203 uint32_t oldCapacity = oldElements->GetLength();
204 uint32_t newCapacity = ComputeCapacity(oldCapacity);
205 uint32_t firstIndex = deque->GetFirst();
206 uint32_t lastIndex = deque->GetLast();
207 JSHandle<TaggedArray> newElements =
208 thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, length, firstIndex, lastIndex);
209 deque->SetFirst(0);
210 deque->SetLast(length);
211 deque->SetElements(thread, newElements);
212
213 return JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(deque));
214 }
215
OwnEnumKeys(JSThread * thread,const JSHandle<JSAPIDeque> & deque)216 JSHandle<TaggedArray> JSAPIDeque::OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
217 {
218 uint32_t length = deque->GetSize(thread);
219
220 JSHandle<TaggedArray> oldElements(thread, deque->GetElements(thread));
221 ASSERT(!oldElements->IsDictionaryMode());
222 uint32_t oldCapacity = oldElements->GetLength();
223 uint32_t newCapacity = ComputeCapacity(oldCapacity);
224 uint32_t firstIndex = deque->GetFirst();
225 uint32_t lastIndex = deque->GetLast();
226 JSHandle<TaggedArray> newElements =
227 thread->GetEcmaVM()->GetFactory()->CopyDeque(oldElements, newCapacity, length, firstIndex, lastIndex);
228 deque->SetFirst(0);
229 deque->SetLast(length);
230 deque->SetElements(thread, newElements);
231
232 return JSObject::GetOwnEnumPropertyKeys(thread, JSHandle<JSObject>::Cast(deque));
233 }
234
GetOwnProperty(JSThread * thread,const JSHandle<JSAPIDeque> & deque,const JSHandle<JSTaggedValue> & key)235 bool JSAPIDeque::GetOwnProperty(JSThread *thread, const JSHandle<JSAPIDeque> &deque,
236 const JSHandle<JSTaggedValue> &key)
237 {
238 uint32_t index = 0;
239 if (UNLIKELY(!JSTaggedValue::ToElementIndex(thread, key.GetTaggedValue(), &index))) {
240 JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
241 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
242 CString errorMsg =
243 "The type of \"key\" can not obtain attributes of no-number type. Received value is: "
244 + ConvertToString(thread, *result);
245 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
246 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
247 }
248
249 uint32_t length = deque->GetSize(thread);
250 if (index >= length) {
251 ASSERT(length > 0);
252 std::ostringstream oss;
253 oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
254 << ". Received value is: " << index;
255 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
256 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
257 }
258
259 deque->Get(thread, index);
260 return true;
261 }
262
GetIteratorObj(JSThread * thread,const JSHandle<JSAPIDeque> & deque)263 JSTaggedValue JSAPIDeque::GetIteratorObj(JSThread *thread, const JSHandle<JSAPIDeque> &deque)
264 {
265 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
266 JSHandle<JSAPIDequeIterator> iter(factory->NewJSAPIDequeIterator(deque));
267
268 return iter.GetTaggedValue();
269 }
270
GetProperty(JSThread * thread,const JSHandle<JSAPIDeque> & obj,const JSHandle<JSTaggedValue> & key)271 OperationResult JSAPIDeque::GetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj,
272 const JSHandle<JSTaggedValue> &key)
273 {
274 int length = static_cast<int>(obj->GetSize(thread));
275 if (length == 0) {
276 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
277 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
278 JSTaggedValue::Exception(),
279 PropertyMetaData(false)));
280 }
281 JSHandle<JSTaggedValue> indexKey = key;
282 if (indexKey->IsDouble()) {
283 // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
284 // For integer which is greater than INT32_MAX, it will remain TaggedDouble
285 indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
286 }
287 if (!indexKey->IsInt()) {
288 CString errorMsg = "The type of \"index\" must be small integer.";
289 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
290 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
291 OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
292 }
293
294 int index = indexKey->GetInt();
295 if (index < 0 || index >= length) {
296 std::ostringstream oss;
297 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
298 << ". Received value is: " << index;
299 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
300 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
301 JSTaggedValue::Exception(),
302 PropertyMetaData(false)));
303 }
304
305 return OperationResult(thread, obj->Get(thread, index), PropertyMetaData(false));
306 }
307
SetProperty(JSThread * thread,const JSHandle<JSAPIDeque> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)308 bool JSAPIDeque::SetProperty(JSThread *thread, const JSHandle<JSAPIDeque> &obj,
309 const JSHandle<JSTaggedValue> &key,
310 const JSHandle<JSTaggedValue> &value)
311 {
312 int length = static_cast<int>(obj->GetSize(thread));
313 JSHandle<JSTaggedValue> indexKey = key;
314 if (indexKey->IsDouble()) {
315 // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
316 // For integer which is greater than INT32_MAX, it will remain TaggedDouble
317 indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
318 }
319 if (!indexKey->IsInt()) {
320 return false;
321 }
322
323 int index = indexKey->GetInt();
324 if (index < 0 || index >= length) {
325 return false;
326 }
327
328 obj->Set(thread, index, value.GetTaggedValue());
329 return true;
330 }
331 } // namespace panda::ecmascript
332