• 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 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/internal_call_params.h"
20 #include "ecmascript/js_invoker.h"
21 #include "ecmascript/js_set.h"
22 #include "ecmascript/js_set_iterator.h"
23 #include "ecmascript/linked_hash_table-inl.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(factory->NewFromCanBeCompressString("add"));
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     InternalCallParams *arguments = thread->GetInternalCallParams();
79     while (!next->IsFalse()) {
80         // ReturnIfAbrupt(next).
81         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
82         // Let nextValue be IteratorValue(next).
83         JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
84         // ReturnIfAbrupt(nextValue).
85         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, nextValue.GetTaggedValue());
86         arguments->MakeArgv(nextValue);
87         if (nextValue->IsArray(thread)) {
88             auto prop = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
89             arguments->MakeArgv(prop);
90         }
91         JSTaggedValue ret = JSFunction::Call(thread, adder, JSHandle<JSTaggedValue>(set), 1, arguments->GetArgv());
92         // Let status be Call(adder, set, «nextValue.[[value]]»).
93         JSHandle<JSTaggedValue> status(thread, ret);
94 
95         if (thread->HasPendingException()) {
96             return JSIterator::IteratorCloseAndReturn(thread, iter, status);
97         }
98         // Let next be IteratorStep(iter).
99         next = JSIterator::IteratorStep(thread, iter);
100     }
101     return set.GetTaggedValue();
102 }
103 
Add(EcmaRuntimeCallInfo * argv)104 JSTaggedValue BuiltinsSet::Add(EcmaRuntimeCallInfo *argv)
105 {
106     ASSERT(argv);
107     BUILTINS_API_TRACE(argv->GetThread(), Set, Add);
108     JSThread *thread = argv->GetThread();
109     [[maybe_unused]] EcmaHandleScope handleScope(thread);
110     JSHandle<JSTaggedValue> self = GetThis(argv);
111 
112     // 2.If Type(S) is not Object, throw a TypeError exception.
113     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
114     if (!self->IsJSSet()) {
115         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
116     }
117 
118     JSHandle<JSTaggedValue> value(GetCallArg(argv, 0));
119     JSHandle<JSSet> set(JSTaggedValue::ToObject(thread, self));
120 
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(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, 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(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, 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(*JSTaggedValue::ToObject(thread, self));
175     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
176     bool flag = jsSet->Has(value.GetTaggedValue());
177     return GetTaggedBoolean(flag);
178 }
179 
ForEach(EcmaRuntimeCallInfo * argv)180 JSTaggedValue BuiltinsSet::ForEach([[maybe_unused]] EcmaRuntimeCallInfo *argv)
181 {
182     JSThread *thread = argv->GetThread();
183     [[maybe_unused]] EcmaHandleScope handleScope(thread);
184     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
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(thread, JSSet::Cast(*JSTaggedValue::ToObject(thread, 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     // composed arguments
203     JSHandle<JSTaggedValue> iter(factory->NewJSSetIterator(set, IterationKind::KEY));
204     JSHandle<JSTaggedValue> result = JSIterator::IteratorStep(thread, iter);
205     InternalCallParams *arguments = thread->GetInternalCallParams();
206     while (!result->IsFalse()) {
207         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result.GetTaggedValue());
208         JSHandle<JSTaggedValue> value = JSIterator::IteratorValue(thread, result);
209         // Let funcResult be Call(callbackfn, T, «e, e, S»).
210         arguments->MakeArgv(value, value, JSHandle<JSTaggedValue>(set));
211         JSTaggedValue ret = JSFunction::Call(thread, func, thisArg, 3, arguments->GetArgv());  // 3: three args
212         // returnIfAbrupt
213         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, ret);
214         result = JSIterator::IteratorStep(thread, iter);
215     }
216     return JSTaggedValue::Undefined();
217 }
218 
Species(EcmaRuntimeCallInfo * argv)219 JSTaggedValue BuiltinsSet::Species([[maybe_unused]] EcmaRuntimeCallInfo *argv)
220 {
221     return GetThis(argv).GetTaggedValue();
222 }
223 
GetSize(EcmaRuntimeCallInfo * argv)224 JSTaggedValue BuiltinsSet::GetSize(EcmaRuntimeCallInfo *argv)
225 {
226     ASSERT(argv);
227     BUILTINS_API_TRACE(argv->GetThread(), Set, GetSize);
228     JSThread *thread = argv->GetThread();
229     [[maybe_unused]] EcmaHandleScope handleScope(thread);
230     JSHandle<JSTaggedValue> self(GetThis(argv));
231     // 2.If Type(S) is not Object, throw a TypeError exception.
232     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
233     if (!self->IsJSSet()) {
234         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSSet", JSTaggedValue::Exception());
235     }
236     JSSet *jsSet = JSSet::Cast(*JSTaggedValue::ToObject(thread, self));
237     int count = jsSet->GetSize();
238     return JSTaggedValue(count);
239 }
240 
Entries(EcmaRuntimeCallInfo * argv)241 JSTaggedValue BuiltinsSet::Entries(EcmaRuntimeCallInfo *argv)
242 {
243     ASSERT(argv);
244     BUILTINS_API_TRACE(argv->GetThread(), Set, Entries);
245     JSThread *thread = argv->GetThread();
246     [[maybe_unused]] EcmaHandleScope handleScope(thread);
247     JSHandle<JSTaggedValue> self = GetThis(argv);
248     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::KEY_AND_VALUE);
249     return iter.GetTaggedValue();
250 }
251 
Values(EcmaRuntimeCallInfo * argv)252 JSTaggedValue BuiltinsSet::Values(EcmaRuntimeCallInfo *argv)
253 {
254     ASSERT(argv);
255     BUILTINS_API_TRACE(argv->GetThread(), Set, Values);
256     JSThread *thread = argv->GetThread();
257     [[maybe_unused]] EcmaHandleScope handleScope(thread);
258     JSHandle<JSTaggedValue> self = GetThis(argv);
259     JSHandle<JSTaggedValue> iter = JSSetIterator::CreateSetIterator(thread, self, IterationKind::VALUE);
260     return iter.GetTaggedValue();
261 }
262 }  // namespace panda::ecmascript::builtins
263