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_queue_iterator.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_api/js_api_queue.h"
22 #include "ecmascript/object_factory.h"
23
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
26 using ContainerError = containers::ContainerError;
27 using ErrorFlag = containers::ErrorFlag;
28 // QueueIteratorPrototype%.next()
Next(EcmaRuntimeCallInfo * argv)29 JSTaggedValue JSAPIQueueIterator::Next(EcmaRuntimeCallInfo *argv)
30 {
31 ASSERT(argv);
32 JSThread *thread = argv->GetThread();
33 [[maybe_unused]] EcmaHandleScope handleScope(thread);
34 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
35
36 if (!input->IsJSAPIQueueIterator()) {
37 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
38 "The Symbol.iterator method cannot be bound");
39 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
40 }
41 JSHandle<JSAPIQueueIterator> iter(input);
42 JSHandle<JSTaggedValue> queue(thread, iter->GetIteratedQueue());
43 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
44 if (queue->IsUndefined()) {
45 return globalConst->GetUndefinedIterResult();
46 }
47
48 uint32_t index = iter->GetNextIndex();
49 uint32_t length = JSAPIQueue::GetArrayLength(thread, JSHandle<JSAPIQueue>(queue));
50 if (index >= length) {
51 JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
52 iter->SetIteratedQueue(thread, undefinedHandle);
53 return globalConst->GetUndefinedIterResult();
54 }
55 iter->SetNextIndex(index + 1);
56
57 JSHandle<JSTaggedValue> value(thread, JSHandle<JSAPIQueue>::Cast(queue)->Get(thread, index));
58 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
59
60 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
61 }
62 } // namespace panda::ecmascript
63