• 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 "ecmascript/js_set_iterator.h"
17 
18 #include "ecmascript/builtins/builtins_errors.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/linked_hash_table.h"
21 
22 namespace panda::ecmascript {
23 using BuiltinsBase = base::BuiltinsBase;
Next(EcmaRuntimeCallInfo * argv)24 JSTaggedValue JSSetIterator::Next(EcmaRuntimeCallInfo *argv)
25 {
26     ASSERT(argv);
27     JSThread *thread = argv->GetThread();
28     [[maybe_unused]] EcmaHandleScope handleScope(thread);
29     // 1.If Type(O) is not Object, throw a TypeError exception.
30     JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
31     return NextInternal(thread, thisObj);
32 }
33 
NextInternal(JSThread * thread,JSHandle<JSTaggedValue> thisObj)34 JSTaggedValue JSSetIterator::NextInternal(JSThread *thread, JSHandle<JSTaggedValue> thisObj)
35 {
36     // 3.If O does not have all of the internal slots of a Set Iterator Instance (23.2.5.3), throw a TypeError
37     // exception.
38     if (!thisObj->IsJSSetIterator()) {
39         THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not a set iterator", JSTaggedValue::Exception());
40     }
41     JSHandle<JSSetIterator> iter(thisObj);
42     iter->Update(thread);
43     JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
44     // 4.Let s be O.[[IteratedSet]].
45     JSHandle<JSTaggedValue> iteratedSet(thread, iter->GetIteratedSet());
46 
47     // 5.Let index be O.[[SetNextIndex]].
48     int index = static_cast<int>(iter->GetNextIndex());
49     IterationKind itemKind = iter->GetIterationKind();
50     // 7.If s is undefined, return CreateIterResultObject(undefined, true).
51     if (iteratedSet->IsUndefined()) {
52         return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
53     }
54     JSHandle<LinkedHashSet> set(iteratedSet);
55     int totalElements = set->NumberOfElements() + set->NumberOfDeletedElements();
56 
57     while (index < totalElements) {
58         if (!set->GetKey(index).IsHole()) {
59             iter->SetNextIndex(index + 1);
60             JSHandle<JSTaggedValue> key(thread, set->GetKey(index));
61             // If itemKind is value
62             if (itemKind == IterationKind::VALUE || itemKind == IterationKind::KEY) {
63                 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
64             }
65             // If itemKind is key+value, then
66             ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67             JSHandle<TaggedArray> array(factory->NewTaggedArray(2));  // 2: key and value pair
68             array->Set(thread, 0, key);
69             array->Set(thread, 1, key);
70             JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, array));
71             return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
72         }
73         index++;
74     }
75     // 13.Set O.[[IteratedSet]] to undefined.
76     iter->SetIteratedSet(thread, JSTaggedValue::Undefined());
77     return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
78 }
79 
Update(const JSThread * thread)80 void JSSetIterator::Update(const JSThread *thread)
81 {
82     DISALLOW_GARBAGE_COLLECTION;
83     JSTaggedValue iteratedSet = GetIteratedSet();
84     if (iteratedSet.IsUndefined()) {
85         return;
86     }
87     LinkedHashSet *set = LinkedHashSet::Cast(iteratedSet.GetTaggedObject());
88     if (set->GetNextTable().IsHole()) {
89         return;
90     }
91     int index = static_cast<int>(GetNextIndex());
92     JSTaggedValue nextTable = set->GetNextTable();
93     while (!nextTable.IsHole()) {
94         index -= set->GetDeletedElementsAt(index);
95         set = LinkedHashSet::Cast(nextTable.GetTaggedObject());
96         nextTable = set->GetNextTable();
97     }
98     SetIteratedSet(thread, JSTaggedValue(set));
99     SetNextIndex(index);
100 }
101 
CreateSetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,IterationKind kind)102 JSHandle<JSTaggedValue> JSSetIterator::CreateSetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
103                                                          IterationKind kind)
104 {
105     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
106     if (!obj->IsJSSet()) {
107         JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
108         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", undefinedHandle);
109     }
110     JSHandle<JSTaggedValue> iter(factory->NewJSSetIterator(JSHandle<JSSet>(obj), kind));
111     return iter;
112 }
113 }  // namespace panda::ecmascript
114