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