• 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_tree_set_iterator.h"
17 
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/containers/containers_errors.h"
20 #include "ecmascript/js_api/js_api_tree_set.h"
21 #include "ecmascript/js_array.h"
22 #include "ecmascript/object_factory.h"
23 #include "ecmascript/tagged_tree.h"
24 
25 namespace panda::ecmascript {
26 using BuiltinsBase = base::BuiltinsBase;
27 using ContainerError = containers::ContainerError;
28 using ErrorFlag = containers::ErrorFlag;
Next(EcmaRuntimeCallInfo * argv)29 JSTaggedValue JSAPITreeSetIterator::Next(EcmaRuntimeCallInfo *argv)
30 {
31     ASSERT(argv);
32     JSThread *thread = argv->GetThread();
33     [[maybe_unused]] EcmaHandleScope handleScope(thread);
34     // Let input be the this value
35     JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
36 
37     if (!input->IsJSAPITreeSetIterator()) {
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<JSAPITreeSetIterator> iter(input);
43     // Let it be [[IteratedSet]].
44     JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
45 
46     // If it is undefined, return undefinedIteratorResult.
47     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
48     if (iteratedSet->IsUndefined()) {
49         return globalConst->GetUndefinedIterResult();
50     }
51     JSHandle<TaggedTreeSet> set(thread, JSHandle<JSAPITreeSet>::Cast(iteratedSet)->GetTreeSet());
52     uint32_t elements = static_cast<uint32_t>(set->NumberOfElements());
53 
54     JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries());
55     if ((iter->GetEntries().IsHole()) || (elements != entries->GetLength())) {
56         entries.Update(TaggedTreeSet::GetArrayFromSet(thread, set).GetTaggedValue());
57         iter->SetEntries(thread, entries);
58     }
59 
60     // Let index be Set.[[NextIndex]].
61     uint32_t index = static_cast<uint32_t>(iter->GetNextIndex());
62     if (index < elements) {
63         IterationKind itemKind = IterationKind(iter->GetIterationKind());
64 
65         int keyIndex = entries->Get(index).GetInt();
66         iter->SetNextIndex(index + 1);
67 
68         JSHandle<JSTaggedValue> key(thread, set->GetKey(keyIndex));
69         // If itemKind is key or value, let result be e.[[Key]].
70         if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
71             return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
72         }
73         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
74         JSHandle<TaggedArray> array = factory->NewTaggedArray(2);  // 2 means the length of array
75         array->Set(thread, 0, key);
76         array->Set(thread, 1, key);
77         JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
78         return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
79     }
80 
81     // Set [[IteratedSet]] to undefined.
82     iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
83     return globalConst->GetUndefinedIterResult();
84 }
85 
CreateTreeSetIterator(JSThread * thread,JSHandle<JSTaggedValue> & obj,IterationKind kind)86 JSHandle<JSTaggedValue> JSAPITreeSetIterator::CreateTreeSetIterator(JSThread *thread,
87                                                                     JSHandle<JSTaggedValue> &obj,
88                                                                     IterationKind kind)
89 {
90     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91     if (!obj->IsJSAPITreeSet()) {
92         if (obj->IsJSProxy() && JSHandle<JSProxy>::Cast(obj)->GetTarget().IsJSAPITreeSet()) {
93             obj = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(obj)->GetTarget());
94         } else {
95             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
96                                                                 "The Symbol.iterator method cannot be bound");
97             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
98                                              JSHandle<JSTaggedValue>(thread, JSTaggedValue::Exception()));
99         }
100     }
101     JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeSetIterator(JSHandle<JSAPITreeSet>(obj), kind));
102     return iter;
103 }
104 }  // namespace panda::ecmascript
105