14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/js_finalization_registry.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/ecma_context.h"
194514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
204514f5e3Sopenharmony_ci#include "ecmascript/jobs/micro_job_queue.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cinamespace panda::ecmascript {
254514f5e3Sopenharmony_ci// -------------------------------CellRecordVector-----------------------------------
264514f5e3Sopenharmony_ciJSHandle<CellRecordVector> CellRecordVector::Append(const JSThread *thread, const JSHandle<CellRecordVector> &vector,
274514f5e3Sopenharmony_ci                                                    const JSHandle<JSTaggedValue> &value)
284514f5e3Sopenharmony_ci{
294514f5e3Sopenharmony_ci    JSHandle<WeakVector> oldVector(vector);
304514f5e3Sopenharmony_ci    JSHandle<WeakVector> newVector = WeakVector::FillOrAppend(thread, oldVector, value);
314514f5e3Sopenharmony_ci    return JSHandle<CellRecordVector>(newVector);
324514f5e3Sopenharmony_ci}
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_cibool CellRecordVector::IsEmpty()
354514f5e3Sopenharmony_ci{
364514f5e3Sopenharmony_ci    if (Empty()) {
374514f5e3Sopenharmony_ci        return true;
384514f5e3Sopenharmony_ci    }
394514f5e3Sopenharmony_ci    for (uint32_t i = 0; i < GetEnd(); i++) {
404514f5e3Sopenharmony_ci        JSTaggedValue value = Get(i);
414514f5e3Sopenharmony_ci        if (!value.IsHole()) {
424514f5e3Sopenharmony_ci            return false;
434514f5e3Sopenharmony_ci        }
444514f5e3Sopenharmony_ci    }
454514f5e3Sopenharmony_ci    return true;
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// ---------------------------JSFinalizationRegistry-----------------------------------
494514f5e3Sopenharmony_civoid JSFinalizationRegistry::Register(JSThread *thread, JSHandle<JSTaggedValue> target,
504514f5e3Sopenharmony_ci                                      JSHandle<JSTaggedValue> heldValue,
514514f5e3Sopenharmony_ci                                      JSHandle<JSTaggedValue> unregisterToken, JSHandle<JSFinalizationRegistry> obj)
524514f5e3Sopenharmony_ci{
534514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
544514f5e3Sopenharmony_ci    JSHandle<CellRecord> cellRecord = factory->NewCellRecord();
554514f5e3Sopenharmony_ci    cellRecord->SetToWeakRefTarget(thread, target.GetTaggedValue());
564514f5e3Sopenharmony_ci    cellRecord->SetHeldValue(thread, heldValue);
574514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> cell(cellRecord);
584514f5e3Sopenharmony_ci    // If unregisterToken is undefined, we use vector to store
594514f5e3Sopenharmony_ci    // otherwise we use hash map to store to facilitate subsequent delete operations
604514f5e3Sopenharmony_ci    if (!unregisterToken->IsUndefined()) {
614514f5e3Sopenharmony_ci        JSHandle<LinkedHashMap> maybeUnregister(thread, obj->GetMaybeUnregister());
624514f5e3Sopenharmony_ci        JSHandle<CellRecordVector> array(thread, JSTaggedValue::Undefined());
634514f5e3Sopenharmony_ci        if (maybeUnregister->Has(thread, unregisterToken.GetTaggedValue())) {
644514f5e3Sopenharmony_ci            array = JSHandle<CellRecordVector>(thread, maybeUnregister->Get(thread, unregisterToken.GetTaggedValue()));
654514f5e3Sopenharmony_ci        } else {
664514f5e3Sopenharmony_ci            array = JSHandle<CellRecordVector>(CellRecordVector::Create(thread));
674514f5e3Sopenharmony_ci        }
684514f5e3Sopenharmony_ci        array = CellRecordVector::Append(thread, array, cell);
694514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> arrayValue(array);
704514f5e3Sopenharmony_ci        maybeUnregister = LinkedHashMap::SetWeakRef(thread, maybeUnregister, unregisterToken, arrayValue);
714514f5e3Sopenharmony_ci        obj->SetMaybeUnregister(thread, maybeUnregister);
724514f5e3Sopenharmony_ci    } else {
734514f5e3Sopenharmony_ci        JSHandle<CellRecordVector> noUnregister(thread, obj->GetNoUnregister());
744514f5e3Sopenharmony_ci        noUnregister = CellRecordVector::Append(thread, noUnregister, cell);
754514f5e3Sopenharmony_ci        obj->SetNoUnregister(thread, noUnregister);
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci    JSFinalizationRegistry::AddFinRegLists(thread, obj);
784514f5e3Sopenharmony_ci}
794514f5e3Sopenharmony_ci
804514f5e3Sopenharmony_cibool JSFinalizationRegistry::Unregister(JSThread *thread, JSHandle<JSTaggedValue> UnregisterToken,
814514f5e3Sopenharmony_ci                                        JSHandle<JSFinalizationRegistry> obj)
824514f5e3Sopenharmony_ci{
834514f5e3Sopenharmony_ci    // Because we have stored the object that may be unregistered in the hash map when registering,
844514f5e3Sopenharmony_ci    // at this time we just need to find it in the map and delete it
854514f5e3Sopenharmony_ci    JSHandle<LinkedHashMap> maybeUnregister(thread, obj->GetMaybeUnregister());
864514f5e3Sopenharmony_ci    int entry = maybeUnregister->FindElement(thread, UnregisterToken.GetTaggedValue());
874514f5e3Sopenharmony_ci    if (entry == -1) {
884514f5e3Sopenharmony_ci        return false;
894514f5e3Sopenharmony_ci    }
904514f5e3Sopenharmony_ci    maybeUnregister->RemoveEntry(thread, entry);
914514f5e3Sopenharmony_ci    JSHandle<LinkedHashMap> newMaybeUnregister = LinkedHashMap::Shrink(thread, maybeUnregister);
924514f5e3Sopenharmony_ci    obj->SetMaybeUnregister(thread, newMaybeUnregister);
934514f5e3Sopenharmony_ci    return true;
944514f5e3Sopenharmony_ci}
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_civoid JSFinalizationRegistry::CleanFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> obj)
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
994514f5e3Sopenharmony_ci    if (obj->GetPrev().IsNull() && obj->GetNext().IsNull()) {
1004514f5e3Sopenharmony_ci        env->SetFinRegLists(thread, JSTaggedValue::Undefined());
1014514f5e3Sopenharmony_ci        return;
1024514f5e3Sopenharmony_ci    }
1034514f5e3Sopenharmony_ci    if (!obj->GetPrev().IsNull()) {
1044514f5e3Sopenharmony_ci        JSHandle<JSFinalizationRegistry> prev(thread, obj->GetPrev());
1054514f5e3Sopenharmony_ci        prev->SetNext(thread, obj->GetNext());
1064514f5e3Sopenharmony_ci    }
1074514f5e3Sopenharmony_ci    if (!obj->GetNext().IsNull()) {
1084514f5e3Sopenharmony_ci        JSHandle<JSFinalizationRegistry> next(thread, obj->GetNext());
1094514f5e3Sopenharmony_ci        next->SetPrev(thread, obj->GetPrev());
1104514f5e3Sopenharmony_ci    } else {
1114514f5e3Sopenharmony_ci        env->SetFinRegLists(thread, obj->GetPrev());
1124514f5e3Sopenharmony_ci    }
1134514f5e3Sopenharmony_ci    obj->SetPrev(thread, JSTaggedValue::Null());
1144514f5e3Sopenharmony_ci    obj->SetNext(thread, JSTaggedValue::Null());
1154514f5e3Sopenharmony_ci}
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_civoid JSFinalizationRegistry::CheckAndCall(JSThread *thread)
1184514f5e3Sopenharmony_ci{
1194514f5e3Sopenharmony_ci    if (thread->GetCheckAndCallEnterState()) {
1204514f5e3Sopenharmony_ci        return;
1214514f5e3Sopenharmony_ci    }
1224514f5e3Sopenharmony_ci    CheckAndCallScope scope(thread);
1234514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1244514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> prev = env->GetFinRegLists();
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci    if (prev->IsUndefined()) {
1274514f5e3Sopenharmony_ci        return;
1284514f5e3Sopenharmony_ci    }
1294514f5e3Sopenharmony_ci    JSMutableHandle<JSTaggedValue> current(thread, prev.GetTaggedValue());
1304514f5e3Sopenharmony_ci    JSMutableHandle<JSFinalizationRegistry> jsFinalizationRegistry(thread, current.GetTaggedValue());
1314514f5e3Sopenharmony_ci    while (!current->IsNull()) {
1324514f5e3Sopenharmony_ci        jsFinalizationRegistry.Update(current.GetTaggedValue());
1334514f5e3Sopenharmony_ci        current.Update(jsFinalizationRegistry->GetPrev());
1344514f5e3Sopenharmony_ci        if (CleanupFinalizationRegistry(thread, jsFinalizationRegistry)) {
1354514f5e3Sopenharmony_ci            // If the objects registered on the current JSFinalizationRegistry object have all been gc,
1364514f5e3Sopenharmony_ci            // then we clean up this JSFinalizationRegistry object from the FinRegLists
1374514f5e3Sopenharmony_ci            CleanFinRegLists(thread, jsFinalizationRegistry);
1384514f5e3Sopenharmony_ci        }
1394514f5e3Sopenharmony_ci    }
1404514f5e3Sopenharmony_ci}
1414514f5e3Sopenharmony_ci
1424514f5e3Sopenharmony_civoid DealCallBackOfMap(JSThread *thread, JSHandle<CellRecordVector> &cellVect,
1434514f5e3Sopenharmony_ci    JSHandle<job::MicroJobQueue> &job, JSHandle<JSFunction> &func)
1444514f5e3Sopenharmony_ci{
1454514f5e3Sopenharmony_ci    if (!cellVect->Empty()) {
1464514f5e3Sopenharmony_ci        uint32_t cellVectLen = cellVect->GetEnd();
1474514f5e3Sopenharmony_ci        for (uint32_t i = 0; i < cellVectLen; ++i) {
1484514f5e3Sopenharmony_ci            JSTaggedValue value = cellVect->Get(i);
1494514f5e3Sopenharmony_ci            if (value.IsHole()) {
1504514f5e3Sopenharmony_ci                continue;
1514514f5e3Sopenharmony_ci            }
1524514f5e3Sopenharmony_ci            JSHandle<CellRecord> cellRecord(thread, value);
1534514f5e3Sopenharmony_ci            // if WeakRefTarget have been gc, set callback to job and delete
1544514f5e3Sopenharmony_ci            if (cellRecord->GetFromWeakRefTarget().IsUndefined()) {
1554514f5e3Sopenharmony_ci                JSHandle<TaggedArray> argv = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(1);
1564514f5e3Sopenharmony_ci                argv->Set(thread, 0, cellRecord->GetHeldValue());
1574514f5e3Sopenharmony_ci                job::MicroJobQueue::EnqueueJob(thread, job, job::QueueType::QUEUE_PROMISE, func, argv);
1584514f5e3Sopenharmony_ci                cellVect->Delete(thread, i);
1594514f5e3Sopenharmony_ci            }
1604514f5e3Sopenharmony_ci        }
1614514f5e3Sopenharmony_ci    }
1624514f5e3Sopenharmony_ci}
1634514f5e3Sopenharmony_ci
1644514f5e3Sopenharmony_cibool JSFinalizationRegistry::CleanupFinalizationRegistry(JSThread *thread, JSHandle<JSFinalizationRegistry> obj)
1654514f5e3Sopenharmony_ci{
1664514f5e3Sopenharmony_ci    // 1. Assert: finalizationRegistry has [[Cells]] and [[CleanupCallback]] internal slots.
1674514f5e3Sopenharmony_ci    // 2. Let callback be finalizationRegistry.[[CleanupCallback]].
1684514f5e3Sopenharmony_ci    // 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is empty,
1694514f5e3Sopenharmony_ci    // an implementation may perform the following steps:
1704514f5e3Sopenharmony_ci    //     a. Choose any such cell.
1714514f5e3Sopenharmony_ci    //     b. Remove cell from finalizationRegistry.[[Cells]].
1724514f5e3Sopenharmony_ci    //     c. Perform ? HostCallJobCallback(callback, undefined, « cell.[[HeldValue]] »).
1734514f5e3Sopenharmony_ci    // 4. Return unused.
1744514f5e3Sopenharmony_ci    ASSERT(obj->IsECMAObject());
1754514f5e3Sopenharmony_ci    auto ecmaVm = thread->GetEcmaVM();
1764514f5e3Sopenharmony_ci    JSHandle<job::MicroJobQueue> job = thread->GetCurrentEcmaContext()->GetMicroJobQueue();
1774514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
1784514f5e3Sopenharmony_ci    JSHandle<JSFunction> func(thread, obj->GetCleanupCallback());
1794514f5e3Sopenharmony_ci    JSHandle<CellRecordVector> noUnregister(thread, obj->GetNoUnregister());
1804514f5e3Sopenharmony_ci    if (!noUnregister->Empty()) {
1814514f5e3Sopenharmony_ci        uint32_t noUnregisterLen = noUnregister->GetEnd();
1824514f5e3Sopenharmony_ci        for (uint32_t i = 0; i < noUnregisterLen; ++i) {
1834514f5e3Sopenharmony_ci            JSTaggedValue value = noUnregister->Get(i);
1844514f5e3Sopenharmony_ci            if (value.IsHole()) {
1854514f5e3Sopenharmony_ci                continue;
1864514f5e3Sopenharmony_ci            }
1874514f5e3Sopenharmony_ci            JSHandle<CellRecord> cellRecord(thread, value);
1884514f5e3Sopenharmony_ci            // if WeakRefTarget have been gc, set callback to job and delete
1894514f5e3Sopenharmony_ci            if (cellRecord->GetFromWeakRefTarget().IsUndefined()) {
1904514f5e3Sopenharmony_ci                JSHandle<TaggedArray> argv = factory->NewTaggedArray(1);
1914514f5e3Sopenharmony_ci                argv->Set(thread, 0, cellRecord->GetHeldValue());
1924514f5e3Sopenharmony_ci                job::MicroJobQueue::EnqueueJob(thread, job, job::QueueType::QUEUE_PROMISE, func, argv);
1934514f5e3Sopenharmony_ci                noUnregister->Delete(thread, i);
1944514f5e3Sopenharmony_ci            }
1954514f5e3Sopenharmony_ci        }
1964514f5e3Sopenharmony_ci    }
1974514f5e3Sopenharmony_ci    JSMutableHandle<LinkedHashMap> maybeUnregister(thread, obj->GetMaybeUnregister());
1984514f5e3Sopenharmony_ci    int index = 0;
1994514f5e3Sopenharmony_ci    int totalElements = maybeUnregister->NumberOfElements() + maybeUnregister->NumberOfDeletedElements();
2004514f5e3Sopenharmony_ci    while (index < totalElements) {
2014514f5e3Sopenharmony_ci        if (!maybeUnregister->GetKey(index++).IsHole()) {
2024514f5e3Sopenharmony_ci            JSHandle<CellRecordVector> cellVect(thread, maybeUnregister->GetValue(index - 1));
2034514f5e3Sopenharmony_ci            DealCallBackOfMap(thread, cellVect, job, func);
2044514f5e3Sopenharmony_ci            if (!cellVect->Empty()) {
2054514f5e3Sopenharmony_ci                continue;
2064514f5e3Sopenharmony_ci            }
2074514f5e3Sopenharmony_ci            maybeUnregister->RemoveEntry(thread, index - 1);
2084514f5e3Sopenharmony_ci        }
2094514f5e3Sopenharmony_ci    }
2104514f5e3Sopenharmony_ci    JSHandle<LinkedHashMap> newMap = LinkedHashMap::Shrink(thread, maybeUnregister);
2114514f5e3Sopenharmony_ci    obj->SetMaybeUnregister(thread, newMap);
2124514f5e3Sopenharmony_ci    // Determine whether the objects registered on the current JSFinalizationRegistry object
2134514f5e3Sopenharmony_ci    // have all been gc
2144514f5e3Sopenharmony_ci    int remainSize = newMap->NumberOfElements() + newMap->NumberOfDeletedElements();
2154514f5e3Sopenharmony_ci    if (noUnregister->IsEmpty() && remainSize == 0) {
2164514f5e3Sopenharmony_ci        return true;
2174514f5e3Sopenharmony_ci    }
2184514f5e3Sopenharmony_ci    return false;
2194514f5e3Sopenharmony_ci}
2204514f5e3Sopenharmony_ci
2214514f5e3Sopenharmony_civoid JSFinalizationRegistry::AddFinRegLists(JSThread *thread, JSHandle<JSFinalizationRegistry> next)
2224514f5e3Sopenharmony_ci{
2234514f5e3Sopenharmony_ci    // If any of prev and next is not null, it means that the current object is already in the linked list,
2244514f5e3Sopenharmony_ci    // ignore this addition
2254514f5e3Sopenharmony_ci    if (!next->GetPrev().IsNull() || !next->GetNext().IsNull()) {
2264514f5e3Sopenharmony_ci        return;
2274514f5e3Sopenharmony_ci    }
2284514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
2294514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> lists = env->GetFinRegLists();
2304514f5e3Sopenharmony_ci    if (!lists->IsUndefined()) {
2314514f5e3Sopenharmony_ci        JSHandle<JSFinalizationRegistry> prev(lists);
2324514f5e3Sopenharmony_ci        // Prevent the same object from being connected end to end,
2334514f5e3Sopenharmony_ci        // which should not happen and will lead to an infinite loop
2344514f5e3Sopenharmony_ci        if (JSTaggedValue::SameValue(next.GetTaggedValue(), prev.GetTaggedValue())) {
2354514f5e3Sopenharmony_ci            return;
2364514f5e3Sopenharmony_ci        }
2374514f5e3Sopenharmony_ci        prev->SetNext(thread, next);
2384514f5e3Sopenharmony_ci        next->SetPrev(thread, prev);
2394514f5e3Sopenharmony_ci    }
2404514f5e3Sopenharmony_ci    env->SetFinRegLists(thread, next);
2414514f5e3Sopenharmony_ci}
2424514f5e3Sopenharmony_ci} // namespace