• 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_weak_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_iterator.h"
22 #include "ecmascript/js_weak_container.h"
23 #include "ecmascript/linked_hash_table-inl.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(factory->NewFromCanBeCompressString("add"));
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     JSMutableHandle<JSTaggedValue> status(thread, JSTaggedValue::Undefined());
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         InternalCallParams *arguments = thread->GetInternalCallParams();
87         arguments->MakeArgv(nextValue);
88         if (nextValue->IsArray(thread)) {
89             auto prop = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
90             arguments->MakeArgv(prop);
91         }
92         JSTaggedValue ret = JSFunction::Call(thread, adder, JSHandle<JSTaggedValue>(weakSet), 1, arguments->GetArgv());
93 
94         // Let status be Call(adder, weakset, «nextValue.[[value]]»).
95         status.Update(ret);
96         // If status is an abrupt completion, return IteratorClose(iter, status).
97         if (thread->HasPendingException()) {
98             return JSIterator::IteratorCloseAndReturn(thread, iter, status);
99         }
100         // Let next be IteratorStep(iter).
101         next = JSIterator::IteratorStep(thread, iter);
102     }
103     return weakSet.GetTaggedValue();
104 }
105 
Add(EcmaRuntimeCallInfo * argv)106 JSTaggedValue BuiltinsWeakSet::Add(EcmaRuntimeCallInfo *argv)
107 {
108     ASSERT(argv);
109     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Add);
110     JSThread *thread = argv->GetThread();
111     [[maybe_unused]] EcmaHandleScope handleScope(thread);
112     JSHandle<JSTaggedValue> self = GetThis(argv);
113 
114     // 2.If Type(S) is not Object, throw a TypeError exception.
115     // 3.If S does not have a [[WeakSetData]] internal slot, throw a TypeError exception.
116     if (!self->IsJSWeakSet()) {
117         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
118     }
119 
120     JSHandle<JSTaggedValue> value(GetCallArg(argv, 0));
121     if (!value->IsHeapObject()) {
122         THROW_TYPE_ERROR_AND_RETURN(thread, "value is not an object", JSTaggedValue::Exception());
123     }
124     if (value->IsSymbol() || value->IsString()) {
125         THROW_TYPE_ERROR_AND_RETURN(thread, "value is Symblol or String", JSTaggedValue::Exception());
126     }
127 
128     JSHandle<JSWeakSet> weakSet(thread, JSWeakSet::Cast(*JSTaggedValue::ToObject(thread, self)));
129 
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     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
149     if (!value->IsHeapObject()) {
150         GetTaggedBoolean(false);
151     }
152     return GetTaggedBoolean(JSWeakSet::Delete(thread, weakSet, value));
153 }
154 
Has(EcmaRuntimeCallInfo * argv)155 JSTaggedValue BuiltinsWeakSet::Has(EcmaRuntimeCallInfo *argv)
156 {
157     ASSERT(argv);
158     BUILTINS_API_TRACE(argv->GetThread(), WeakSet, Has);
159     JSThread *thread = argv->GetThread();
160     [[maybe_unused]] EcmaHandleScope handleScope(thread);
161     JSHandle<JSTaggedValue> self = GetThis(argv);
162     // 2.If Type(S) is not Object, throw a TypeError exception.
163     // 3.If S does not have a [[SetData]] internal slot, throw a TypeError exception.
164     if (!self->IsJSWeakSet()) {
165         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakSet", JSTaggedValue::Exception());
166     }
167     JSWeakSet *jsWeakSet = JSWeakSet::Cast(*JSTaggedValue::ToObject(thread, self));
168     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
169     if (!value->IsHeapObject()) {
170         GetTaggedBoolean(false);
171     }
172     return GetTaggedBoolean(jsWeakSet->Has(value.GetTaggedValue()));
173 }
174 }  // namespace panda::ecmascript::builtins
175