• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "js_api_tree_set_iterator.h"
17 
18 #include "base/builtins_base.h"
19 #include "js_api_tree_set.h"
20 #include "js_array.h"
21 #include "tagged_tree-inl.h"
22 #include "object_factory.h"
23 
24 namespace panda::ecmascript {
25 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)26 JSTaggedValue JSAPITreeSetIterator::Next(EcmaRuntimeCallInfo *argv)
27 {
28     ASSERT(argv);
29     JSThread *thread = argv->GetThread();
30     [[maybe_unused]] EcmaHandleScope handleScope(thread);
31     // Let input be the this value
32     JSHandle<JSTaggedValue> input(BuiltinsBase::GetThis(argv));
33 
34     if (!input->IsJSAPITreeSetIterator()) {
35         THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a tree set iterator", JSTaggedValue::Exception());
36     }
37     JSHandle<JSAPITreeSetIterator> iter(input);
38     // Let it be [[IteratedSet]].
39     JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
40 
41     // If it is undefined, return CreateIterResultObject(undefined, true).
42     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
43     if (iteratedSet->IsUndefined()) {
44         return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue();
45     }
46     JSHandle<TaggedTreeSet> set(thread, JSHandle<JSAPITreeSet>::Cast(iteratedSet)->GetTreeSet());
47     int elements = set->NumberOfElements();
48 
49     JSMutableHandle<TaggedArray> entries(thread, iter->GetEntries());
50     if (elements != static_cast<int>(entries->GetLength())) {
51         entries.Update(TaggedTreeSet::GetArrayFromSet(thread, set).GetTaggedValue());
52         iter->SetEntries(thread, entries);
53     }
54 
55     // Let index be Set.[[NextIndex]].
56     int index = iter->GetNextIndex().GetInt();
57     if (index < elements) {
58         IterationKind itemKind = IterationKind(iter->GetIterationKind().GetInt());
59 
60         int keyIndex = entries->Get(index).GetInt();
61         iter->SetNextIndex(thread, JSTaggedValue(index + 1));
62 
63         JSHandle<JSTaggedValue> key(thread, set->GetKey(keyIndex));
64         // If itemKind is key or value, let result be e.[[Key]].
65         if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
66             return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
67         }
68         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
69         JSHandle<TaggedArray> array(factory->NewTaggedArray(2));  // 2 means the length of array
70         array->Set(thread, 0, key);
71         array->Set(thread, 1, key);
72         JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
73         return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
74     }
75 
76     // Set [[IteratedSet]] to undefined.
77     iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
78     return JSIterator::CreateIterResultObject(thread, globalConst->GetHandledUndefined(), true).GetTaggedValue();
79 }
80 
CreateTreeSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)81 JSHandle<JSTaggedValue> JSAPITreeSetIterator::CreateTreeSetIterator(JSThread *thread,
82                                                                     const JSHandle<JSTaggedValue> &obj,
83                                                                     IterationKind kind)
84 {
85     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
86     if (!obj->IsJSAPITreeSet()) {
87         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not TreeSet", thread->GlobalConstants()->GetHandledUndefined());
88     }
89     JSHandle<JSTaggedValue> iter(factory->NewJSAPITreeSetIterator(JSHandle<JSAPITreeSet>(obj), kind));
90     return iter;
91 }
92 }  // namespace panda::ecmascript
93