• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_MANAGER_H
17 #define ECMASCRIPT_MODULE_JS_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_source_text.h"
22 #include "ecmascript/napi/jsnapi_helper.h"
23 #include "ecmascript/tagged_dictionary.h"
24 
25 namespace panda::ecmascript {
26 enum class ModuleExecuteMode {
27     ExecuteZipMode,
28     ExecuteBufferMode
29 };
30 class ModuleManager {
31 public:
32     explicit ModuleManager(EcmaVM *vm);
~ModuleManager()33     ~ModuleManager()
34     {
35         InstantiatingSModuleList_.clear();
36         resolvedModules_.clear();
37     }
38 
39     JSTaggedValue GetModuleValueInner(int32_t index);
40     JSTaggedValue GetModuleValueInner(int32_t index, JSTaggedValue jsFunc);
41     JSTaggedValue GetModuleValueInner(int32_t index, JSHandle<JSTaggedValue> currentModule);
42     JSTaggedValue GetModuleValueOutter(int32_t index);
43     JSTaggedValue GetModuleValueOutter(int32_t index, JSTaggedValue jsFunc);
44     JSTaggedValue GetModuleValueOutter(int32_t index, JSHandle<JSTaggedValue> currentModule);
45     JSTaggedValue GetLazyModuleValueOutter(int32_t index, JSTaggedValue jsFunc);
46     void StoreModuleValue(int32_t index, JSTaggedValue value);
47     void StoreModuleValue(int32_t index, JSTaggedValue value, JSTaggedValue jsFunc);
48     JSTaggedValue GetModuleNamespace(int32_t index);
49     JSTaggedValue GetModuleNamespace(int32_t index, JSTaggedValue currentFunc);
50     JSTaggedValue GetModuleNamespaceInternal(int32_t index, JSTaggedValue currentModule);
51 
52     // deprecated begin
53     JSTaggedValue GetModuleValueInner(JSTaggedValue key);
54     JSTaggedValue GetModuleValueInner(JSTaggedValue key, JSTaggedValue jsFunc);
55     JSTaggedValue GetModuleValueOutter(JSTaggedValue key);
56     JSTaggedValue GetModuleValueOutter(JSTaggedValue key, JSTaggedValue jsFunc);
57     void StoreModuleValue(JSTaggedValue key, JSTaggedValue value);
58     void StoreModuleValue(JSTaggedValue key, JSTaggedValue value, JSTaggedValue jsFunc);
59     JSTaggedValue GetModuleNamespace(JSTaggedValue localName);
60     JSTaggedValue GetModuleNamespace(JSTaggedValue localName, JSTaggedValue currentFunc);
61     JSTaggedValue GetModuleNamespaceInternal(JSTaggedValue localName, JSTaggedValue currentModule);
62     // deprecated end
63 
64     JSHandle<SourceTextModule> GetImportedModule(const CString &referencing);
65     JSHandle<SourceTextModule> PUBLIC_API HostGetImportedModule(const CString &referencing);
66     JSTaggedValue HostGetImportedModule(void *src);
67     bool IsLocalModuleLoaded(const CString& referencing);
68     bool IsSharedModuleLoaded(const CString &referencing);
69     bool IsModuleLoaded(const CString &referencing);
70 
71     bool IsEvaluatedModule(const CString &referencing);
72     bool IsInstantiatedModule(const CString &referencing);
73     bool IsLocalModuleInstantiated(const CString &referencing);
74     bool NeedExecuteModule(const CString &referencing);
75 
76     JSHandle<JSTaggedValue> LoadNativeModule(JSThread *thread, const CString &key);
77 
78     JSHandle<JSTaggedValue> ExecuteNativeModuleMayThrowError(JSThread *thread, const CString &recordName);
79 
80     JSHandle<JSTaggedValue> ExecuteNativeModule(JSThread *thread, const CString &recordName);
81 
82     JSHandle<JSTaggedValue> ExecuteJsonModule(JSThread *thread, const CString &recordName,
83                                               const CString &filename, const JSPandaFile *jsPandaFile);
84     JSHandle<JSTaggedValue> ExecuteCjsModule(JSThread *thread, const CString &recordName,
85                                              const JSPandaFile *jsPandaFile);
86 
87     JSTaggedValue GetCurrentModule();
88     JSHandle<JSTaggedValue> GenerateSendableFuncModule(const JSHandle<JSTaggedValue> &module);
89 
90     JSHandle<JSTaggedValue> TryGetImportedModule(const CString& referencing);
91     void Iterate(RootVisitor &v);
92     void AddToInstantiatingSModuleList(const CString &record);
GetExecuteMode()93     ModuleExecuteMode GetExecuteMode() const
94     {
95         return isExecuteBuffer_.load(std::memory_order_acquire);
96     }
SetExecuteMode(ModuleExecuteMode mode)97     void SetExecuteMode(ModuleExecuteMode mode)
98     {
99         isExecuteBuffer_.store(mode, std::memory_order_release);
100     }
101 
102     static CString PUBLIC_API GetRecordName(JSTaggedValue module);
103     static int GetExportObjectIndex(EcmaVM *vm, JSHandle<SourceTextModule> ecmaModule, const CString &key);
104 
NextModuleAsyncEvaluatingOrdinal()105     uint32_t NextModuleAsyncEvaluatingOrdinal()
106     {
107         uint32_t ordinal = nextModuleAsyncEvaluatingOrdinal_++;
108         return ordinal;
109     }
AddResolveImportedModule(const CString & recordName,JSTaggedValue module)110     inline void AddResolveImportedModule(const CString &recordName, JSTaggedValue module)
111     {
112         resolvedModules_.emplace(recordName, module);
113     }
114 
UpdateResolveImportedModule(const CString & recordName,JSTaggedValue module)115     inline void UpdateResolveImportedModule(const CString &recordName, JSTaggedValue module)
116     {
117         resolvedModules_[recordName] = module;
118     }
119 
NativeObjDestory()120     void NativeObjDestory()
121     {
122         for (auto it = resolvedModules_.begin(); it != resolvedModules_.end(); it++) {
123             CString key = it->first;
124             ASSERT(!key.empty());
125             JSTaggedValue module = it->second;
126             SourceTextModule::Cast(module)->DestoryLazyImportArray();
127             SourceTextModule::Cast(module)->DestoryEcmaModuleFilenameString();
128             SourceTextModule::Cast(module)->DestoryEcmaModuleRecordNameString();
129         }
130     }
IsVMBundlePack()131     inline bool IsVMBundlePack()
132     {
133         return vm_->IsBundlePack();
134     }
135 
136 private:
137     NO_COPY_SEMANTIC(ModuleManager);
138     NO_MOVE_SEMANTIC(ModuleManager);
139 
140     JSTaggedValue GetModuleValueOutterInternal(int32_t index, JSTaggedValue currentModule);
141     void StoreModuleValueInternal(JSHandle<SourceTextModule> &currentModule,
142                                   int32_t index, JSTaggedValue value);
143 
144     JSTaggedValue GetLazyModuleValueOutterInternal(int32_t index, JSTaggedValue currentModule);
145 
146     // deprecated begin
147     JSTaggedValue GetModuleValueOutterInternal(JSTaggedValue key, JSTaggedValue currentModule);
148     void StoreModuleValueInternal(JSHandle<SourceTextModule> &currentModule,
149                                   JSTaggedValue key, JSTaggedValue value);
150     // deprecated end
151 
152     CVector<CString> GetInstantiatingSModuleList();
153 
154     void ClearInstantiatingSModuleList();
155 
156     void RemoveModuleFromCache(const CString &recordName);
157 
158     void RemoveModuleNameFromList(const CString &recordName);
159 
160     static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4;
161 
162     uint32_t nextModuleAsyncEvaluatingOrdinal_{SourceTextModule::FIRST_ASYNC_EVALUATING_ORDINAL};
163 
164     EcmaVM *vm_ {nullptr};
165     CUnorderedMap<CString, JSTaggedValue> resolvedModules_;
166     std::atomic<ModuleExecuteMode> isExecuteBuffer_ {ModuleExecuteMode::ExecuteZipMode};
167     CVector<CString> InstantiatingSModuleList_;
168 
169     friend class EcmaVM;
170     friend class PatchLoader;
171     friend class ModuleDeregister;
172     friend class SharedModuleManager;
173 };
174 } // namespace panda::ecmascript
175 #endif // ECMASCRIPT_MODULE_JS_MODULE_MANAGER_H
176