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