• 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_map.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_map_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 {
WeakMapConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue BuiltinsWeakMap::WeakMapConstructor(EcmaRuntimeCallInfo *argv)
28 {
29     ASSERT(argv);
30     BUILTINS_API_TRACE(argv->GetThread(), WeakMap, 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 WeakMap be OrdinaryCreateFromConstructor(NewTarget, "%WeakMapPrototype%", «‍[[WeakMapData]]» ).
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<JSWeakMap> weakMap = JSHandle<JSWeakMap>::Cast(obj);
46 
47     // 4.Set weakmap’s [[WeakMapData]] internal slot to a new empty List.
48     JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
49     weakMap->SetLinkedMap(thread, linkedMap);
50     // add data into set from iterable
51     // 5.If iterable is not present, let iterable be undefined.
52     // 6.If iterable is either undefined or null, let iter be undefined.
53     JSHandle<JSTaggedValue> iterable = GetCallArg(argv, 0);
54     // 8.If iter is undefined, return set
55     if (iterable->IsUndefined() || iterable->IsNull()) {
56         return weakMap.GetTaggedValue();
57     }
58     if (!iterable->IsECMAObject()) {
59         THROW_TYPE_ERROR_AND_RETURN(thread, "iterable is not object", JSTaggedValue::Exception());
60     }
61     // Let adder be Get(weakMap, "set").
62     JSHandle<JSTaggedValue> adderKey(factory->NewFromCanBeCompressString("set"));
63     JSHandle<JSTaggedValue> adder =
64         JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(weakMap), adderKey).GetValue();
65     // ReturnIfAbrupt(adder).
66     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
67     // If IsCallable(adder) is false, throw a TypeError exception
68     if (!adder->IsCallable()) {
69         THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
70     }
71     // Let iter be GetIterator(iterable).
72     JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, iterable));
73     // ReturnIfAbrupt(iter).
74     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter.GetTaggedValue());
75     JSHandle<JSTaggedValue> keyIndex(thread, JSTaggedValue(0));
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     while (!next->IsFalse()) {
81         // ReturnIfAbrupt(next).
82         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
83         // Let nextValue be IteratorValue(next).
84         JSHandle<JSTaggedValue> nextValue(JSIterator::IteratorValue(thread, next));
85         // ReturnIfAbrupt(nextValue).
86         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, next.GetTaggedValue());
87         // If Type(nextItem) is not Object
88         if (!nextValue->IsECMAObject()) {
89             JSHandle<JSObject> typeError = factory->GetJSError(ErrorType::TYPE_ERROR, "nextItem is not Object");
90             JSHandle<JSTaggedValue> record(
91                 factory->NewCompletionRecord(CompletionRecordType::THROW, JSHandle<JSTaggedValue>(typeError)));
92             JSTaggedValue ret = JSIterator::IteratorClose(thread, iter, record).GetTaggedValue();
93             if (!thread->HasPendingException()) {
94                 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, typeError.GetTaggedValue(), ret);
95             };
96             return ret;
97         }
98         // Let k be Get(nextItem, "0").
99         JSHandle<JSTaggedValue> key = JSObject::GetProperty(thread, nextValue, keyIndex).GetValue();
100         // If k is an abrupt completion, return IteratorClose(iter, k).
101         if (thread->HasPendingException()) {
102             return JSIterator::IteratorCloseAndReturn(thread, iter, key);
103         }
104 
105         // Let v be Get(nextItem, "1").
106         JSHandle<JSTaggedValue> value = JSObject::GetProperty(thread, nextValue, valueIndex).GetValue();
107         // If v is an abrupt completion, return IteratorClose(iter, v).
108         if (thread->HasPendingException()) {
109             return JSIterator::IteratorCloseAndReturn(thread, iter, value);
110         }
111 
112         // Let status be Call(adder, weakMap, «nextValue.[[value]]»).
113         InternalCallParams *arguments = thread->GetInternalCallParams();
114         arguments->MakeArgv(key, value);
115         JSTaggedValue ret = JSFunction::Call(thread,
116             adder, JSHandle<JSTaggedValue>(weakMap), 2, arguments->GetArgv());  // 2: key and value pair
117 
118         status.Update(ret);
119         // If status is an abrupt completion, return IteratorClose(iter, status).
120         if (thread->HasPendingException()) {
121             return JSIterator::IteratorCloseAndReturn(thread, iter, status);
122         }
123         // Let next be IteratorStep(iter).
124         next = JSIterator::IteratorStep(thread, iter);
125     }
126     return weakMap.GetTaggedValue();
127 }
128 
Delete(EcmaRuntimeCallInfo * argv)129 JSTaggedValue BuiltinsWeakMap::Delete(EcmaRuntimeCallInfo *argv)
130 {
131     ASSERT(argv);
132     BUILTINS_API_TRACE(argv->GetThread(), WeakMap, 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 [[WeakMapData]] internal slot, throw a TypeError exception.
138     if (!self->IsJSWeakMap()) {
139         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
140     }
141 
142     JSHandle<JSWeakMap> weakMap(self);
143     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
144     // 5.if Type(key) is not Object, return false.
145     if (!key->IsHeapObject()) {
146         return GetTaggedBoolean(false);
147     }
148     return GetTaggedBoolean(JSWeakMap::Delete(thread, weakMap, key));
149 }
150 
Has(EcmaRuntimeCallInfo * argv)151 JSTaggedValue BuiltinsWeakMap::Has(EcmaRuntimeCallInfo *argv)
152 {
153     ASSERT(argv);
154     BUILTINS_API_TRACE(argv->GetThread(), WeakMap, 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 [[WeakMapData]] internal slot, throw a TypeError exception.
160     if (!self->IsJSWeakMap()) {
161         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
162     }
163     JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
164     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
165     // 5.if Type(key) is not Object, return false.
166     if (!key->IsHeapObject()) {
167         return GetTaggedBoolean(false);
168     }
169     return GetTaggedBoolean(jsWeakMap->Has(key.GetTaggedValue()));
170 }
171 
Get(EcmaRuntimeCallInfo * argv)172 JSTaggedValue BuiltinsWeakMap::Get(EcmaRuntimeCallInfo *argv)
173 {
174     ASSERT(argv);
175     BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Get);
176     JSThread *thread = argv->GetThread();
177     [[maybe_unused]] EcmaHandleScope handleScope(thread);
178     JSHandle<JSTaggedValue> self(GetThis(argv));
179     // 2.If Type(S) is not Object, throw a TypeError exception.
180     // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
181     if (!self->IsJSWeakMap()) {
182         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
183     }
184     JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
185     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
186     if (!key->IsHeapObject()) {
187         return JSTaggedValue::Undefined();
188     }
189     return jsWeakMap->Get(key.GetTaggedValue());
190 }
191 
Set(EcmaRuntimeCallInfo * argv)192 JSTaggedValue BuiltinsWeakMap::Set(EcmaRuntimeCallInfo *argv)
193 {
194     ASSERT(argv);
195     BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Set);
196     JSThread *thread = argv->GetThread();
197     [[maybe_unused]] EcmaHandleScope handleScope(thread);
198     JSHandle<JSTaggedValue> self = GetThis(argv);
199 
200     // 2.If Type(S) is not Object, throw a TypeError exception.
201     // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
202     if (!self->IsJSWeakMap()) {
203         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
204     }
205 
206     JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
207     if (!key->IsHeapObject()) {
208         THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not an object.", JSTaggedValue::Exception());
209     }
210     if (key->IsSymbol() || key->IsString()) {
211         THROW_TYPE_ERROR_AND_RETURN(thread, "key is Symblol or String", JSTaggedValue::Exception());
212     }
213 
214     JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
215 
216     JSHandle<JSWeakMap> map(self);
217     JSWeakMap::Set(thread, map, key, value);
218     return map.GetTaggedValue();
219 }
220 }  // namespace panda::ecmascript::builtins
221