• 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_iterator.h"
17 
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/js_api/js_api_deque.h"
20 #include "ecmascript/js_iterator.h"
21 #include "ecmascript/js_tagged_value-inl.h"
22 
23 namespace panda::ecmascript {
24 using BuiltinsBase = base::BuiltinsBase;
25 using ContainerError = containers::ContainerError;
26 using ErrorFlag = containers::ErrorFlag;
27 // DequeIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * argv)28 JSTaggedValue JSAPIDequeIterator::Next(EcmaRuntimeCallInfo *argv)
29 {
30     ASSERT(argv);
31     JSThread *thread = argv->GetThread();
32     [[maybe_unused]] EcmaHandleScope handleScope(thread);
33     JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
34 
35     if (!input->IsJSAPIDequeIterator()) {
36         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
37                                                             "The Symbol.iterator method cannot be bound");
38         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
39     }
40     JSHandle<JSAPIDequeIterator> iter(input);
41     JSHandle<JSTaggedValue> iteratorDeque(thread, iter->GetIteratedDeque());
42     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
43     if (iteratorDeque->IsUndefined()) {
44         return globalConst->GetUndefinedIterResult();
45     }
46     JSHandle<JSAPIDeque> deque = JSHandle<JSAPIDeque>::Cast(iteratorDeque);
47     uint32_t index = iter->GetNextIndex();
48 
49     JSHandle<TaggedArray> elements(thread, deque->GetElements());
50     uint32_t capacity = elements->GetLength();
51     uint32_t first = deque->GetFirst();
52     uint32_t last = deque->GetLast();
53     if (index == last) {
54         JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
55         iter->SetIteratedDeque(thread, undefinedHandle);
56         return globalConst->GetUndefinedIterResult();
57     }
58     ASSERT(capacity != 0);
59     iter->SetNextIndex((index + 1) % capacity);
60     uint32_t elementIndex = (index + capacity - first) % capacity;
61     JSHandle<JSTaggedValue> value(thread, JSHandle<JSAPIDeque>::Cast(iteratorDeque)->Get(elementIndex));
62 
63     return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
64 }
65 } // namespace panda::ecmascript
66