1 /*
2 * Copyright (c) 2024 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_bitvector_iterator.h"
17
18 #include "ecmascript/js_api/js_api_bitvector.h"
19 #include "ecmascript/shared_objects/concurrent_api_scope.h"
20
21 namespace panda::ecmascript {
22 using BuiltinsBase = base::BuiltinsBase;
23 // BitVectorIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * argv)24 JSTaggedValue JSAPIBitVectorIterator::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->IsJSAPIBitVectorIterator()) {
32 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not an bit vector iterator", JSTaggedValue::Exception());
33 }
34 JSHandle<JSAPIBitVectorIterator> iter(input);
35 // Let a be O.[[IteratedBitVectorLike]].
36 JSHandle<JSTaggedValue> bitVector(thread, iter->GetIteratedBitVector());
37 // If a is undefined, return an undefinedIteratorResult.
38 if (bitVector->IsUndefined()) {
39 return thread->GlobalConstants()->GetUndefinedIterResult();
40 }
41 // Let index be O.[[BitVectorLikeNextIndex]].
42 uint32_t index = iter->GetNextIndex();
43 // If a has a [[TypedBitVectorName]] internal slot, then
44 // Let len be the value of O’s [[BitVectorLength]] internal slot.
45 ASSERT(bitVector->IsJSAPIBitVector());
46 [[maybe_unused]] ConcurrentApiScope<JSAPIBitVector> scope(thread, bitVector);
47 const uint32_t length = static_cast<uint32_t>(JSHandle<JSAPIBitVector>::Cast(bitVector)->GetSize());
48 // If index >= len, then
49 if (index >= length) {
50 // Set O.[[IteratedVectorLike]] to undefined.
51 // Return undefinedIteratorResult.
52 JSHandle<JSTaggedValue> undefinedHandle = thread->GlobalConstants()->GetHandledUndefined();
53 iter->SetIteratedBitVector(thread, undefinedHandle);
54 return thread->GlobalConstants()->GetUndefinedIterResult();
55 }
56 // Set O.[[VectorLikeNextIndex]] to index + 1.
57 iter->SetNextIndex(index + 1);
58 JSHandle<JSTaggedValue> value(thread, JSHandle<JSAPIBitVector>::Cast(bitVector)->Get(thread, index));
59
60 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
61 }
62 } // namespace panda::ecmascript
63