• 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 
24 namespace panda::ecmascript {
25 class ModuleManager {
26 public:
27     explicit ModuleManager(EcmaVM *vm);
28     ~ModuleManager() = default;
29 
30     JSTaggedValue GetModuleValueInner(int32_t index);
31     JSTaggedValue GetModuleValueInner(int32_t index, JSTaggedValue jsFunc);
32     JSTaggedValue GetModuleValueInner(int32_t index, JSHandle<JSTaggedValue> currentModule);
33     JSTaggedValue GetModuleValueOutter(int32_t index);
34     JSTaggedValue GetModuleValueOutter(int32_t index, JSTaggedValue jsFunc);
35     JSTaggedValue GetModuleValueOutter(int32_t index, JSHandle<JSTaggedValue> currentModule);
36     void StoreModuleValue(int32_t index, JSTaggedValue value);
37     void StoreModuleValue(int32_t index, JSTaggedValue value, JSTaggedValue jsFunc);
38     JSTaggedValue GetModuleNamespace(int32_t index);
39     JSTaggedValue GetModuleNamespace(int32_t index, JSTaggedValue currentFunc);
40     JSTaggedValue GetModuleNamespaceInternal(int32_t index, JSTaggedValue currentModule);
41 
42     // deprecated begin
43     JSTaggedValue GetModuleValueInner(JSTaggedValue key);
44     JSTaggedValue GetModuleValueInner(JSTaggedValue key, JSTaggedValue jsFunc);
45     JSTaggedValue GetModuleValueOutter(JSTaggedValue key);
46     JSTaggedValue GetModuleValueOutter(JSTaggedValue key, JSTaggedValue jsFunc);
47     void StoreModuleValue(JSTaggedValue key, JSTaggedValue value);
48     void StoreModuleValue(JSTaggedValue key, JSTaggedValue value, JSTaggedValue jsFunc);
49     JSTaggedValue GetModuleNamespace(JSTaggedValue localName);
50     JSTaggedValue GetModuleNamespace(JSTaggedValue localName, JSTaggedValue currentFunc);
51     JSTaggedValue GetModuleNamespaceInternal(JSTaggedValue localName, JSTaggedValue currentModule);
52     // deprecated end
53 
54     JSHandle<SourceTextModule> HostGetImportedModule(const CString &referencingModule);
55     JSHandle<SourceTextModule> HostGetImportedModule(JSTaggedValue referencing);
56     JSHandle<SourceTextModule> HostGetImportedModule(void *src);
57     bool IsImportedModuleLoaded(JSTaggedValue referencing);
58 
59     JSHandle<JSTaggedValue> ResolveNativeModule(const CString &moduleRequestName, ModuleTypes moduleType);
60     JSHandle<JSTaggedValue> HostResolveImportedModule(const void *buffer, size_t size, const CString &filename);
61     JSHandle<JSTaggedValue> HostResolveImportedModule(const CString &referencingModule,
62         bool excuteFromJob = false);
63     JSHandle<JSTaggedValue> PUBLIC_API HostResolveImportedModuleWithMerge(const CString &referencingModule,
64         const CString &recordName, bool excuteFromJob = false);
65     JSHandle<JSTaggedValue> HostResolveImportedModule(const JSPandaFile *jsPandaFile, const CString &filename);
66 
67     JSTaggedValue GetCurrentModule();
68     JSTaggedValue GetNativeModuleValue(JSThread *thread, JSTaggedValue currentModule,
69         JSTaggedValue resolvedModule, ResolvedIndexBinding *binding);
70     JSTaggedValue GetCJSModuleValue(JSThread *thread, JSTaggedValue currentModule,
71         JSTaggedValue resolvedModule, ResolvedIndexBinding *binding);
72     void AddResolveImportedModule(const JSPandaFile *jsPandaFile, const CString &referencingModule);
73     void AddResolveImportedModule(const CString &referencingModule, JSHandle<JSTaggedValue> moduleRecord);
74     void Iterate(const RootVisitor &v);
75 
GetExecuteMode()76     bool GetExecuteMode() const
77     {
78         return isExecuteBuffer_;
79     }
SetExecuteMode(bool mode)80     void SetExecuteMode(bool mode)
81     {
82         isExecuteBuffer_ = mode;
83     }
84 
85     static CString GetRecordName(JSTaggedValue module);
86     static int GetExportObjectIndex(EcmaVM *vm, JSHandle<SourceTextModule> ecmaModule, const std::string &key);
87 
88 private:
89     NO_COPY_SEMANTIC(ModuleManager);
90     NO_MOVE_SEMANTIC(ModuleManager);
91 
92     bool SkipDefaultBundleFile(const CString &moduleFileName) const;
93     JSHandle<JSTaggedValue> ResolveModuleInMergedABC(JSThread *thread, const JSPandaFile *jsPandaFile,
94         const CString &recordName, bool excuteFromJob = false);
95     JSHandle<JSTaggedValue> CreateEmptyModule();
96     JSTaggedValue GetModuleValueOutterInternal(int32_t index, JSTaggedValue currentModule);
97     void StoreModuleValueInternal(JSHandle<SourceTextModule> &currentModule,
98                                   int32_t index, JSTaggedValue value);
99 
100     JSTaggedValue GetValueFromExportObject(JSHandle<JSTaggedValue> &exportObject, int32_t index);
101 
102     // deprecated begin
103     JSTaggedValue GetModuleValueOutterInternal(JSTaggedValue key, JSTaggedValue currentModule);
104     void StoreModuleValueInternal(JSHandle<SourceTextModule> &currentModule,
105                                   JSTaggedValue key, JSTaggedValue value);
106     // deprecated end
107 
108     JSHandle<JSTaggedValue> ResolveModule(JSThread *thread, const JSPandaFile *jsPandaFile,
109         bool excuteFromJob = false);
110     JSHandle<JSTaggedValue> ResolveModuleWithMerge(JSThread *thread, const JSPandaFile *jsPandaFile,
111         const CString &recordName, bool excuteFromJob = false);
112 
113     static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4;
114 
115     EcmaVM *vm_ {nullptr};
116     JSTaggedValue resolvedModules_ {JSTaggedValue::Hole()};
117     JSTaggedValue cachedEmptyModule_ {JSTaggedValue::Hole()};
118     bool isExecuteBuffer_ {false};
119 
120     friend class EcmaVM;
121     friend class PatchLoader;
122     friend class ModuleDeregister;
123 };
124 } // namespace panda::ecmascript
125 #endif // ECMASCRIPT_MODULE_JS_MODULE_MANAGER_H
126