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_lightweightmap_iterator.h"
17
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/base/typed_array_helper-inl.h"
20 #include "ecmascript/base/typed_array_helper.h"
21 #include "ecmascript/containers/containers_errors.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/js_api/js_api_lightweightmap.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 JSAPILightWeightMapIterator::Next(EcmaRuntimeCallInfo *argv)
32 {
33 ASSERT(argv != nullptr);
34 JSThread *thread = argv->GetThread();
35 [[maybe_unused]] EcmaHandleScope handleScope(thread);
36 JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
37 if (!input->IsJSAPILightWeightMapIterator()) {
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<JSAPILightWeightMapIterator> iter(input);
43 JSHandle<JSTaggedValue> oldlightWeightMap(thread, iter->GetIteratedLightWeightMap());
44 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
45 JSHandle<JSAPILightWeightMap> lightWeightMap(oldlightWeightMap);
46 IterationKind itemKind = IterationKind(iter->GetIterationKind());
47 if (oldlightWeightMap->IsUndefined()) {
48 return globalConst->GetUndefinedIterResult();
49 }
50 int32_t index = iter->GetNextIndex();
51 int32_t length = static_cast<int32_t>(lightWeightMap->GetSize());
52 if (index >= length) {
53 JSHandle<JSTaggedValue> undefinedHandle = globalConst->GetHandledUndefined();
54 iter->SetIteratedLightWeightMap(thread, undefinedHandle);
55 return globalConst->GetUndefinedIterResult();
56 }
57 iter->SetNextIndex(index + 1);
58
59 JSHandle<JSTaggedValue> key(thread, lightWeightMap->GetKeyAt(thread, lightWeightMap, index));
60 if (itemKind == IterationKind::KEY) {
61 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
62 }
63 JSHandle<JSTaggedValue> value(thread, lightWeightMap->Get(thread, lightWeightMap, key));
64 if (itemKind == IterationKind::VALUE) {
65 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
66 }
67 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
68 JSHandle<TaggedArray> array = factory->NewTaggedArray(2); // 2 means the length of array
69 array->Set(thread, 0, key);
70 array->Set(thread, 1, value);
71 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
72
73 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
74 }
75
CreateLightWeightMapIterator(JSThread * thread,JSHandle<JSTaggedValue> & obj,IterationKind kind)76 JSHandle<JSTaggedValue> JSAPILightWeightMapIterator::CreateLightWeightMapIterator(JSThread *thread,
77 JSHandle<JSTaggedValue> &obj,
78 IterationKind kind)
79 {
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 if (!obj->IsJSAPILightWeightMap()) {
82 if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget().IsJSAPILightWeightMap()) {
83 obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget());
84 } else {
85 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
86 "The Symbol.iterator method cannot be bound");
87 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
88 JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
89 }
90 }
91 JSHandle<JSTaggedValue> iter(factory->NewJSAPILightWeightMapIterator(JSHandle<JSAPILightWeightMap>(obj), kind));
92 return iter;
93 }
94 } // namespace panda::ecmascript
95