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_ECMA_MODULE_H 17 #define ECMASCRIPT_ECMA_MODULE_H 18 19 #include "ecmascript/js_object.h" 20 #include "ecmascript/js_tagged_value-inl.h" 21 #include "ecmascript/mem/c_string.h" 22 23 namespace panda::ecmascript { 24 class EcmaVm; 25 26 // Forward declaration 27 class EcmaModule : public ECMAObject { 28 public: Cast(ObjectHeader * object)29 static EcmaModule *Cast(ObjectHeader *object) 30 { 31 return static_cast<EcmaModule *>(object); 32 } 33 34 JSHandle<JSTaggedValue> GetItem(const JSThread *thread, JSHandle<JSTaggedValue> itemName); 35 36 static void AddItem(const JSThread *thread, JSHandle<EcmaModule> module, JSHandle<JSTaggedValue> itemName, 37 JSHandle<JSTaggedValue> itemValue); 38 39 static void RemoveItem(const JSThread *thread, JSHandle<EcmaModule> module, JSHandle<JSTaggedValue> itemName); 40 41 void DebugPrint(const JSThread *thread, const CString &caller); 42 43 static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4; 44 45 static constexpr size_t NAME_DICTIONARY_OFFSET = ECMAObject::SIZE; 46 ACCESSORS(NameDictionary, NAME_DICTIONARY_OFFSET, SIZE) 47 48 DECL_VISIT_OBJECT_FOR_JS_OBJECT(ECMAObject, NAME_DICTIONARY_OFFSET, SIZE) 49 DECL_DUMP() 50 51 protected: 52 static void CopyModuleInternal(const JSThread *thread, JSHandle<EcmaModule> dstModule, 53 JSHandle<EcmaModule> srcModule); 54 55 friend class ModuleManager; 56 }; 57 58 class ModuleManager { 59 public: 60 explicit ModuleManager(EcmaVM *vm); 61 ~ModuleManager() = default; 62 63 void AddModule(JSHandle<JSTaggedValue> moduleName, JSHandle<JSTaggedValue> module); 64 65 void RemoveModule(JSHandle<JSTaggedValue> moduleName); 66 67 JSHandle<JSTaggedValue> GetModule(const JSThread *thread, JSHandle<JSTaggedValue> moduleName); 68 69 CString GenerateAmiPath(const CString ¤tPathFile, const CString &relativeFile); 70 71 const CString &GetCurrentExportModuleName(); 72 73 const CString &GetPrevExportModuleName(); 74 75 void SetCurrentExportModuleName(const std::string_view &moduleFile); 76 77 void RestoreCurrentExportModuleName(); 78 79 void AddModuleItem(const JSThread *thread, JSHandle<JSTaggedValue> itemName, JSHandle<JSTaggedValue> value); 80 81 JSHandle<JSTaggedValue> GetModuleItem(const JSThread *thread, JSHandle<JSTaggedValue> module, 82 JSHandle<JSTaggedValue> itemName); 83 84 void CopyModule(const JSThread *thread, JSHandle<JSTaggedValue> src); 85 86 void DebugPrint(const JSThread *thread, const CString &caller); 87 Iterate(const RootVisitor & v)88 void Iterate(const RootVisitor &v) 89 { 90 v(Root::ROOT_VM, ObjectSlot(reinterpret_cast<uintptr_t>(&ecmaModules_))); 91 } 92 93 private: 94 static constexpr uint32_t DEAULT_DICTIONART_CAPACITY = 4; 95 96 NO_COPY_SEMANTIC(ModuleManager); 97 NO_MOVE_SEMANTIC(ModuleManager); 98 99 EcmaVM *vm_{nullptr}; 100 JSTaggedValue ecmaModules_ {JSTaggedValue::Hole()}; 101 std::vector<CString> moduleNames_ {DEAULT_DICTIONART_CAPACITY}; 102 103 friend class EcmaVM; 104 }; 105 } // namespace panda::ecmascript 106 107 #endif 108