1 /*
2 * Copyright (c) 2021 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_array_iterator.h"
17
18 #include "ecmascript/base/typed_array_helper-inl.h"
19 #include "ecmascript/base/typed_array_helper.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/object_factory.h"
23
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
26 // 22.1.5.2.1 %ArrayIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * argv)27 JSTaggedValue JSArrayIterator::Next(EcmaRuntimeCallInfo *argv)
28 {
29 ASSERT(argv);
30 JSThread *thread = argv->GetThread();
31 [[maybe_unused]] EcmaHandleScope handleScope(thread);
32 // 1.Let O be the this value.
33 JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
34 return NextInternal(thread, thisObj);
35 }
36
NextInternal(JSThread * thread,JSHandle<JSTaggedValue> thisObj)37 JSTaggedValue JSArrayIterator::NextInternal(JSThread *thread, JSHandle<JSTaggedValue> thisObj)
38 {
39 // 2.If Type(O) is not Object, throw a TypeError exception.
40 // 3.If O does not have all of the internal slots of an TaggedArray Iterator Instance (22.1.5.3), throw a TypeError
41 // exception.
42 if (!thisObj->IsJSArrayIterator()) {
43 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not an array iterator", JSTaggedValue::Exception());
44 }
45 JSHandle<JSArrayIterator> iter(thisObj);
46 // 4.Let a be O.[[IteratedArrayLike]].
47 JSHandle<JSTaggedValue> array(thread, iter->GetIteratedArray());
48 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
49 // 5.If a is undefined, return CreateIterResultObject(undefined, true).
50 if (array->IsUndefined()) {
51 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
52 }
53 // 6.Let index be O.[[ArrayLikeNextIndex]].
54 uint32_t index = iter->GetNextIndex();
55 // 7.Let itemKind be O.[[ArrayLikeIterationKind]].
56 IterationKind itemKind = iter->GetIterationKind();
57
58 uint32_t length;
59 // 8. If a has a [[TypedArrayName]] internal slot, then
60 // a. Let len be the value of O’s [[ArrayLength]] internal slot.
61 if (array->IsTypedArray()) {
62 length = JSHandle<JSTypedArray>::Cast(array)->GetArrayLength();
63 } else if (array->IsJSArray()) {
64 length = JSHandle<JSArray>(array)->GetArrayLength();
65 } else {
66 // 9.Else
67 JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
68 length =
69 JSTaggedValue::ToLength(thread, JSTaggedValue::GetProperty(thread, array, lengthKey).GetValue()).ToUint32();
70 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
71 }
72
73 // 10.If index ≥ len, then
74 if (index >= length) {
75 // Set O.[[IteratedArrayLike]] to undefined.
76 // Return CreateIterResultObject(undefined, true).
77 iter->SetIteratedArray(thread, undefinedHandle);
78 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
79 }
80 // 11.Set O.[[ArrayLikeNextIndex]] to index + 1.
81 iter->SetNextIndex(index + 1);
82 // 12.If itemKind is key, return CreateIterResultObject(index, false).
83 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(index));
84 if (itemKind == IterationKind::KEY) {
85 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
86 }
87 JSHandle<JSTaggedValue> value = JSArray::FastGetPropertyByValue(thread, array, index);
88 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
89 // 15.If itemKind is value, let result be elementValue.
90 if (itemKind == IterationKind::VALUE) {
91 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
92 }
93 // 16. Else
94 ASSERT_PRINT(itemKind == IterationKind::KEY_AND_VALUE, "itemKind is invalid");
95 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
96 JSHandle<TaggedArray> resultArray(factory->NewTaggedArray(2)); // 2 means the length of array
97 resultArray->Set(thread, 0, key);
98 resultArray->Set(thread, 1, value);
99 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, resultArray));
100 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
101 }
102 } // namespace panda::ecmascript
103