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