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