1/*
2 * Copyright (c) 2023 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#ifndef ECMASCRIPT_MODULE_JS_MODULE_DEREGISTER_H
17#define ECMASCRIPT_MODULE_JS_MODULE_DEREGISTER_H
18
19#include "ecmascript/js_object.h"
20#include "ecmascript/js_tagged_value.h"
21#include "ecmascript/module/js_module_source_text.h"
22
23namespace panda::ecmascript {
24class ModuleDeregister {
25public:
26    static inline void InitForDeregisterModule(JSHandle<JSTaggedValue> moduleRecord, bool executeFromJob)
27    {
28        if (!executeFromJob) {
29            return;
30        }
31        LoadingTypes moduleLoadingType = LoadingTypes::DYNAMITC_MODULE;
32        JSHandle<SourceTextModule> module = JSHandle<SourceTextModule>::Cast(moduleRecord);
33        module->SetLoadingTypes(moduleLoadingType);
34        module->SetRegisterCounts(1);
35    }
36
37    static inline void ProcessModuleReference(JSThread *thread, const JSHandle<JSTaggedValue> &nameSpVal)
38    {
39        JSHandle<ModuleNamespace> nameSp = JSHandle<ModuleNamespace>::Cast(nameSpVal);
40        JSHandle<SourceTextModule> moduleRecord(thread, nameSp->GetModule());
41        JSTaggedValue weakNameSp = JSTaggedValue(nameSpVal->CreateAndGetWeakRef());
42        moduleRecord->SetNamespace(thread, weakNameSp);
43    }
44
45    static void FreeModuleRecord(void *env, void *pointer, void *hint);
46
47    static void RemoveModule(JSThread *thread, JSHandle<SourceTextModule> module);
48
49    static void ReviseLoadedModuleCount(JSThread *thread, const CString &moduleName);
50
51    static void IncreaseRegisterCounts(JSThread *thread, JSHandle<SourceTextModule> module,
52        std::set<CString> &increaseModule);
53
54    static void DecreaseRegisterCounts(JSThread *thread, JSHandle<SourceTextModule> module,
55        std::set<CString> &decreaseModule);
56};
57}  // namespace panda::ecmascript
58#endif  // ECMASCRIPT_MODULE_JS_MODULE_DEREGISTER_H
59