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