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 if (nextValue->IsArray(thread)) {
91 auto prop = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
92 info->SetCallArg(prop.GetTaggedValue());
93 }
94 // Let status be Call(adder, weakset, «nextValue.[[value]]»).
95 JSFunction::Call(info);
96 // If status is an abrupt completion, return IteratorClose(iter, status).
97 if (thread->HasPendingException()) {
98 return JSIterator::IteratorCloseAndReturn(thread, iter);
99 }
100 // Let next be IteratorStep(iter).
101 next = JSIterator::IteratorStep(thread, iter);
102 // ReturnIfAbrupt(next).
103 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
104 }
105 return weakSet.GetTaggedValue();
106 }
107
Add(EcmaRuntimeCallInfo * argv)108 JSTaggedValue BuiltinsWeakSet::Add(EcmaRuntimeCallInfo *argv)
109 {
110 ASSERT(argv);
111 BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Add);
112 JSThread *thread = argv->GetThread();
113 [[maybe_unused]] EcmaHandleScope handleScope(thread);
114 JSHandle<JSTaggedValue> self = GetThis(argv);
115
116 // 2.If Type(S) is not Object, throw a TypeError exception.
117 // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception.
118 if (!self->IsJSWeakSet()) {
119 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
120 }
121
122 JSHandle<JSTaggedValue> value(GetCallArg(argv, 0));
123 // 4.If CanBeHeldWeakly(value) is false, throw a TypeError exception.
124 if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
125 THROW_TYPE_ERROR_AND_RETURN(thread, "invalid value used in weak set", JSTaggedValue::Exception());
126 }
127
128 JSHandle<JSWeakSet> weakSet(thread, JSWeakSet::Cast(*JSTaggedValue::ToObject(thread, self)));
129 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
130 JSWeakSet::Add(thread, weakSet, value);
131 return weakSet.GetTaggedValue();
132 }
133
Delete(EcmaRuntimeCallInfo * argv)134 JSTaggedValue BuiltinsWeakSet::Delete(EcmaRuntimeCallInfo *argv)
135 {
136 ASSERT(argv);
137 BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Delete);
138 JSThread *thread = argv->GetThread();
139 [[maybe_unused]] EcmaHandleScope handleScope(thread);
140 JSHandle<JSTaggedValue> self = GetThis(argv);
141 // 2.If Type(S) is not Object, throw a TypeError exception.
142 // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception.
143 if (!self->IsJSWeakSet()) {
144 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
145 }
146
147 JSHandle<JSWeakSet> weakSet(thread, JSWeakSet::Cast(*JSTaggedValue::ToObject(thread, self)));
148 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
149 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
150 // 4.If CanBeHeldWeakly(value) is false, return false.
151 if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
152 GetTaggedBoolean(false);
153 }
154 return GetTaggedBoolean(JSWeakSet::Delete(thread, weakSet, value));
155 }
156
Has(EcmaRuntimeCallInfo * argv)157 JSTaggedValue BuiltinsWeakSet::Has(EcmaRuntimeCallInfo *argv)
158 {
159 ASSERT(argv);
160 BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Has);
161 JSThread *thread = argv->GetThread();
162 [[maybe_unused]] EcmaHandleScope handleScope(thread);
163 JSHandle<JSTaggedValue> self = GetThis(argv);
164 // 2.If Type(S) is not Object, throw a TypeError exception.
165 // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
166 if (!self->IsJSWeakSet()) {
167 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
168 }
169 JSWeakSet *jsWeakSet = JSWeakSet::Cast(*JSTaggedValue::ToObject(thread, self));
170 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
171 JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
172 // 4.If CanBeHeldWeakly(value) is false, return false.
173 if (!JSTaggedValue::CanBeHeldWeakly(thread, value)) {
174 GetTaggedBoolean(false);
175 }
176 return GetTaggedBoolean(jsWeakSet->Has(value.GetTaggedValue()));
177 }
178 } // namespace panda::ecmascript::builtins
179