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_lightweightset_iterator.h"
17
18 #include "ecmascript/base/typed_array_helper-inl.h"
19 #include "ecmascript/base/typed_array_helper.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_lightweightset.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 JSAPILightWeightSetIterator::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->IsJSAPILightWeightSetIterator()) {
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<JSAPILightWeightSetIterator> iter(input);
43 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
44 JSHandle<JSTaggedValue> lightWeightSet(thread, iter->GetIteratedLightWeightSet());
45 uint32_t index = iter->GetNextIndex();
46 IterationKind itemKind = IterationKind(iter->GetIterationKind());
47 if (lightWeightSet->IsUndefined()) {
48 return globalConst->GetUndefinedIterResult();
49 }
50 uint32_t length = 0;
51 if (lightWeightSet->IsJSAPILightWeightSet()) {
52 length = JSHandle<JSAPILightWeightSet>(lightWeightSet)->GetLength();
53 }
54 if (index >= length) {
55 JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
56 iter->SetIteratedLightWeightSet(thread, undefinedHandle);
57 return globalConst->GetUndefinedIterResult();
58 }
59 iter->SetNextIndex(index + 1);
60 JSHandle<TaggedArray> valueArray(
61 thread, TaggedArray::Cast(JSHandle<JSAPILightWeightSet>(lightWeightSet)->GetValues().GetTaggedObject()));
62 JSHandle<JSTaggedValue> value(thread, valueArray->Get(index));
63 if (itemKind == IterationKind::VALUE) {
64 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
65 }
66 TaggedArray *hashArray =
67 TaggedArray::Cast(JSHandle<JSAPILightWeightSet>(lightWeightSet)->GetHashes().GetTaggedObject());
68 JSHandle<JSTaggedValue> keyHandle(thread, hashArray->Get(index));
69 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
70 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
71 array->Set(thread, 0, keyHandle);
72 array->Set(thread, 1, value);
73 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
74 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
75 }
76 } // namespace panda::ecmascript
77