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