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