1/*
2 * Copyright (c) 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#ifndef ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
17#define ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
18
19#include "ecmascript/js_tagged_value-inl.h"
20#include "ecmascript/jspandafile/js_pandafile.h"
21#include "ecmascript/module/js_module_manager.h"
22#include "ecmascript/module/js_module_source_text.h"
23#include "ecmascript/module/js_shared_module.h"
24#include "ecmascript/napi/jsnapi_helper.h"
25#include "ecmascript/napi/jsnapi_helper.h"
26#include "ecmascript/tagged_dictionary.h"
27
28namespace panda::ecmascript {
29struct StateVisit {
30    Mutex mutex;
31    ConditionVariable cv;
32    uint32_t threadId;
33};
34class SharedModuleManager {
35public:
36    static PUBLIC_API SharedModuleManager *GetInstance();
37
38    void Initialize() {};
39
40    void Destroy()
41    {
42        resolvedSharedModules_.clear();
43    }
44
45    JSTaggedValue GetSendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
46
47    JSTaggedValue GetSendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
48
49    JSTaggedValue GetLazySendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
50
51    JSTaggedValue GetLazySendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
52
53    void Iterate(const RootVisitor &v);
54
55    JSHandle<JSTaggedValue> ResolveImportedModule(JSThread *thread, const CString &referencingModule,
56                                                  bool executeFromJob);
57
58    JSHandle<JSTaggedValue> PUBLIC_API ResolveImportedModuleWithMerge(JSThread *thread, const CString &fileName,
59                                                                      const CString &recordName, bool executeFromJob);
60
61    StateVisit &findModuleMutexWithLock(JSThread *thread, const JSHandle<SourceTextModule> &module);
62
63    bool SearchInSModuleManager(JSThread *thread, const CString &recordName);
64
65    void InsertInSModuleManager(JSThread *thread, const CString &recordName,
66        JSHandle<SourceTextModule> &moduleRecord);
67
68    void PUBLIC_API TransferSModule(JSThread *thread);
69
70    bool IsInstantiatedSModule(JSThread *thread, const JSHandle<SourceTextModule> &module);
71
72    JSHandle<JSTaggedValue> GenerateFuncModule(JSThread *thread, const JSPandaFile *jsPandaFile,
73        const CString &entryPoint, ClassKind classKind = ClassKind::NON_SENDABLE);
74
75    JSHandle<ModuleNamespace> SModuleNamespaceCreate(JSThread *thread, const JSHandle<JSTaggedValue> &module,
76                                                            const JSHandle<TaggedArray> &exports);
77
78    inline void AddResolveImportedSModule(const CString &recordName, JSTaggedValue module)
79    {
80        resolvedSharedModules_.emplace(recordName, module);
81    }
82
83    inline void UpdateResolveImportedSModule(const CString &recordName, JSTaggedValue module)
84    {
85        resolvedSharedModules_[recordName] = module;
86    }
87    void SharedNativeObjDestory();
88
89private:
90    SharedModuleManager() = default;
91    ~SharedModuleManager() = default;
92
93    NO_COPY_SEMANTIC(SharedModuleManager);
94    NO_MOVE_SEMANTIC(SharedModuleManager);
95
96    JSHandle<JSTaggedValue> ResolveSharedImportedModuleWithMerge(JSThread *thread, const CString &fileName,
97        const CString &recordName, const JSPandaFile *jsPandaFile, JSRecordInfo *recordInfo);
98
99    bool SearchInSModuleManagerUnsafe(const CString &recordName);
100
101    JSHandle<SourceTextModule> GetSModuleUnsafe(JSThread *thread, const CString &recordName);
102
103    JSHandle<SourceTextModule> GetSModule(JSThread *thread, const CString &recordName);
104
105    static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4;
106    CUnorderedMap<CString, JSTaggedValue> resolvedSharedModules_;
107    CMap<CString, StateVisit> sharedModuleMutex_;
108    Mutex mutex_;
109
110    friend class SourceTextModule;
111    friend class EcmaContext;
112    friend class ModuleManager;
113};
114} // namespace panda::ecmascript
115#endif // ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
116