• 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/builtins/builtins_weak_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_iterator.h"
22 #include "ecmascript/js_weak_container.h"
23 #include "ecmascript/linked_hash_table.h"
24 #include "ecmascript/object_factory.h"
25 
26 namespace panda::ecmascript::builtins {
WeakSetConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsWeakSet::WeakSetConstructor(EcmaRuntimeCallInfo *argv)
28 {
29     ASSERT(argv);
30     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Constructor);
31     JSThread *thread = argv->GetThread();
32     [[maybe_unused]] EcmaHandleScope handleScope(thread);
33     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
34     // 1.If NewTarget is undefined, throw a TypeError exception
35     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
36     if (newTarget->IsUndefined()) {
37         // throw type error
38         THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
39     }
40     // 2.Let weakset be OrdinaryCreateFromConstructor(NewTarget, "%WeakSetPrototype%", «‍[[WeakSetData]]» ).
41     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
42     JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
43     // 3.returnIfAbrupt()
44     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
45     JSHandle<JSWeakSet> weakSet = JSHandle<JSWeakSet>::Cast(obj);
46     // 3.ReturnIfAbrupt(weakSet).
47     // 4.WeakSet set’s [[WeakSetData]] internal slot to a new empty List.
48     JSHandle<LinkedHashSet> linkedSet = LinkedHashSet::Create(thread);
49     weakSet->SetLinkedSet(thread, linkedSet);
50 
51     // add data into weakset from iterable
52     // 5.If iterable is not present, let iterable be undefined.
53     // 6.If iterable is either undefined or null, let iter be undefined.
54     JSHandle<JSTaggedValue> iterable(GetCallArg(argv, 0));
55     // 8.If iter is undefined, return weakset
56     if (iterable->IsUndefined() || iterable->IsNull()) {
57         return weakSet.GetTaggedValue();
58     }
59     // Let adder be Get(weakset, "add").
60     JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledAddString();
61     JSHandle<JSTaggedValue> weakSetHandle(weakSet);
62     JSHandle<JSTaggedValue> adder = JSObject::GetProperty(thread, weakSetHandle, adderKey).GetValue();
63     // ReturnIfAbrupt(adder).
64     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
65     // If IsCallable(adder) is false, throw a TypeError exception
66     if (!adder->IsCallable()) {
67         THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
68     }
69     // Let iter be GetIterator(iterable).
70     JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
71 
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     JSMutableHandle<JSTaggedValue> status(thread, JSTaggedValue::Undefined());
80     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
81     while (!next->IsFalse()) {
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         EcmaRuntimeCallInfo *info =
87             EcmaInterpreter::NewRuntimeCallInfo(thread, adder, JSHandle<JSTaggedValue>(weakSet), undefined, 1);
88         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
89         info->SetCallArg(nextValue.GetTaggedValue());
90         // Let status be Call(adder, weakset, «nextValue.[[value]]»).
91         JSFunction::Call(info);
92         // If status is an abrupt completion, return IteratorClose(iter, status).
93         if (thread->HasPendingException()) {
94             return JSIterator::IteratorCloseAndReturn(thread, iter);
95         }
96         // Let next be IteratorStep(iter).
97         next = JSIterator::IteratorStep(thread, iter);
98         // ReturnIfAbrupt(next).
99         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
100     }
101     return weakSet.GetTaggedValue();
102 }
103 
Add(EcmaRuntimeCallInfo * argv)104 JSTaggedValue BuiltinsWeakSet::Add(EcmaRuntimeCallInfo *argv)
105 {
106     ASSERT(argv);
107     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, 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 [[WeakSetData]] internal slot, throw a TypeError exception.
114     if (!self->IsJSWeakSet()) {
115         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
116     }
117 
118     JSHandle<JSTaggedValue> value(GetCallArg(argv, 0));
119     // 4.If CanBeHeldWeakly(value) is false, throw a TypeError exception.
120     if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
121         THROW_TYPE_ERROR_AND_RETURN(thread, "invalid value used in weak set", JSTaggedValue::Exception());
122     }
123 
124     JSHandle<JSWeakSet> weakSet(self);
125     JSWeakSet::Add(thread, weakSet, value);
126     return weakSet.GetTaggedValue();
127 }
128 
Delete(EcmaRuntimeCallInfo * argv)129 JSTaggedValue BuiltinsWeakSet::Delete(EcmaRuntimeCallInfo *argv)
130 {
131     ASSERT(argv);
132     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Delete);
133     JSThread *thread = argv->GetThread();
134     [[maybe_unused]] EcmaHandleScope handleScope(thread);
135     JSHandle<JSTaggedValue> self = GetThis(argv);
136     // 2.If Type(S) is not Object, throw a TypeError exception.
137     // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception.
138     if (!self->IsJSWeakSet()) {
139         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
140     }
141 
142     JSHandle<JSWeakSet> weakSet(self);
143     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
144     // 4.If CanBeHeldWeakly(value) is false, return false.
145     if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
146         GetTaggedBoolean(false);
147     }
148     return GetTaggedBoolean(JSWeakSet::Delete(thread, weakSet, value));
149 }
150 
Has(EcmaRuntimeCallInfo * argv)151 JSTaggedValue BuiltinsWeakSet::Has(EcmaRuntimeCallInfo *argv)
152 {
153     ASSERT(argv);
154     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Has);
155     JSThread *thread = argv->GetThread();
156     [[maybe_unused]] EcmaHandleScope handleScope(thread);
157     JSHandle<JSTaggedValue> self = GetThis(argv);
158     // 2.If Type(S) is not Object, throw a TypeError exception.
159     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
160     if (!self->IsJSWeakSet()) {
161         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
162     }
163     JSWeakSet *jsWeakSet = JSWeakSet::Cast(self.GetTaggedValue().GetTaggedObject());
164     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
165     // 4.If CanBeHeldWeakly(value) is false, return false.
166     if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
167         GetTaggedBoolean(false);
168     }
169     return GetTaggedBoolean(jsWeakSet->Has(thread, value.GetTaggedValue()));
170 }
171 }  // namespace panda::ecmascript::builtins
172