• 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     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
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     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
141     JSSet::Clear(thread, set);
142     return JSTaggedValue::Undefined();
143 }
144 
Delete(EcmaRuntimeCallInfo * argv)145 JSTaggedValue BuiltinsSet::Delete(EcmaRuntimeCallInfo *argv)
146 {
147     ASSERT(argv);
148     BUILTINS_API_TRACE(argv->GetThread(), Set, Delete);
149     JSThread *thread = argv->GetThread();
150     [[maybe_unused]] EcmaHandleScope handleScope(thread);
151     JSHandle<JSTaggedValue> self = GetThis(argv);
152     // 2.If Type(S) is not Object, throw a TypeError exception.
153     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
154     if (!self->IsJSSet()) {
155         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
156     }
157 
158     JSHandle<JSSet> set(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, self)));
159     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
160     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
161     bool flag = JSSet::Delete(thread, set, value);
162     return GetTaggedBoolean(flag);
163 }
164 
Has(EcmaRuntimeCallInfo * argv)165 JSTaggedValue BuiltinsSet::Has(EcmaRuntimeCallInfo *argv)
166 {
167     ASSERT(argv);
168     BUILTINS_API_TRACE(argv->GetThread(), Set, Has);
169     JSThread *thread = argv->GetThread();
170     [[maybe_unused]] EcmaHandleScope handleScope(thread);
171     JSHandle<JSTaggedValue> self = GetThis(argv);
172     // 2.If Type(S) is not Object, throw a TypeError exception.
173     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
174     if (!self->IsJSSet()) {
175         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
176     }
177     JSSet *jsSet = JSSet::Cast(*JSTaggedValue::ToObject(thread, self));
178     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
179     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
180     bool flag = jsSet->Has(value.GetTaggedValue());
181     return GetTaggedBoolean(flag);
182 }
183 
ForEach(EcmaRuntimeCallInfo * argv)184 JSTaggedValue BuiltinsSet::ForEach(EcmaRuntimeCallInfo *argv)
185 {
186     JSThread *thread = argv->GetThread();
187     BUILTINS_API_TRACE(thread, Set, ForEach);
188     [[maybe_unused]] EcmaHandleScope handleScope(thread);
189     JSHandle<JSTaggedValue> self = GetThis(argv);
190     // 2.If Type(S) is not Object, throw a TypeError exception.
191     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
192     if (!self->IsJSSet()) {
193         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
194     }
195     JSHandle<JSSet> set(self);
196 
197     // 4.If IsCallable(callbackfn) is false, throw a TypeError exception.
198     JSHandle<JSTaggedValue> func(GetCallArg(argv, 0));
199     if (!func->IsCallable()) {
200         THROW_TYPE_ERROR_AND_RETURN(thread, "callbackfn is not callable", JSTaggedValue::Exception());
201     }
202 
203     // 5.If thisArg was supplied, let T be thisArg; else let T be undefined.
204     JSHandle<JSTaggedValue> thisArg = GetCallArg(argv, 1);
205 
206     // 6.Let entries be the List that is the value of S’s [[SetData]] internal slot.
207     JSMutableHandle<LinkedHashSet> hashSet(thread, set->GetLinkedSet());
208     const uint32_t argsLength = 3;
209     int index = 0;
210     int totalElements = hashSet->NumberOfElements() + hashSet->NumberOfDeletedElements();
211     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
212     // 7.Repeat for each e that is an element of entries, in original insertion order
213     while (index < totalElements) {
214         JSHandle<JSTaggedValue> key(thread, hashSet->GetKey(index++));
215         // a. If e is not empty, then
216         if (!key->IsHole()) {
217             EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(
218                 thread, func, thisArg, undefined, argsLength);
219             RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
220             info->SetCallArg(key.GetTaggedValue(), key.GetTaggedValue(), set.GetTaggedValue());
221             // i. Let funcResult be Call(callbackfn, T, «e, e, S»).
222             JSTaggedValue ret = JSFunction::Call(info);  // 3: three args
223             // ii. ReturnIfAbrupt(funcResult).
224             RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
225             // Maybe add or delete
226             JSTaggedValue nextTable = hashSet->GetNextTable();
227             while (!nextTable.IsHole()) {
228                 index -= hashSet->GetDeletedElementsAt(index);
229                 hashSet.Update(nextTable);
230                 nextTable = hashSet->GetNextTable();
231             }
232             totalElements = hashSet->NumberOfElements() + hashSet->NumberOfDeletedElements();
233         }
234     }
235     return JSTaggedValue::Undefined();
236 }
237 
Species(EcmaRuntimeCallInfo * argv)238 JSTaggedValue BuiltinsSet::Species(EcmaRuntimeCallInfo *argv)
239 {
240     return GetThis(argv).GetTaggedValue();
241 }
242 
GetSize(EcmaRuntimeCallInfo * argv)243 JSTaggedValue BuiltinsSet::GetSize(EcmaRuntimeCallInfo *argv)
244 {
245     ASSERT(argv);
246     BUILTINS_API_TRACE(argv->GetThread(), Set, GetSize);
247     JSThread *thread = argv->GetThread();
248     [[maybe_unused]] EcmaHandleScope handleScope(thread);
249     JSHandle<JSTaggedValue> self(GetThis(argv));
250     // 2.If Type(S) is not Object, throw a TypeError exception.
251     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
252     if (!self->IsJSSet()) {
253         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
254     }
255     JSSet *jsSet = JSSet::Cast(*JSTaggedValue::ToObject(thread, self));
256     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
257     uint32_t count = jsSet->GetSize();
258     return JSTaggedValue(count);
259 }
260 
Entries(EcmaRuntimeCallInfo * argv)261 JSTaggedValue BuiltinsSet::Entries(EcmaRuntimeCallInfo *argv)
262 {
263     ASSERT(argv);
264     BUILTINS_API_TRACE(argv->GetThread(), Set, Entries);
265     JSThread *thread = argv->GetThread();
266     [[maybe_unused]] EcmaHandleScope handleScope(thread);
267     JSHandle<JSTaggedValue> self = GetThis(argv);
268     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::KEY_AND_VALUE);
269     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
270     return iter.GetTaggedValue();
271 }
272 
Values(EcmaRuntimeCallInfo * argv)273 JSTaggedValue BuiltinsSet::Values(EcmaRuntimeCallInfo *argv)
274 {
275     ASSERT(argv);
276     BUILTINS_API_TRACE(argv->GetThread(), Set, Values);
277     JSThread *thread = argv->GetThread();
278     [[maybe_unused]] EcmaHandleScope handleScope(thread);
279     JSHandle<JSTaggedValue> self = GetThis(argv);
280     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::VALUE);
281     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
282     return iter.GetTaggedValue();
283 }
284 }  // namespace panda::ecmascript::builtins
285