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