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