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
18 #include "ecmascript/builtins/builtins_map.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/interpreter/interpreter.h"
22 #include "ecmascript/js_map_iterator.h"
23 #include "ecmascript/js_weak_container.h"
24 #include "ecmascript/linked_hash_table.h"
25 #include "ecmascript/object_factory.h"
26
27 namespace panda::ecmascript::builtins {
WeakMapConstructor(EcmaRuntimeCallInfo * argv)28 JSTaggedValue BuiltinsWeakMap::WeakMapConstructor(EcmaRuntimeCallInfo *argv)
29 {
30 ASSERT(argv);
31 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, 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 WeakMap be OrdinaryCreateFromConstructor(NewTarget, "%WeakMapPrototype%", «[[WeakMapData]]» ).
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<JSWeakMap> weakMap = JSHandle<JSWeakMap>::Cast(obj);
47
48 // 4.Set weakmap’s [[WeakMapData]] internal slot to a new empty List.
49 JSHandle<LinkedHashMap> linkedMap = LinkedHashMap::Create(thread);
50 weakMap->SetLinkedMap(thread, linkedMap);
51 // add data into set 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 set
56 if (iterable->IsUndefined() || iterable->IsNull()) {
57 return weakMap.GetTaggedValue();
58 }
59 if (!iterable->IsECMAObject()) {
60 THROW_TYPE_ERROR_AND_RETURN(thread, "iterable is not object", JSTaggedValue::Exception());
61 }
62 // Let adder be Get(weakMap, "set").
63 JSHandle<JSTaggedValue> adderKey = thread->GlobalConstants()->GetHandledSetString();
64 JSHandle<JSTaggedValue> adder =
65 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(weakMap), adderKey).GetValue();
66 // ReturnIfAbrupt(adder).
67 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, adder.GetTaggedValue());
68 // If IsCallable(adder) is false, throw a TypeError exception
69 if (!adder->IsCallable()) {
70 THROW_TYPE_ERROR_AND_RETURN(thread, "adder is not callable", adder.GetTaggedValue());
71 }
72 return BuiltinsMap::AddEntriesFromIterable(thread, obj, iterable, adder, factory);
73 }
74
Delete(EcmaRuntimeCallInfo * argv)75 JSTaggedValue BuiltinsWeakMap::Delete(EcmaRuntimeCallInfo *argv)
76 {
77 ASSERT(argv);
78 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Delete);
79 JSThread *thread = argv->GetThread();
80 [[maybe_unused]] EcmaHandleScope handleScope(thread);
81 JSHandle<JSTaggedValue> self = GetThis(argv);
82 // 2.If Type(S) is not Object, throw a TypeError exception.
83 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
84 if (!self->IsJSWeakMap()) {
85 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
86 }
87
88 JSHandle<JSWeakMap> weakMap(self);
89 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
90 // 5.If CanBeHeldWeakly(key) is false, return false.
91 if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
92 return GetTaggedBoolean(false);
93 }
94 return GetTaggedBoolean(JSWeakMap::Delete(thread, weakMap, key));
95 }
96
Has(EcmaRuntimeCallInfo * argv)97 JSTaggedValue BuiltinsWeakMap::Has(EcmaRuntimeCallInfo *argv)
98 {
99 ASSERT(argv);
100 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Has);
101 JSThread *thread = argv->GetThread();
102 [[maybe_unused]] EcmaHandleScope handleScope(thread);
103 JSHandle<JSTaggedValue> self(GetThis(argv));
104 // 2.If Type(S) is not Object, throw a TypeError exception.
105 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
106 if (!self->IsJSWeakMap()) {
107 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
108 }
109 JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
110 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
111 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
112 // 5.If CanBeHeldWeakly(key) is false, return false.
113 if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
114 return GetTaggedBoolean(false);
115 }
116 return GetTaggedBoolean(jsWeakMap->Has(key.GetTaggedValue()));
117 }
118
Get(EcmaRuntimeCallInfo * argv)119 JSTaggedValue BuiltinsWeakMap::Get(EcmaRuntimeCallInfo *argv)
120 {
121 ASSERT(argv);
122 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Get);
123 JSThread *thread = argv->GetThread();
124 [[maybe_unused]] EcmaHandleScope handleScope(thread);
125 JSHandle<JSTaggedValue> self(GetThis(argv));
126 // 2.If Type(S) is not Object, throw a TypeError exception.
127 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
128 if (!self->IsJSWeakMap()) {
129 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
130 }
131 JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
132 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
133 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
134 // 4.If CanBeHeldWeakly(key) is false, return undefined.
135 if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
136 return JSTaggedValue::Undefined();
137 }
138 return jsWeakMap->Get(key.GetTaggedValue());
139 }
140
Set(EcmaRuntimeCallInfo * argv)141 JSTaggedValue BuiltinsWeakMap::Set(EcmaRuntimeCallInfo *argv)
142 {
143 ASSERT(argv);
144 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Set);
145 JSThread *thread = argv->GetThread();
146 [[maybe_unused]] EcmaHandleScope handleScope(thread);
147 JSHandle<JSTaggedValue> self = GetThis(argv);
148
149 // 2.If Type(S) is not Object, throw a TypeError exception.
150 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
151 if (!self->IsJSWeakMap()) {
152 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
153 }
154
155 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
156 // 4.If CanBeHeldWeakly(key) is false, throw a TypeError exception.
157 if (!JSTaggedValue::CanBeHeldWeakly(thread, key)) {
158 THROW_TYPE_ERROR_AND_RETURN(thread, "invalid value used as weak map key.", JSTaggedValue::Exception());
159 }
160
161 JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
162
163 JSHandle<JSWeakMap> map(self);
164 JSWeakMap::Set(thread, map, key, value);
165 return map.GetTaggedValue();
166 }
167 } // namespace panda::ecmascript::builtins
168