• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace panda::ecmascript {
29 struct StateVisit {
30     Mutex mutex;
31     ConditionVariable cv;
32     uint32_t threadId;
33 };
34 class SharedModuleManager {
35 public:
36     static PUBLIC_API SharedModuleManager *GetInstance();
37 
Initialize()38     void Initialize() {};
39 
Destroy()40     void Destroy()
41     {
42         resolvedSharedModules_.clear();
43     }
44 
45     JSTaggedValue GetSendableModuleValueInner(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
46 
47     JSTaggedValue GetSendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
48 
49     JSTaggedValue GetSendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
50 
51     JSTaggedValue GetLazySendableModuleValue(JSThread *thread, int32_t index, JSTaggedValue jsFunc);
52 
53     JSTaggedValue GetLazySendableModuleValueImpl(JSThread *thread, int32_t index, JSTaggedValue currentModule) const;
54 
55     void Iterate(RootVisitor &v);
56 
57     StateVisit &findModuleMutexWithLock(JSThread *thread, const JSHandle<SourceTextModule> &module);
58 
59     bool SearchInSModuleManager(JSThread *thread, const CString &recordName);
60 
61     void InsertInSModuleManager(JSThread *thread, const CString &recordName,
62         JSHandle<SourceTextModule> &moduleRecord);
63 
64     JSHandle<SourceTextModule> GetSModule(JSThread *thread, const CString &recordName);
65 
66     void PUBLIC_API TransferSModule(JSThread *thread);
67 
68     bool IsInstantiatedSModule(JSThread *thread, const JSHandle<SourceTextModule> &module);
69 
70     JSHandle<JSTaggedValue> GenerateFuncModule(JSThread *thread, const JSPandaFile *jsPandaFile,
71         const CString &entryPoint, ClassKind classKind = ClassKind::NON_SENDABLE);
72 
73     JSHandle<ModuleNamespace> SModuleNamespaceCreate(JSThread *thread, const JSHandle<JSTaggedValue> &module,
74                                                             const JSHandle<TaggedArray> &exports);
75 
AddResolveImportedSModule(const CString & recordName,JSTaggedValue module)76     inline void AddResolveImportedSModule(const CString &recordName, JSTaggedValue module)
77     {
78         resolvedSharedModules_.emplace(recordName, module);
79     }
80 
UpdateResolveImportedSModule(const CString & recordName,JSTaggedValue module)81     inline void UpdateResolveImportedSModule(const CString &recordName, JSTaggedValue module)
82     {
83         resolvedSharedModules_[recordName] = module;
84     }
85     void SharedNativeObjDestory();
86 
GetSharedMutex()87     RecursiveMutex& GetSharedMutex()
88     {
89         return sharedMutex_;
90     }
91 
92 private:
93     SharedModuleManager() = default;
94     ~SharedModuleManager() = default;
95 
96     NO_COPY_SEMANTIC(SharedModuleManager);
97     NO_MOVE_SEMANTIC(SharedModuleManager);
98 
99     bool SearchInSModuleManagerUnsafe(const CString &recordName);
100 
101     JSHandle<SourceTextModule> GetSModuleUnsafe(JSThread *thread, const CString &recordName);
102 
103     static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4;
104     CUnorderedMap<CString, JSTaggedValue> resolvedSharedModules_;
105     CMap<CString, StateVisit> sharedModuleMutex_;
106     Mutex mutex_;
107     RecursiveMutex sharedMutex_;
108 
109     friend class SourceTextModule;
110     friend class EcmaContext;
111     friend class ModuleManager;
112 };
113 } // namespace panda::ecmascript
114 #endif // ECMASCRIPT_MODULE_JS_SHARED_MODULE_MANAGER_H
115