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_linked_list_iterator.h"
17
18 #include "ecmascript/base/typed_array_helper.h"
19 #include "ecmascript/base/typed_array_helper-inl.h"
20 #include "ecmascript/builtins/builtins_errors.h"
21 #include "ecmascript/containers/containers_errors.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_api/js_api_linked_list.h"
24 #include "ecmascript/js_array.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tagged_list.h"
27
28 namespace panda::ecmascript {
29 using BuiltinsBase = base::BuiltinsBase;
30 using ContainerError = containers::ContainerError;
31 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)32 JSTaggedValue JSAPILinkedListIterator::Next(EcmaRuntimeCallInfo *argv)
33 {
34 ASSERT(argv);
35 JSThread *thread = argv->GetThread();
36 [[maybe_unused]] EcmaHandleScope handleScope(thread);
37 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
38 if (!input->IsJSAPILinkedListIterator()) {
39 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
40 "The Symbol.iterator method cannot be bound");
41 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
42 }
43 JSHandle<JSAPILinkedListIterator> iter(input);
44 JSHandle<JSTaggedValue> linkedList(thread, iter->GetIteratedLinkedList());
45 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
46 JSHandle<TaggedDoubleList> list(linkedList);
47 if (linkedList->IsUndefined()) {
48 return globalConst->GetUndefinedIterResult();
49 }
50 int index = static_cast<int>(iter->GetNextIndex());
51 int length = list->Length();
52 if (index >= length) {
53 JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
54 iter->SetIteratedLinkedList(thread, undefinedHandle);
55 return globalConst->GetUndefinedIterResult();
56 }
57 iter->SetNextIndex(index + 1);
58 int dataIndex = static_cast<int>(iter->GetDataIndex());
59 std::pair<int, JSTaggedValue> resultPair = list->GetByDataIndex(dataIndex);
60 iter->SetDataIndex(resultPair.first);
61 JSHandle<JSTaggedValue> value(thread, resultPair.second);
62 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
63 }
64
CreateLinkedListIterator(JSThread * thread,JSHandle<JSTaggedValue> & obj)65 JSHandle<JSTaggedValue> JSAPILinkedListIterator::CreateLinkedListIterator(JSThread *thread,
66 JSHandle<JSTaggedValue> &obj)
67 {
68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
69 if (!obj->IsJSAPILinkedList()) {
70 if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget().IsJSAPILinkedList()) {
71 obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget());
72 } else {
73 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
74 "The Symbol.iterator method cannot be bound");
75 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
76 JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
77 }
78 }
79 JSHandle<JSTaggedValue> iter(factory->NewJSAPILinkedListIterator(JSHandle<JSAPILinkedList>(obj)));
80 return iter;
81 }
82 } // namespace panda::ecmascript
83