* Copyright (c) 2022-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecmascript/builtins/builtins_finalization_registry.h"
#include "ecmascript/js_finalization_registry.h"
#include "ecmascript/js_tagged_value-inl.h"
#include "ecmascript/linked_hash_table.h"
namespace panda::ecmascript::builtins {
JSTaggedValue BuiltinsFinalizationRegistry::FinalizationRegistryConstructor(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, FinalizationRegistry, Constructor);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
if (newTarget->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "new target can't be undefined", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> cleanupCallback = GetCallArg(argv, 0);
if (!cleanupCallback->IsCallable()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "cleanupCallback not Callable", JSTaggedValue::Exception());
}
JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
JSHandle<JSFinalizationRegistry> finalization = JSHandle<JSFinalizationRegistry>::Cast(obj);
finalization->SetCleanupCallback(thread, cleanupCallback);
JSHandle<CellRecordVector> noUnregister(CellRecordVector::Create(thread));
JSHandle<LinkedHashMap> maybeUnregister = LinkedHashMap::Create(thread);
finalization->SetNoUnregister(thread, noUnregister);
finalization->SetMaybeUnregister(thread, maybeUnregister);
JSHandle<JSTaggedValue> objValue(finalization);
return finalization.GetTaggedValue();
}
JSTaggedValue BuiltinsFinalizationRegistry::Register(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, FinalizationRegistry, Register);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> target = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> heldValue = GetCallArg(argv, 1);
JSHandle<JSTaggedValue> unregisterToken = GetCallArg(argv, 2);
JSHandle<JSTaggedValue> finalizationRegistry = GetThis(argv);
if (!finalizationRegistry->IsJSFinalizationRegistry()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot",
JSTaggedValue::Exception());
}
if (!JSTaggedValue::CanBeHeldWeakly(thread, target)) {
THROW_TYPE_ERROR_AND_RETURN(thread, "target invalid", JSTaggedValue::Exception());
}
if (JSTaggedValue::SameValue(thread, target, heldValue)) {
THROW_TYPE_ERROR_AND_RETURN(thread, "target and heldValue should not be equal", JSTaggedValue::Exception());
}
if (!JSTaggedValue::CanBeHeldWeakly(thread, unregisterToken) && !unregisterToken->IsUndefined()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "unregisterToken invalid", JSTaggedValue::Exception());
}
JSHandle<JSFinalizationRegistry> finRegHandle(finalizationRegistry);
JSFinalizationRegistry::Register(thread, target, heldValue, unregisterToken, finRegHandle);
return JSTaggedValue::Undefined();
}
JSTaggedValue BuiltinsFinalizationRegistry::Unregister(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, FinalizationRegistry, Unregister);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> unregisterToken = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> finalizationRegistry = GetThis(argv);
if (!finalizationRegistry->IsJSFinalizationRegistry()) {
THROW_TYPE_ERROR_AND_RETURN(thread, "thisValue is not object or does not have an internalSlot internal slot",
JSTaggedValue::Exception());
}
if (!JSTaggedValue::CanBeHeldWeakly(thread, unregisterToken)) {
THROW_TYPE_ERROR_AND_RETURN(thread, "unregisterToken invalid", JSTaggedValue::Exception());
}
JSHandle<JSFinalizationRegistry> finRegHandle(finalizationRegistry);
bool removed = JSFinalizationRegistry::Unregister(thread, unregisterToken, finRegHandle);
return JSTaggedValue(removed);
}
}