• 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_vector_iterator.h"
17 
18 #include "ecmascript/base/typed_array_helper.h"
19 #include "ecmascript/js_api/js_api_vector.h"
20 
21 namespace panda::ecmascript {
22 using BuiltinsBase = base::BuiltinsBase;
23 // VectorIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * argv)24 JSTaggedValue JSAPIVectorIterator::Next(EcmaRuntimeCallInfo *argv)
25 {
26     ASSERT(argv);
27     JSThread *thread = argv->GetThread();
28     [[maybe_unused]] EcmaHandleScope handleScope(thread);
29     JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
30 
31     if (!input->IsJSAPIVectorIterator()) {
32         THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not an vector iterator", JSTaggedValue::Exception());
33     }
34     JSHandle<JSAPIVectorIterator> iter(input);
35     // Let a be O.[[IteratedVectorLike]].
36     JSHandle<JSTaggedValue> vector(thread, iter->GetIteratedVector());
37     // If a is undefined, return an undefinedIteratorResult.
38     if (vector->IsUndefined()) {
39         return thread->GlobalConstants()->GetUndefinedIterResult();
40     }
41     // Let index be O.[[VectorLikeNextIndex]].
42     uint32_t index = iter->GetNextIndex();
43     // If a has a [[TypedVectorName]] internal slot, then
44     // Let len be the value of O’s [[VectorLength]] internal slot.
45     ASSERT(vector->IsJSAPIVector());
46     const uint32_t length = static_cast<uint32_t>(JSHandle<JSAPIVector>::Cast(vector)->GetSize());
47     // If index >= len, then
48     if (index >= length) {
49         // Set O.[[IteratedVectorLike]] to undefined.
50         // Return undefinedIteratorResult.
51         JSHandle<JSTaggedValue> undefinedHandle = thread->GlobalConstants()->GetHandledUndefined();
52         iter->SetIteratedVector(thread, undefinedHandle);
53         return thread->GlobalConstants()->GetUndefinedIterResult();
54     }
55     // Set O.[[VectorLikeNextIndex]] to index + 1.
56     iter->SetNextIndex(index + 1);
57     JSHandle<JSTaggedValue> value(thread, JSAPIVector::Get(thread, JSHandle<JSAPIVector>::Cast(vector),
58                                     static_cast<int32_t>(index)));
59     return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
60 }
61 } // namespace panda::ecmascript
62