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 Type(key) is not Object, return false.
91 if (!key->IsHeapObject()) {
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 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
111 // 5.if Type(key) is not Object, return false.
112 if (!key->IsHeapObject()) {
113 return GetTaggedBoolean(false);
114 }
115 return GetTaggedBoolean(jsWeakMap->Has(key.GetTaggedValue()));
116 }
117
Get(EcmaRuntimeCallInfo * argv)118 JSTaggedValue BuiltinsWeakMap::Get(EcmaRuntimeCallInfo *argv)
119 {
120 ASSERT(argv);
121 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Get);
122 JSThread *thread = argv->GetThread();
123 [[maybe_unused]] EcmaHandleScope handleScope(thread);
124 JSHandle<JSTaggedValue> self(GetThis(argv));
125 // 2.If Type(S) is not Object, throw a TypeError exception.
126 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
127 if (!self->IsJSWeakMap()) {
128 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
129 }
130 JSWeakMap *jsWeakMap = JSWeakMap::Cast(*JSTaggedValue::ToObject(thread, self));
131 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
132 if (!key->IsHeapObject()) {
133 return JSTaggedValue::Undefined();
134 }
135 return jsWeakMap->Get(key.GetTaggedValue());
136 }
137
Set(EcmaRuntimeCallInfo * argv)138 JSTaggedValue BuiltinsWeakMap::Set(EcmaRuntimeCallInfo *argv)
139 {
140 ASSERT(argv);
141 BUILTINS_API_TRACE(argv->GetThread(), WeakMap, Set);
142 JSThread *thread = argv->GetThread();
143 [[maybe_unused]] EcmaHandleScope handleScope(thread);
144 JSHandle<JSTaggedValue> self = GetThis(argv);
145
146 // 2.If Type(S) is not Object, throw a TypeError exception.
147 // 3.If S does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
148 if (!self->IsJSWeakMap()) {
149 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not JSWeakMap.", JSTaggedValue::Exception());
150 }
151
152 JSHandle<JSTaggedValue> key = GetCallArg(argv, 0);
153 if (!key->IsHeapObject()) {
154 THROW_TYPE_ERROR_AND_RETURN(thread, "obj is not an object.", JSTaggedValue::Exception());
155 }
156 if (key->IsSymbol() || key->IsString()) {
157 THROW_TYPE_ERROR_AND_RETURN(thread, "key is Symblol or String", JSTaggedValue::Exception());
158 }
159
160 JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
161
162 JSHandle<JSWeakMap> map(self);
163 JSWeakMap::Set(thread, map, key, value);
164 return map.GetTaggedValue();
165 }
166 } // namespace panda::ecmascript::builtins
167