1 /*
2 * Copyright (c) 2022 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_finalization_registry.h"
17
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/js_finalization_registry.h"
20 #include "ecmascript/linked_hash_table.h"
21 #include "ecmascript/object_factory.h"
22
23 namespace panda::ecmascript::builtins {
24 // 26.2.1.1
FinalizationRegistryConstructor(EcmaRuntimeCallInfo * argv)25 JSTaggedValue BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(EcmaRuntimeCallInfo *argv)
26 {
27 ASSERT(argv);
28 JSThread *thread = argv->GetThread();
29 BUILTINS_API_TRACE(thread, FinalizationRegistry, Constructor);
30 [[maybe_unused]] EcmaHandleScope handleScope(thread);
31 JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 // 1. If NewTarget is undefined, throw a TypeError exception.
34 if (newTarget->IsUndefined()) {
35 THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
36 }
37 // 2. If IsCallable(cleanupCallback) is false, throw a TypeError exception.
38 JSHandle<JSTaggedValue> cleanupCallback = GetCallArg(argv, 0);
39 if (!cleanupCallback->IsCallable()) {
40 THROW_TYPE_ERROR_AND_RETURN(thread, "cleanupCallback not Callable", JSTaggedValue::Exception());
41 }
42 // 3. Let finalizationRegistry be ? OrdinaryCreateFromConstructor(NewTarget, "%FinalizationRegistry.prototype%",
43 // « [[Realm]], [[CleanupCallback]], [[Cells]] »).
44 JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
45 JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
46 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
47 JSHandle<JSFinalizationRegistry> finalization = JSHandle<JSFinalizationRegistry>::Cast(obj);
48 // 4. Let fn be the active function object.
49 // 5. Set finalizationRegistry.[[Realm]] to fn.[[Realm]].
50 // 6. Set finalizationRegistry.[[CleanupCallback]] to cleanupCallback.
51 finalization->SetCleanupCallback(thread, cleanupCallback);
52 // 7. Set finalizationRegistry.[[Cells]] to a new empty List.
53 JSHandle<CellRecordVector> noUnregister(CellRecordVector::Create(thread));
54 JSHandle<LinkedHashMap> maybeUnregister = LinkedHashMap::Create(thread);
55 finalization->SetNoUnregister(thread, noUnregister);
56 finalization->SetMaybeUnregister(thread, maybeUnregister);
57 JSHandle<JSTaggedValue> objValue(finalization);
58 // 8. Return finalizationRegistry.
59 return finalization.GetTaggedValue();
60 }
61 // 26.2.3.2
Register(EcmaRuntimeCallInfo * argv)62 JSTaggedValue BuiltinsFinalizationRegistry::Register(EcmaRuntimeCallInfo *argv)
63 {
64 ASSERT(argv);
65 JSThread *thread = argv->GetThread();
66 BUILTINS_API_TRACE(thread, FinalizationRegistry, Register);
67 [[maybe_unused]] EcmaHandleScope handleScope(thread);
68 JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
69 JSHandle<JSTaggedValue> heldValue = GetCallArg(argv, 1);
70 JSHandle<JSTaggedValue> unregisterToken = GetCallArg(argv, 2); // 2 : unregisterToken storage location
71 // 1. Let finalizationRegistry be the this value.
72 JSHandle<JSTaggedValue> finalizationRegistry = GetThis(argv);
73 // 2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]).
74 if (!finalizationRegistry->IsJSFinalizationRegistry()) {
75 THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot",
76 JSTaggedValue::Exception());
77 }
78 // 3. If CanBeHeldWeakly(target) is false, throw a TypeError exception.
79 if (!JSTaggedValue::CanBeHeldWeakly(thread, target)) {
80 THROW_TYPE_ERROR_AND_RETURN(thread, "target invalid", JSTaggedValue::Exception());
81 }
82 // 4. If SameValue(target, heldValue) is true, throw a TypeError exception.
83 if (JSTaggedValue::SameValue(target, heldValue)) {
84 THROW_TYPE_ERROR_AND_RETURN(thread, "target and heldValue should not be equal", JSTaggedValue::Exception());
85 }
86 // 5. If CanBeHeldWeakly(unregisterToken) is false, then
87 // a. If unregisterToken is not undefined, throw a TypeError exception.
88 // b. Set unregisterToken to empty.
89 if (!JSTaggedValue::CanBeHeldWeakly(thread, unregisterToken) && !unregisterToken->IsUndefined()) {
90 THROW_TYPE_ERROR_AND_RETURN(thread, "unregisterToken invalid", JSTaggedValue::Exception());
91 }
92 // 6. Let cell be the Record { [[WeakRefTarget]]: target,
93 // [[HeldValue]]: heldValue, [[UnregisterToken]]: unregisterToken }.
94 // 7. Append cell to finalizationRegistry.[[Cells]].
95 JSHandle<JSFinalizationRegistry> finRegHandle(finalizationRegistry);
96 JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finRegHandle);
97 // 8. Return undefined.
98 return JSTaggedValue::Undefined();
99 }
100 // 26.2.3.3
Unregister(EcmaRuntimeCallInfo * argv)101 JSTaggedValue BuiltinsFinalizationRegistry::Unregister(EcmaRuntimeCallInfo *argv)
102 {
103 ASSERT(argv);
104 JSThread *thread = argv->GetThread();
105 BUILTINS_API_TRACE(thread, FinalizationRegistry, Unregister);
106 [[maybe_unused]] EcmaHandleScope handleScope(thread);
107 JSHandle<JSTaggedValue> unregisterToken = GetCallArg(argv, 0);
108 // 1. Let finalizationRegistry be the this value.
109 JSHandle<JSTaggedValue> finalizationRegistry = GetThis(argv);
110 // 2. Perform ? RequireInternalSlot(finalizationRegistry, [[Cells]]).
111 if (!finalizationRegistry->IsJSFinalizationRegistry()) {
112 THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot",
113 JSTaggedValue::Exception());
114 }
115 // 3. If CanBeHeldWeakly(unregisterToken) is false, throw a TypeError exception.
116 if (!JSTaggedValue::CanBeHeldWeakly(thread, unregisterToken)) {
117 THROW_TYPE_ERROR_AND_RETURN(thread, "unregisterToken invalid", JSTaggedValue::Exception());
118 }
119 // 4. Let removed be false.
120 // 5. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell of
121 // finalizationRegistry.[[Cells]], do
122 // a. If cell.[[UnregisterToken]] is not empty and SameValue(cell.[[UnregisterToken]], unregisterToken)
123 // is true, then
124 // i. Remove cell from finalizationRegistry.[[Cells]].
125 // ii. Set removed to true.
126 JSHandle<JSFinalizationRegistry> finRegHandle(finalizationRegistry);
127 bool removed = JSFinalizationRegistry::Unregister(thread, unregisterToken, finRegHandle);
128 // 6. Return removed.
129 return JSTaggedValue(removed);
130 }
131 } // namespace panda::ecmascript::builtins