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