• 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 "builtins_set.h"
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/js_set.h"
22 #include "ecmascript/js_set_iterator.h"
23 #include "ecmascript/linked_hash_table.h"
24 #include "ecmascript/object_factory.h"
25 #include "ecmascript/tagged_array-inl.h"
26 
27 namespace panda::ecmascript::builtins {
SetConstructor(EcmaRuntimeCallInfo * argv)28 JSTaggedValue BuiltinsSet::SetConstructor(EcmaRuntimeCallInfo *argv)
29 {
30     ASSERT(argv);
31     BUILTINS_API_TRACE(argv->GetThread(), Set, Constructor);
32     JSThread *thread = argv->GetThread();
33     [[maybe_unused]] EcmaHandleScope handleScope(thread);
34     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
35     // 1.If NewTarget is undefined, throw a TypeError exception
36     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
37     if (newTarget->IsUndefined()) {
38         // throw type error
39         THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
40     }
41     // 2.Let set be OrdinaryCreateFromConstructor(NewTarget, "%SetPrototype%", «‍[[SetData]]» ).
42     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
43     JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
44     // 3.returnIfAbrupt()
45     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
46     JSHandle<JSSet> set = JSHandle<JSSet>::Cast(obj);
47     // 3.ReturnIfAbrupt(set).
48     // 4.Set set’s [[SetData]] internal slot to a new empty List.
49     JSHandle<LinkedHashSet> linkedSet = LinkedHashSet::Create(thread);
50     set->SetLinkedSet(thread, linkedSet);
51 
52     // add data into set from iterable
53     // 5.If iterable is not present, let iterable be undefined.
54     // 6.If iterable is either undefined or null, let iter be undefined.
55     JSHandle<JSTaggedValue> iterable(GetCallArg(argv, 0));
56     // 8.If iter is undefined, return set
57     if (iterable->IsUndefined() || iterable->IsNull()) {
58         return set.GetTaggedValue();
59     }
60     // Let adder be Get(set, "add").
61     JSHandle<JSTaggedValue> adderKey(thread->GlobalConstants()->GetHandledAddString());
62     JSHandle<JSTaggedValue> setHandle(set);
63     JSHandle<JSTaggedValue> adder = JSObject::GetProperty(thread, setHandle, adderKey).GetValue();
64     // ReturnIfAbrupt(adder).
65     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
66     // If IsCallable(adder) is false, throw a TypeError exception
67     if (!adder->IsCallable()) {
68         THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
69     }
70     // Let iter be GetIterator(iterable).
71     JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
72     // ReturnIfAbrupt(iter).
73     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue());
74     // values in iterator_result may be a JSArray, values[0] = key values[1]=value, used valueIndex to get value from
75     // jsarray
76     JSHandle<JSTaggedValue> valueIndex(thread, JSTaggedValue(1));
77     JSHandle<JSTaggedValue> next = JSIterator::IteratorStep(thread, iter);
78     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
79     while (!next->IsFalse()) {
80         // Let nextValue be IteratorValue(next).
81         JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
82         // ReturnIfAbrupt(nextValue).
83         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, nextValue.GetTaggedValue());
84         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
85         EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, adder, setHandle, undefined, 1);
86         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, nextValue.GetTaggedValue());
87         info->SetCallArg(nextValue.GetTaggedValue());
88         if (nextValue->IsArray(thread)) {
89             auto prop = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
90             info->SetCallArg(prop.GetTaggedValue());
91         }
92         JSFunction::Call(info);
93         // Let status be Call(adder, set, «nextValue.[[value]]»).
94         if (thread->HasPendingException()) {
95             return JSIterator::IteratorCloseAndReturn(thread, iter);
96         }
97         // Let next be IteratorStep(iter).
98         next = JSIterator::IteratorStep(thread, iter);
99         // ReturnIfAbrupt(next).
100         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
101     }
102     return set.GetTaggedValue();
103 }
104 
Add(EcmaRuntimeCallInfo * argv)105 JSTaggedValue BuiltinsSet::Add(EcmaRuntimeCallInfo *argv)
106 {
107     ASSERT(argv);
108     BUILTINS_API_TRACE(argv->GetThread(), Set, Add);
109     JSThread *thread = argv->GetThread();
110     [[maybe_unused]] EcmaHandleScope handleScope(thread);
111     JSHandle<JSTaggedValue> self = GetThis(argv);
112 
113     // 2.If Type(S) is not Object, throw a TypeError exception.
114     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
115     if (!self->IsJSSet()) {
116         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
117     }
118 
119     JSHandle<JSTaggedValue> value(GetCallArg(argv, 0));
120     JSHandle<JSSet> set(JSTaggedValue::ToObject(thread, self));
121 
122     JSSet::Add(thread, set, value);
123     return set.GetTaggedValue();
124 }
125 
Clear(EcmaRuntimeCallInfo * argv)126 JSTaggedValue BuiltinsSet::Clear(EcmaRuntimeCallInfo *argv)
127 {
128     ASSERT(argv);
129     BUILTINS_API_TRACE(argv->GetThread(), Set, Clear);
130     JSThread *thread = argv->GetThread();
131     [[maybe_unused]] EcmaHandleScope handleScope(thread);
132     JSHandle<JSTaggedValue> self = GetThis(argv);
133 
134     // 2.If Type(S) is not Object, throw a TypeError exception.
135     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
136     if (!self->IsJSSet()) {
137         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
138     }
139     JSHandle<JSSet> set(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, self)));
140     JSSet::Clear(thread, set);
141     return JSTaggedValue::Undefined();
142 }
143 
Delete(EcmaRuntimeCallInfo * argv)144 JSTaggedValue BuiltinsSet::Delete(EcmaRuntimeCallInfo *argv)
145 {
146     ASSERT(argv);
147     BUILTINS_API_TRACE(argv->GetThread(), Set, Delete);
148     JSThread *thread = argv->GetThread();
149     [[maybe_unused]] EcmaHandleScope handleScope(thread);
150     JSHandle<JSTaggedValue> self = GetThis(argv);
151     // 2.If Type(S) is not Object, throw a TypeError exception.
152     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
153     if (!self->IsJSSet()) {
154         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
155     }
156 
157     JSHandle<JSSet> set(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, self)));
158     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
159     bool flag = JSSet::Delete(thread, set, value);
160     return GetTaggedBoolean(flag);
161 }
162 
Has(EcmaRuntimeCallInfo * argv)163 JSTaggedValue BuiltinsSet::Has(EcmaRuntimeCallInfo *argv)
164 {
165     ASSERT(argv);
166     BUILTINS_API_TRACE(argv->GetThread(), Set, Has);
167     JSThread *thread = argv->GetThread();
168     [[maybe_unused]] EcmaHandleScope handleScope(thread);
169     JSHandle<JSTaggedValue> self = GetThis(argv);
170     // 2.If Type(S) is not Object, throw a TypeError exception.
171     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
172     if (!self->IsJSSet()) {
173         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
174     }
175     JSSet *jsSet = JSSet::Cast(*JSTaggedValue::ToObject(thread, self));
176     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
177     bool flag = jsSet->Has(value.GetTaggedValue());
178     return GetTaggedBoolean(flag);
179 }
180 
ForEach(EcmaRuntimeCallInfo * argv)181 JSTaggedValue BuiltinsSet::ForEach([[maybe_unused]] EcmaRuntimeCallInfo *argv)
182 {
183     JSThread *thread = argv->GetThread();
184     BUILTINS_API_TRACE(thread, Set, ForEach);
185     [[maybe_unused]] EcmaHandleScope handleScope(thread);
186     JSHandle<JSTaggedValue> self = GetThis(argv);
187     // 2.If Type(S) is not Object, throw a TypeError exception.
188     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
189     if (!self->IsJSSet()) {
190         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
191     }
192     JSHandle<JSSet> set(self);
193 
194     // 4.If IsCallable(callbackfn) is false, throw a TypeError exception.
195     JSHandle<JSTaggedValue> func(GetCallArg(argv, 0));
196     if (!func->IsCallable()) {
197         THROW_TYPE_ERROR_AND_RETURN(thread, "callbackfn is not callable", JSTaggedValue::Exception());
198     }
199 
200     // 5.If thisArg was supplied, let T be thisArg; else let T be undefined.
201     JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1);
202 
203     // 6.Let entries be the List that is the value of S’s [[SetData]] internal slot.
204     JSMutableHandle<LinkedHashSet> hashSet(thread, set->GetLinkedSet());
205     const int32_t argsLength = 3;
206     int index = 0;
207     int totalElements = hashSet->NumberOfElements() + hashSet->NumberOfDeletedElements();
208     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
209     // 7.Repeat for each e that is an element of entries, in original insertion order
210     while (index < totalElements) {
211         JSHandle<JSTaggedValue> key(thread, hashSet->GetKey(index++));
212         // a. If e is not empty, then
213         if (!key->IsHole()) {
214             EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(
215                 thread, func, thisArg, undefined, argsLength);
216             RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
217             info->SetCallArg(key.GetTaggedValue(), key.GetTaggedValue(), set.GetTaggedValue());
218             // i. Let funcResult be Call(callbackfn, T, «e, e, S»).
219             JSTaggedValue ret = JSFunction::Call(info);  // 3: three args
220             // ii. ReturnIfAbrupt(funcResult).
221             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
222             // Maybe add or delete
223             JSTaggedValue nextTable = hashSet->GetNextTable();
224             while (!nextTable.IsHole()) {
225                 index -= hashSet->GetDeletedElementsAt(index);
226                 hashSet.Update(nextTable);
227                 nextTable = hashSet->GetNextTable();
228             }
229             totalElements = hashSet->NumberOfElements() + hashSet->NumberOfDeletedElements();
230         }
231     }
232     return JSTaggedValue::Undefined();
233 }
234 
Species(EcmaRuntimeCallInfo * argv)235 JSTaggedValue BuiltinsSet::Species([[maybe_unused]] EcmaRuntimeCallInfo *argv)
236 {
237     return GetThis(argv).GetTaggedValue();
238 }
239 
GetSize(EcmaRuntimeCallInfo * argv)240 JSTaggedValue BuiltinsSet::GetSize(EcmaRuntimeCallInfo *argv)
241 {
242     ASSERT(argv);
243     BUILTINS_API_TRACE(argv->GetThread(), Set, GetSize);
244     JSThread *thread = argv->GetThread();
245     [[maybe_unused]] EcmaHandleScope handleScope(thread);
246     JSHandle<JSTaggedValue> self(GetThis(argv));
247     // 2.If Type(S) is not Object, throw a TypeError exception.
248     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
249     if (!self->IsJSSet()) {
250         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
251     }
252     JSSet *jsSet = JSSet::Cast(*JSTaggedValue::ToObject(thread, self));
253     int count = jsSet->GetSize();
254     return JSTaggedValue(count);
255 }
256 
Entries(EcmaRuntimeCallInfo * argv)257 JSTaggedValue BuiltinsSet::Entries(EcmaRuntimeCallInfo *argv)
258 {
259     ASSERT(argv);
260     BUILTINS_API_TRACE(argv->GetThread(), Set, Entries);
261     JSThread *thread = argv->GetThread();
262     [[maybe_unused]] EcmaHandleScope handleScope(thread);
263     JSHandle<JSTaggedValue> self = GetThis(argv);
264     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::KEY_AND_VALUE);
265     return iter.GetTaggedValue();
266 }
267 
Values(EcmaRuntimeCallInfo * argv)268 JSTaggedValue BuiltinsSet::Values(EcmaRuntimeCallInfo *argv)
269 {
270     ASSERT(argv);
271     BUILTINS_API_TRACE(argv->GetThread(), Set, Values);
272     JSThread *thread = argv->GetThread();
273     [[maybe_unused]] EcmaHandleScope handleScope(thread);
274     JSHandle<JSTaggedValue> self = GetThis(argv);
275     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::VALUE);
276     return iter.GetTaggedValue();
277 }
278 }  // namespace panda::ecmascript::builtins
279