• 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_plain_array_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_plain_array.h"
24 #include "ecmascript/js_array.h"
25 #include "ecmascript/object_factory.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 JSAPIPlainArrayIterator::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->IsJSAPIPlainArrayIterator()) {
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<JSAPIPlainArrayIterator> iter(input);
43     JSHandle<JSTaggedValue> plainArray(thread, iter->GetIteratedPlainArray());
44     JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
45     if (plainArray->IsUndefined()) {
46         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
47     }
48 
49     JSHandle<JSAPIPlainArray> apiPlainArray(plainArray);
50     ASSERT(plainArray->IsJSAPIPlainArray());
51 
52     int32_t length = apiPlainArray->GetLength();
53     int32_t index = iter->GetNextIndex();
54     if (index >= length) {
55         iter->SetIteratedPlainArray(thread, undefinedHandle);
56         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
57     }
58     iter->SetNextIndex(index + 1);
59     JSHandle<TaggedArray> valueArray(thread, TaggedArray::Cast(apiPlainArray->GetValues().GetTaggedObject()));
60     JSHandle<JSTaggedValue> value(thread, valueArray->Get(index));
61     JSHandle<TaggedArray> keyArray(thread, TaggedArray::
62                                    Cast(JSHandle<JSAPIPlainArray>(plainArray)->GetKeys().GetTaggedObject()));
63     JSHandle<JSTaggedValue> keyHandle(thread, keyArray->Get(index));
64     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65     JSHandle<TaggedArray> array(factory->NewTaggedArray(2)); // 2 means the length of array
66     array->Set(thread, 0, keyHandle);
67     array->Set(thread, 1, value);
68     JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
69     return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
70 }
71 } // namespace panda::ecmascript
72