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