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