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