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