• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> input(BuiltinsBase::GetThis(argv));
34 
35     // 2.If Type(O) is not Object, throw a TypeError exception.
36     // 3.If O does not have all of the internal slots of an TaggedArray Iterator Instance (22.1.5.3), throw a TypeError
37     // exception.
38     if (!input->IsJSArrayIterator()) {
39         THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not an array iterator", JSTaggedValue::Exception());
40     }
41     JSHandle<JSArrayIterator> iter(input);
42     // 4.Let a be O.[[IteratedArrayLike]].
43     JSHandle<JSTaggedValue> array(thread, iter->GetIteratedArray());
44     JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
45     // 5.If a is undefined, return CreateIterResultObject(undefined, true).
46     if (array->IsUndefined()) {
47         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
48     }
49     // 6.Let index be O.[[ArrayLikeNextIndex]].
50     uint32_t index = iter->GetNextIndex();
51     // 7.Let itemKind be O.[[ArrayLikeIterationKind]].
52     IterationKind itemKind = iter->GetIterationKind();
53 
54     uint32_t length;
55     // 8. If a has a [[TypedArrayName]] internal slot, then
56     //   a. Let len be the value of O’s [[ArrayLength]] internal slot.
57     if (array->IsTypedArray()) {
58         length = JSHandle<JSTypedArray>::Cast(array)->GetArrayLength();
59     } else if (array->IsJSArray()) {
60         length = JSHandle<JSArray>(array)->GetArrayLength();
61     } else {
62         // 9.Else
63         JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
64         length =
65             JSTaggedValue::ToLength(thread, JSTaggedValue::GetProperty(thread, array, lengthKey).GetValue()).ToUint32();
66         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
67     }
68 
69     // 10.If index ≥ len, then
70     if (index >= length) {
71         // Set O.[[IteratedArrayLike]] to undefined.
72         // Return CreateIterResultObject(undefined, true).
73         iter->SetIteratedArray(thread, undefinedHandle);
74         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
75     }
76     // 11.Set O.[[ArrayLikeNextIndex]] to index + 1.
77     iter->SetNextIndex(index + 1);
78     // 12.If itemKind is key, return CreateIterResultObject(index, false).
79     JSHandle<JSTaggedValue> key(thread, JSTaggedValue(index));
80     if (itemKind == IterationKind::KEY) {
81         return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
82     }
83     JSHandle<JSTaggedValue> value = JSArray::FastGetPropertyByValue(thread, array, index);
84     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
85     // 15.If itemKind is value, let result be elementValue.
86     if (itemKind == IterationKind::VALUE) {
87         return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
88     }
89     // 16. Else
90     ASSERT_PRINT(itemKind == IterationKind::KEY_AND_VALUE, "itemKind is invalid");
91     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
92     JSHandle<TaggedArray> resultArray(factory->NewTaggedArray(2));  // 2 means the length of array
93     resultArray->Set(thread, 0, key);
94     resultArray->Set(thread, 1, value);
95     JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, resultArray));
96     return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
97 }
98 }  // namespace panda::ecmascript
99