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_plain_array_iterator.h"
17
18 #include "ecmascript/base/typed_array_helper-inl.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/js_api/js_api_plain_array.h"
21 #include "ecmascript/js_array.h"
22
23 namespace panda::ecmascript {
24 using BuiltinsBase = base::BuiltinsBase;
25 using ContainerError = containers::ContainerError;
26 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)27 JSTaggedValue JSAPIPlainArrayIterator::Next(EcmaRuntimeCallInfo *argv)
28 {
29 ASSERT(argv);
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope handleScope(thread);
32 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
33 if (!input->IsJSAPIPlainArrayIterator()) {
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<JSAPIPlainArrayIterator> iter(input);
39 JSHandle<JSTaggedValue> plainArray(thread, iter->GetIteratedPlainArray());
40 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
41 if (plainArray->IsUndefined()) {
42 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
43 }
44
45 JSHandle<JSAPIPlainArray> apiPlainArray(plainArray);
46 ASSERT(plainArray->IsJSAPIPlainArray());
47
48 uint32_t length = apiPlainArray->GetLength();
49 int32_t index = iter->GetNextIndex();
50 if (static_cast<uint32_t>(index) >= length) {
51 iter->SetIteratedPlainArray(thread, undefinedHandle);
52 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
53 }
54 iter->SetNextIndex(index + 1);
55 JSHandle<TaggedArray> valueArray(thread, TaggedArray::Cast(apiPlainArray->GetValues().GetTaggedObject()));
56 JSHandle<JSTaggedValue> value(thread, valueArray->Get(index));
57 JSHandle<TaggedArray> keyArray(thread, TaggedArray::
58 Cast(JSHandle<JSAPIPlainArray>(plainArray)->GetKeys().GetTaggedObject()));
59 JSHandle<JSTaggedValue> keyHandle(thread, keyArray->Get(index));
60 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
61 JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
62 array->Set(thread, 0, keyHandle);
63 array->Set(thread, 1, value);
64 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
65 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
66 }
67 } // namespace panda::ecmascript
68