14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License. 54514f5e3Sopenharmony_ci * You may obtain a copy of the License at 64514f5e3Sopenharmony_ci * 74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84514f5e3Sopenharmony_ci * 94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and 134514f5e3Sopenharmony_ci * limitations under the License. 144514f5e3Sopenharmony_ci */ 154514f5e3Sopenharmony_ci 164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MODULE_JS_MODULE_SOURCE_TEXT_H 174514f5e3Sopenharmony_ci#define ECMASCRIPT_MODULE_JS_MODULE_SOURCE_TEXT_H 184514f5e3Sopenharmony_ci 194514f5e3Sopenharmony_ci#include "ecmascript/base/string_helper.h" 204514f5e3Sopenharmony_ci#include "ecmascript/mem/c_containers.h" 214514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_record.h" 224514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_entry.h" 234514f5e3Sopenharmony_ci#include "ecmascript/tagged_array.h" 244514f5e3Sopenharmony_ci#include "ecmascript/sendable_env.h" 254514f5e3Sopenharmony_ci 264514f5e3Sopenharmony_cinamespace panda::ecmascript { 274514f5e3Sopenharmony_cistruct StateVisit; 284514f5e3Sopenharmony_cienum class ModuleStatus : uint8_t { 294514f5e3Sopenharmony_ci UNINSTANTIATED = 0x01, 304514f5e3Sopenharmony_ci INSTANTIATING, 314514f5e3Sopenharmony_ci INSTANTIATED, 324514f5e3Sopenharmony_ci EVALUATING, 334514f5e3Sopenharmony_ci EVALUATING_ASYNC, 344514f5e3Sopenharmony_ci EVALUATED 354514f5e3Sopenharmony_ci}; 364514f5e3Sopenharmony_ci 374514f5e3Sopenharmony_cienum class ModuleTypes : uint8_t { 384514f5e3Sopenharmony_ci ECMA_MODULE = 0x01, 394514f5e3Sopenharmony_ci CJS_MODULE, 404514f5e3Sopenharmony_ci JSON_MODULE, 414514f5e3Sopenharmony_ci NATIVE_MODULE, 424514f5e3Sopenharmony_ci OHOS_MODULE, 434514f5e3Sopenharmony_ci APP_MODULE, 444514f5e3Sopenharmony_ci INTERNAL_MODULE, 454514f5e3Sopenharmony_ci UNKNOWN 464514f5e3Sopenharmony_ci}; 474514f5e3Sopenharmony_ci 484514f5e3Sopenharmony_cienum class LoadingTypes : uint8_t { 494514f5e3Sopenharmony_ci STABLE_MODULE = 0x01, 504514f5e3Sopenharmony_ci DYNAMITC_MODULE, 514514f5e3Sopenharmony_ci OTHERS 524514f5e3Sopenharmony_ci}; 534514f5e3Sopenharmony_ci 544514f5e3Sopenharmony_cienum class SharedTypes : uint8_t { 554514f5e3Sopenharmony_ci UNSENDABLE_MODULE = 0x01, 564514f5e3Sopenharmony_ci SENDABLE_FUNCTION_MODULE, 574514f5e3Sopenharmony_ci SHARED_MODULE, 584514f5e3Sopenharmony_ci TOTAL_KINDS, 594514f5e3Sopenharmony_ci}; 604514f5e3Sopenharmony_ci 614514f5e3Sopenharmony_ciclass SourceTextModule final : public ModuleRecord { 624514f5e3Sopenharmony_cipublic: 634514f5e3Sopenharmony_ci static constexpr int UNDEFINED_INDEX = -1; 644514f5e3Sopenharmony_ci static constexpr int MODULE_ERROR = 1; 654514f5e3Sopenharmony_ci static constexpr size_t DEFAULT_DICTIONART_CAPACITY = 2; 664514f5e3Sopenharmony_ci static constexpr size_t DEFAULT_ARRAY_CAPACITY = 2; 674514f5e3Sopenharmony_ci static constexpr uint8_t DEREGISTER_MODULE_TAG = 1; 684514f5e3Sopenharmony_ci static constexpr uint32_t FIRST_ASYNC_EVALUATING_ORDINAL = 2; 694514f5e3Sopenharmony_ci static constexpr uint32_t NOT_ASYNC_EVALUATED = 0; 704514f5e3Sopenharmony_ci static constexpr uint32_t ASYNC_EVALUATE_DID_FINISH = 1; 714514f5e3Sopenharmony_ci static constexpr bool SHARED_MODULE_TAG = true; 724514f5e3Sopenharmony_ci 734514f5e3Sopenharmony_ci struct AsyncEvaluatingOrdinalCompare { 744514f5e3Sopenharmony_ci bool operator()(const JSHandle<SourceTextModule> &lhs, const JSHandle<SourceTextModule> &rhs) const 754514f5e3Sopenharmony_ci { 764514f5e3Sopenharmony_ci return lhs->GetAsyncEvaluatingOrdinal() < rhs->GetAsyncEvaluatingOrdinal(); 774514f5e3Sopenharmony_ci } 784514f5e3Sopenharmony_ci }; 794514f5e3Sopenharmony_ci using AsyncParentCompletionSet = 804514f5e3Sopenharmony_ci CSet<JSHandle<SourceTextModule>, AsyncEvaluatingOrdinalCompare>; 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci CAST_CHECK(SourceTextModule, IsSourceTextModule); 834514f5e3Sopenharmony_ci 844514f5e3Sopenharmony_ci // 15.2.1.17 Runtime Semantics: HostResolveImportedModule ( referencingModule, specifier ) 854514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> HostResolveImportedModule(JSThread *thread, 864514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module, 874514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &moduleRequest, 884514f5e3Sopenharmony_ci bool executeFromJob = false); 894514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> HostResolveImportedModuleWithMerge(JSThread *thread, 904514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module, 914514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &moduleRequest, 924514f5e3Sopenharmony_ci bool executeFromJob = false); 934514f5e3Sopenharmony_ci 944514f5e3Sopenharmony_ci static CString ReplaceModuleThroughFeature(JSThread *thread, const CString &requestName); 954514f5e3Sopenharmony_ci 964514f5e3Sopenharmony_ci // 15.2.1.16.2 GetExportedNames(exportStarSet) 974514f5e3Sopenharmony_ci static CVector<std::string> GetExportedNames(JSThread *thread, const JSHandle<SourceTextModule> &module, 984514f5e3Sopenharmony_ci const JSHandle<TaggedArray> &exportStarSet); 994514f5e3Sopenharmony_ci 1004514f5e3Sopenharmony_ci // 15.2.1.16.3 ResolveExport(exportName, resolveVector) 1014514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveExport(JSThread *thread, const JSHandle<SourceTextModule> &module, 1024514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName, 1034514f5e3Sopenharmony_ci CVector<std::pair<JSHandle<SourceTextModule>, JSHandle<JSTaggedValue>>> &resolveVector); 1044514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveExportObject(JSThread *thread, const JSHandle<SourceTextModule> &module, 1054514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportObject, 1064514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName); 1074514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveNativeStarExport(JSThread *thread, 1084514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &nativeModule, 1094514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName); 1104514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveCjsStarExport(JSThread *thread, 1114514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &cjsModule, 1124514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName); 1134514f5e3Sopenharmony_ci // 15.2.1.16.4.1 InnerModuleInstantiation ( module, stack, index ) 1144514f5e3Sopenharmony_ci static int InnerModuleInstantiation(JSThread *thread, 1154514f5e3Sopenharmony_ci const JSHandle<ModuleRecord> &moduleRecord, CVector<JSHandle<SourceTextModule>> &stack, 1164514f5e3Sopenharmony_ci int index, bool executeFromJob = false); 1174514f5e3Sopenharmony_ci 1184514f5e3Sopenharmony_ci // 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup ( module ) 1194514f5e3Sopenharmony_ci static void ModuleDeclarationEnvironmentSetup(JSThread *thread, const JSHandle<SourceTextModule> &module); 1204514f5e3Sopenharmony_ci static void ModuleDeclarationArrayEnvironmentSetup(JSThread *thread, const JSHandle<SourceTextModule> &module); 1214514f5e3Sopenharmony_ci 1224514f5e3Sopenharmony_ci // 15.2.1.16.5.1 InnerModuleEvaluation ( module, stack, index ) 1234514f5e3Sopenharmony_ci static int InnerModuleEvaluation(JSThread *thread, const JSHandle<SourceTextModule> &moduleRecord, 1244514f5e3Sopenharmony_ci CVector<JSHandle<SourceTextModule>> &stack, int index, const void *buffer = nullptr, 1254514f5e3Sopenharmony_ci size_t size = 0, bool executeFromJob = false); 1264514f5e3Sopenharmony_ci 1274514f5e3Sopenharmony_ci static int InnerModuleEvaluationUnsafe(JSThread *thread, 1284514f5e3Sopenharmony_ci const JSHandle<ModuleRecord> &moduleRecord, CVector<JSHandle<SourceTextModule>> &stack, 1294514f5e3Sopenharmony_ci int index, const void *buffer, size_t size, bool executeFromJob); 1304514f5e3Sopenharmony_ci // 15.2.1.16.5.2 ModuleExecution ( module ) 1314514f5e3Sopenharmony_ci static Expected<JSTaggedValue, bool> ModuleExecution(JSThread *thread, const JSHandle<SourceTextModule> &module, 1324514f5e3Sopenharmony_ci const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 1334514f5e3Sopenharmony_ci 1344514f5e3Sopenharmony_ci // 16.2.1.5.3.2 ExecuteAsyncModule ( module ) 1354514f5e3Sopenharmony_ci static void ExecuteAsyncModule(JSThread *thread, const JSHandle<SourceTextModule> &module, 1364514f5e3Sopenharmony_ci const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 1374514f5e3Sopenharmony_ci 1384514f5e3Sopenharmony_ci // 16.2.1.5.3.3 GatherAvailableAncestors ( module, execList ) 1394514f5e3Sopenharmony_ci static void GatherAvailableAncestors(JSThread *thread, const JSHandle<SourceTextModule> &module, 1404514f5e3Sopenharmony_ci AsyncParentCompletionSet &execList); 1414514f5e3Sopenharmony_ci 1424514f5e3Sopenharmony_ci // 16.2.1.5.3.4 AsyncModuleExecutionFulfilled ( module ) 1434514f5e3Sopenharmony_ci static void AsyncModuleExecutionFulfilled(JSThread *thread, const JSHandle<SourceTextModule> &module); 1444514f5e3Sopenharmony_ci 1454514f5e3Sopenharmony_ci // 16.2.1.5.3.5 AsyncModuleExecutionRejected ( module, error ) 1464514f5e3Sopenharmony_ci static void AsyncModuleExecutionRejected(JSThread *thread, const JSHandle<SourceTextModule> &module, 1474514f5e3Sopenharmony_ci JSTaggedValue error); 1484514f5e3Sopenharmony_ci 1494514f5e3Sopenharmony_ci static JSTaggedValue AsyncModuleFulfilledFunc(EcmaRuntimeCallInfo *argv); 1504514f5e3Sopenharmony_ci static JSTaggedValue AsyncModuleRejectedFunc(EcmaRuntimeCallInfo *argv); 1514514f5e3Sopenharmony_ci static void AddAsyncParentModule(JSThread *thread, JSHandle<SourceTextModule> &module, 1524514f5e3Sopenharmony_ci JSHandle<SourceTextModule> &parent); 1534514f5e3Sopenharmony_ci // 15.2.1.18 Runtime Semantics: GetModuleNamespace ( module ) 1544514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> GetModuleNamespace(JSThread *thread, const JSHandle<SourceTextModule> &module); 1554514f5e3Sopenharmony_ci 1564514f5e3Sopenharmony_ci static void AddImportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 1574514f5e3Sopenharmony_ci const JSHandle<ImportEntry> &importEntry, size_t idx, uint32_t len); 1584514f5e3Sopenharmony_ci static void AddLocalExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 1594514f5e3Sopenharmony_ci const JSHandle<LocalExportEntry> &exportEntry, size_t idx, uint32_t len); 1604514f5e3Sopenharmony_ci static void AddIndirectExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 1614514f5e3Sopenharmony_ci const JSHandle<IndirectExportEntry> &exportEntry, size_t idx, uint32_t len); 1624514f5e3Sopenharmony_ci static void AddStarExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 1634514f5e3Sopenharmony_ci const JSHandle<StarExportEntry> &exportEntry, size_t idx, uint32_t len); 1644514f5e3Sopenharmony_ci static std::pair<bool, ModuleTypes> CheckNativeModule(const CString &moduleRequestName); 1654514f5e3Sopenharmony_ci static Local<JSValueRef> GetRequireNativeModuleFunc(EcmaVM *vm, ModuleTypes moduleType); 1664514f5e3Sopenharmony_ci static void MakeNormalizedAppArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 1674514f5e3Sopenharmony_ci const CString &soPath, const CString &moduleName); 1684514f5e3Sopenharmony_ci static void MakeAppArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 1694514f5e3Sopenharmony_ci const CString &soPath, const CString &moduleName, const CString &requestName); 1704514f5e3Sopenharmony_ci static void MakeInternalArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 1714514f5e3Sopenharmony_ci const CString &moduleRequestName); 1724514f5e3Sopenharmony_ci static Local<JSValueRef> LoadNativeModuleImpl(EcmaVM *vm, JSThread *thread, 1734514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &requiredModule, ModuleTypes moduleType); 1744514f5e3Sopenharmony_ci static Local<JSValueRef> LoadNativeModuleMayThrowError(JSThread *thread, 1754514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &requiredModule, ModuleTypes moduleType); 1764514f5e3Sopenharmony_ci static bool LoadNativeModule(JSThread *thread, const JSHandle<SourceTextModule> &requiredModule, 1774514f5e3Sopenharmony_ci ModuleTypes moduleType); 1784514f5e3Sopenharmony_ci inline static bool IsNativeModule(ModuleTypes moduleType) 1794514f5e3Sopenharmony_ci { 1804514f5e3Sopenharmony_ci return moduleType == ModuleTypes::OHOS_MODULE || 1814514f5e3Sopenharmony_ci moduleType == ModuleTypes::APP_MODULE || 1824514f5e3Sopenharmony_ci moduleType == ModuleTypes::NATIVE_MODULE || 1834514f5e3Sopenharmony_ci moduleType == ModuleTypes::INTERNAL_MODULE; 1844514f5e3Sopenharmony_ci } 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_ci inline static CString GetResolveErrorReason(const JSHandle<JSTaggedValue> &resolution) 1874514f5e3Sopenharmony_ci { 1884514f5e3Sopenharmony_ci ASSERT(resolution->IsNull() || resolution->IsString()); 1894514f5e3Sopenharmony_ci return resolution->IsNull() ? "' does not provide an export name '" 1904514f5e3Sopenharmony_ci : "' provide an ambiguous export name '"; 1914514f5e3Sopenharmony_ci } 1924514f5e3Sopenharmony_ci 1934514f5e3Sopenharmony_ci inline static bool IsCjsModule(ModuleTypes moduleType) 1944514f5e3Sopenharmony_ci { 1954514f5e3Sopenharmony_ci return moduleType == ModuleTypes::CJS_MODULE; 1964514f5e3Sopenharmony_ci } 1974514f5e3Sopenharmony_ci 1984514f5e3Sopenharmony_ci inline static bool IsJsonModule(ModuleTypes moduleType) 1994514f5e3Sopenharmony_ci { 2004514f5e3Sopenharmony_ci return moduleType == ModuleTypes::JSON_MODULE; 2014514f5e3Sopenharmony_ci } 2024514f5e3Sopenharmony_ci 2034514f5e3Sopenharmony_ci inline static bool IsModuleInSharedHeap(JSHandle<SourceTextModule> currentModule) 2044514f5e3Sopenharmony_ci { 2054514f5e3Sopenharmony_ci return currentModule->GetSharedType() > SharedTypes::UNSENDABLE_MODULE; 2064514f5e3Sopenharmony_ci } 2074514f5e3Sopenharmony_ci 2084514f5e3Sopenharmony_ci inline static bool IsSharedModule(JSHandle<SourceTextModule> currentModule) 2094514f5e3Sopenharmony_ci { 2104514f5e3Sopenharmony_ci return currentModule->GetSharedType() == SharedTypes::SHARED_MODULE; 2114514f5e3Sopenharmony_ci } 2124514f5e3Sopenharmony_ci 2134514f5e3Sopenharmony_ci static bool IsEvaluatedModule(JSThread *thread, StateVisit &stateVisit, 2144514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module); 2154514f5e3Sopenharmony_ci 2164514f5e3Sopenharmony_ci static ModuleStatus GetModuleEvaluatingType(JSThread *thread, StateVisit &stateVisit, 2174514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module); 2184514f5e3Sopenharmony_ci 2194514f5e3Sopenharmony_ci inline static bool IsSendableFunctionModule(JSTaggedValue currentModule) 2204514f5e3Sopenharmony_ci { 2214514f5e3Sopenharmony_ci return SourceTextModule::Cast(currentModule.GetTaggedObject())->GetSharedType() == 2224514f5e3Sopenharmony_ci SharedTypes::SENDABLE_FUNCTION_MODULE; 2234514f5e3Sopenharmony_ci } 2244514f5e3Sopenharmony_ci 2254514f5e3Sopenharmony_ci inline bool *GetLazyImportStatusArray() 2264514f5e3Sopenharmony_ci { 2274514f5e3Sopenharmony_ci return reinterpret_cast<bool *>(GetLazyImportStatus()); 2284514f5e3Sopenharmony_ci } 2294514f5e3Sopenharmony_ci 2304514f5e3Sopenharmony_ci inline void SetLazyImportArray(bool *lazyImportArray) 2314514f5e3Sopenharmony_ci { 2324514f5e3Sopenharmony_ci if (lazyImportArray != nullptr) { 2334514f5e3Sopenharmony_ci DestoryLazyImportArray(); 2344514f5e3Sopenharmony_ci } 2354514f5e3Sopenharmony_ci SetLazyImportStatus(ToUintPtr(lazyImportArray)); 2364514f5e3Sopenharmony_ci } 2374514f5e3Sopenharmony_ci 2384514f5e3Sopenharmony_ci inline void DestoryLazyImportArray() 2394514f5e3Sopenharmony_ci { 2404514f5e3Sopenharmony_ci delete GetLazyImportStatusArray(); 2414514f5e3Sopenharmony_ci SetLazyImportStatus(ToUintPtr(nullptr)); 2424514f5e3Sopenharmony_ci } 2434514f5e3Sopenharmony_ci 2444514f5e3Sopenharmony_ci inline bool IsLazyImportModule(size_t index) 2454514f5e3Sopenharmony_ci { 2464514f5e3Sopenharmony_ci bool *lazyArray = GetLazyImportStatusArray(); 2474514f5e3Sopenharmony_ci if (lazyArray == nullptr) { 2484514f5e3Sopenharmony_ci return false; 2494514f5e3Sopenharmony_ci } 2504514f5e3Sopenharmony_ci return lazyArray[index]; 2514514f5e3Sopenharmony_ci } 2524514f5e3Sopenharmony_ci 2534514f5e3Sopenharmony_ci inline CString GetEcmaModuleFilenameString() const 2544514f5e3Sopenharmony_ci { 2554514f5e3Sopenharmony_ci CString *fileName = reinterpret_cast<CString *>(GetEcmaModuleFilename()); 2564514f5e3Sopenharmony_ci if (fileName == nullptr) { 2574514f5e3Sopenharmony_ci return CString(); 2584514f5e3Sopenharmony_ci } 2594514f5e3Sopenharmony_ci return *fileName; 2604514f5e3Sopenharmony_ci } 2614514f5e3Sopenharmony_ci 2624514f5e3Sopenharmony_ci inline CString GetEcmaModuleRecordNameString() const 2634514f5e3Sopenharmony_ci { 2644514f5e3Sopenharmony_ci CString *recordName = reinterpret_cast<CString *>(GetEcmaModuleRecordName()); 2654514f5e3Sopenharmony_ci if (recordName == nullptr) { 2664514f5e3Sopenharmony_ci return CString(); 2674514f5e3Sopenharmony_ci } 2684514f5e3Sopenharmony_ci return *recordName; 2694514f5e3Sopenharmony_ci } 2704514f5e3Sopenharmony_ci 2714514f5e3Sopenharmony_ci inline void SetEcmaModuleFilenameString(const CString &fileName) 2724514f5e3Sopenharmony_ci { 2734514f5e3Sopenharmony_ci CString *ptr = new CString(fileName); 2744514f5e3Sopenharmony_ci DestoryEcmaModuleFilenameString(); 2754514f5e3Sopenharmony_ci SetEcmaModuleFilename(ToUintPtr(ptr)); 2764514f5e3Sopenharmony_ci } 2774514f5e3Sopenharmony_ci 2784514f5e3Sopenharmony_ci inline void SetEcmaModuleRecordNameString(const CString &recordName) 2794514f5e3Sopenharmony_ci { 2804514f5e3Sopenharmony_ci CString *ptr = new CString(recordName); 2814514f5e3Sopenharmony_ci DestoryEcmaModuleRecordNameString(); 2824514f5e3Sopenharmony_ci SetEcmaModuleRecordName(ToUintPtr(ptr)); 2834514f5e3Sopenharmony_ci } 2844514f5e3Sopenharmony_ci 2854514f5e3Sopenharmony_ci inline void DestoryEcmaModuleFilenameString() 2864514f5e3Sopenharmony_ci { 2874514f5e3Sopenharmony_ci CString *ptr = reinterpret_cast<CString *>(GetEcmaModuleFilename()); 2884514f5e3Sopenharmony_ci delete ptr; 2894514f5e3Sopenharmony_ci SetEcmaModuleFilename(ToUintPtr(nullptr)); 2904514f5e3Sopenharmony_ci } 2914514f5e3Sopenharmony_ci 2924514f5e3Sopenharmony_ci inline void DestoryEcmaModuleRecordNameString() 2934514f5e3Sopenharmony_ci { 2944514f5e3Sopenharmony_ci CString *ptr = reinterpret_cast<CString *>(GetEcmaModuleRecordName()); 2954514f5e3Sopenharmony_ci delete ptr; 2964514f5e3Sopenharmony_ci SetEcmaModuleRecordName(ToUintPtr(nullptr)); 2974514f5e3Sopenharmony_ci } 2984514f5e3Sopenharmony_ci 2994514f5e3Sopenharmony_ci static constexpr size_t SOURCE_TEXT_MODULE_OFFSET = ModuleRecord::SIZE; 3004514f5e3Sopenharmony_ci ACCESSORS(Environment, SOURCE_TEXT_MODULE_OFFSET, NAMESPACE_OFFSET); 3014514f5e3Sopenharmony_ci ACCESSORS(Namespace, NAMESPACE_OFFSET, REQUESTED_MODULES_OFFSET); 3024514f5e3Sopenharmony_ci ACCESSORS(RequestedModules, REQUESTED_MODULES_OFFSET, IMPORT_ENTRIES_OFFSET); 3034514f5e3Sopenharmony_ci ACCESSORS(ImportEntries, IMPORT_ENTRIES_OFFSET, LOCAL_EXPORT_ENTTRIES_OFFSET); 3044514f5e3Sopenharmony_ci ACCESSORS(LocalExportEntries, LOCAL_EXPORT_ENTTRIES_OFFSET, INDIRECT_EXPORT_ENTTRIES_OFFSET); 3054514f5e3Sopenharmony_ci ACCESSORS(IndirectExportEntries, INDIRECT_EXPORT_ENTTRIES_OFFSET, START_EXPORT_ENTTRIES_OFFSET); 3064514f5e3Sopenharmony_ci ACCESSORS(StarExportEntries, START_EXPORT_ENTTRIES_OFFSET, NAME_DICTIONARY_OFFSET); 3074514f5e3Sopenharmony_ci ACCESSORS(NameDictionary, NAME_DICTIONARY_OFFSET, CYCLE_ROOT_OFFSET); 3084514f5e3Sopenharmony_ci ACCESSORS(CycleRoot, CYCLE_ROOT_OFFSET, TOP_LEVEL_CAPABILITY_OFFSET); 3094514f5e3Sopenharmony_ci ACCESSORS(TopLevelCapability, TOP_LEVEL_CAPABILITY_OFFSET, ASYNC_PARENT_MODULES_OFFSET); 3104514f5e3Sopenharmony_ci ACCESSORS(AsyncParentModules, ASYNC_PARENT_MODULES_OFFSET, SENDABLE_ENV_OFFSET); 3114514f5e3Sopenharmony_ci ACCESSORS(SendableEnv, SENDABLE_ENV_OFFSET, EVALUATION_ERROR_OFFSET); 3124514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(EvaluationError, int32_t, EVALUATION_ERROR_OFFSET, DFS_ANCESTOR_INDEX_OFFSET); 3134514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(DFSAncestorIndex, int32_t, DFS_ANCESTOR_INDEX_OFFSET, DFS_INDEX_OFFSET); 3144514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(DFSIndex, int32_t, DFS_INDEX_OFFSET, ASYNC_EVALUATION_OFFSET); 3154514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(AsyncEvaluatingOrdinal, uint32_t, ASYNC_EVALUATION_OFFSET, PENDING_DEPENDENCIES_OFFSET); 3164514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(PendingAsyncDependencies, 3174514f5e3Sopenharmony_ci int32_t, PENDING_DEPENDENCIES_OFFSET, LAYZ_IMPORT_STATUS_OFFSET); 3184514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(LazyImportStatus, uintptr_t, LAYZ_IMPORT_STATUS_OFFSET, ECMA_MODULE_FILENAME); 3194514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(EcmaModuleFilename, uintptr_t, ECMA_MODULE_FILENAME, ECMA_MODULE_RECORDNAME); 3204514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(EcmaModuleRecordName, uintptr_t, ECMA_MODULE_RECORDNAME, BIT_FIELD_OFFSET); 3214514f5e3Sopenharmony_ci ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 3224514f5e3Sopenharmony_ci 3234514f5e3Sopenharmony_ci DEFINE_ALIGN_SIZE(LAST_OFFSET); 3244514f5e3Sopenharmony_ci 3254514f5e3Sopenharmony_ci // define BitField 3264514f5e3Sopenharmony_ci static constexpr size_t STATUS_BITS = 3; 3274514f5e3Sopenharmony_ci static constexpr size_t MODULE_TYPE_BITS = 4; 3284514f5e3Sopenharmony_ci static constexpr size_t IS_NEW_BC_VERSION_BITS = 1; 3294514f5e3Sopenharmony_ci static constexpr size_t HASTLA_BITS = 1; 3304514f5e3Sopenharmony_ci static constexpr size_t LOADING_TYPE_BITS = 3; 3314514f5e3Sopenharmony_ci static constexpr uint16_t REGISTER_COUNTS = 16; 3324514f5e3Sopenharmony_ci static constexpr size_t IS_SHARED_TYPE_BITS = 2; 3334514f5e3Sopenharmony_ci 3344514f5e3Sopenharmony_ci FIRST_BIT_FIELD(BitField, Status, ModuleStatus, STATUS_BITS) 3354514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, Types, ModuleTypes, MODULE_TYPE_BITS, Status) 3364514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, IsNewBcVersion, bool, IS_NEW_BC_VERSION_BITS, Types) 3374514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, HasTLA, bool, HASTLA_BITS, IsNewBcVersion) 3384514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, LoadingTypes, LoadingTypes, LOADING_TYPE_BITS, HasTLA) 3394514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, RegisterCounts, uint16_t, REGISTER_COUNTS, LoadingTypes) 3404514f5e3Sopenharmony_ci NEXT_BIT_FIELD(BitField, SharedType, SharedTypes, IS_SHARED_TYPE_BITS, RegisterCounts) 3414514f5e3Sopenharmony_ci 3424514f5e3Sopenharmony_ci static_assert(static_cast<size_t>(SharedTypes::TOTAL_KINDS) <= (1 << IS_SHARED_TYPE_BITS)); 3434514f5e3Sopenharmony_ci 3444514f5e3Sopenharmony_ci DECL_DUMP() 3454514f5e3Sopenharmony_ci DECL_VISIT_OBJECT(SOURCE_TEXT_MODULE_OFFSET, EVALUATION_ERROR_OFFSET) 3464514f5e3Sopenharmony_ci 3474514f5e3Sopenharmony_ci // 15.2.1.16.5 Evaluate() 3484514f5e3Sopenharmony_ci static JSTaggedValue Evaluate(JSThread *thread, const JSHandle<SourceTextModule> &module, 3494514f5e3Sopenharmony_ci const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 3504514f5e3Sopenharmony_ci 3514514f5e3Sopenharmony_ci // 15.2.1.16.4 Instantiate() 3524514f5e3Sopenharmony_ci static int PUBLIC_API Instantiate(JSThread *thread, 3534514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &moduleHdl, 3544514f5e3Sopenharmony_ci bool executeFromJob = false); 3554514f5e3Sopenharmony_ci 3564514f5e3Sopenharmony_ci static void EvaluateNativeModule(JSThread *thread, JSHandle<SourceTextModule> nativeModule, 3574514f5e3Sopenharmony_ci ModuleTypes moduleType); 3584514f5e3Sopenharmony_ci 3594514f5e3Sopenharmony_ci JSTaggedValue GetModuleValue(JSThread *thread, int32_t index, bool isThrow); 3604514f5e3Sopenharmony_ci void StoreModuleValue(JSThread *thread, int32_t index, const JSHandle<JSTaggedValue> &value); 3614514f5e3Sopenharmony_ci 3624514f5e3Sopenharmony_ci JSTaggedValue GetModuleValue(JSThread *thread, JSTaggedValue key, bool isThrow); 3634514f5e3Sopenharmony_ci void StoreModuleValue(JSThread *thread, const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value); 3644514f5e3Sopenharmony_ci 3654514f5e3Sopenharmony_ci static JSTaggedValue GetValueFromExportObject(JSThread *thread, JSHandle<JSTaggedValue> &exportObject, 3664514f5e3Sopenharmony_ci int32_t index); 3674514f5e3Sopenharmony_ci 3684514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveIndirectExport(JSThread *thread, const JSHandle<JSTaggedValue> &exportEntry, 3694514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName, 3704514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module, 3714514f5e3Sopenharmony_ci CVector<std::pair<JSHandle<SourceTextModule>, 3724514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>>> &resolveVector); 3734514f5e3Sopenharmony_ci static CString GetModuleName(JSTaggedValue currentModule); 3744514f5e3Sopenharmony_ci 3754514f5e3Sopenharmony_ci static bool IsDynamicModule(LoadingTypes types); 3764514f5e3Sopenharmony_ci 3774514f5e3Sopenharmony_ci // taskpool 3784514f5e3Sopenharmony_ci static std::optional<std::set<uint32_t>> GetConcurrentRequestedModules(const JSHandle<Method> &method); 3794514f5e3Sopenharmony_ci static int EvaluateForConcurrent(JSThread *thread, const JSHandle<SourceTextModule> &module, 3804514f5e3Sopenharmony_ci const JSHandle<Method> &method); 3814514f5e3Sopenharmony_ci static int ModuleEvaluation(JSThread *thread, const JSHandle<ModuleRecord> &moduleRecord, 3824514f5e3Sopenharmony_ci int index, const JSHandle<Method> &method); 3834514f5e3Sopenharmony_ci static void CheckCircularImportTool(JSThread *thread, const CString &circularModuleRecordName, 3844514f5e3Sopenharmony_ci CList<CString> &referenceList, bool printOtherCircular = false); 3854514f5e3Sopenharmony_ci 3864514f5e3Sopenharmony_ci static void CheckResolvedBinding(JSThread *thread, const JSHandle<SourceTextModule> &module); 3874514f5e3Sopenharmony_ci static bool IsCircular(const CList<CString> &referenceList, const CString &requiredModuleName); 3884514f5e3Sopenharmony_ci static void PrintCircular(const CList<CString> &referenceList, Level level); 3894514f5e3Sopenharmony_ci static void SearchCircularImport(JSThread *thread, const CString &circularModuleRecordName, 3904514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module, CList<CString> &referenceList, 3914514f5e3Sopenharmony_ci CString &requiredModuleName, bool printOtherCircular); 3924514f5e3Sopenharmony_ci static void CheckResolvedIndexBinding(JSThread *thread, const JSHandle<SourceTextModule> &module); 3934514f5e3Sopenharmony_ci static void SetExportName(JSThread *thread, 3944514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &moduleRequest, const JSHandle<SourceTextModule> &module, 3954514f5e3Sopenharmony_ci CVector<std::string> &exportedNames, JSHandle<TaggedArray> &newExportStarSet); 3964514f5e3Sopenharmony_ciprivate: 3974514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> GetStarResolution(JSThread *thread, const JSHandle<JSTaggedValue> &exportName, 3984514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &moduleRequest, 3994514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module, 4004514f5e3Sopenharmony_ci JSMutableHandle<JSTaggedValue> &starResolution, 4014514f5e3Sopenharmony_ci CVector<std::pair<JSHandle<SourceTextModule>, 4024514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>>> &resolveVector); 4034514f5e3Sopenharmony_ci template <typename T> 4044514f5e3Sopenharmony_ci static void AddExportName(JSThread *thread, const JSTaggedValue &exportEntry, CVector<std::string> &exportedNames); 4054514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveLocalExport(JSThread *thread, const JSHandle<JSTaggedValue> &exportEntry, 4064514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName, 4074514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module); 4084514f5e3Sopenharmony_ci static JSHandle<JSTaggedValue> ResolveElementOfObject(JSThread *thread, 4094514f5e3Sopenharmony_ci const JSHandle<JSHClass> &hclass, 4104514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName, 4114514f5e3Sopenharmony_ci const JSHandle<SourceTextModule> &module); 4124514f5e3Sopenharmony_ci static bool CheckCircularImport(const JSHandle<SourceTextModule> &module, 4134514f5e3Sopenharmony_ci const JSHandle<JSTaggedValue> &exportName, 4144514f5e3Sopenharmony_ci CVector<std::pair<JSHandle<SourceTextModule>, 4154514f5e3Sopenharmony_ci JSHandle<JSTaggedValue>>> &resolveVector); 4164514f5e3Sopenharmony_ci static JSTaggedValue FindByExport(const JSTaggedValue &exportEntriesTv, const JSTaggedValue &key, 4174514f5e3Sopenharmony_ci const JSTaggedValue &dictionary); 4184514f5e3Sopenharmony_ci static void DFSModuleInstantiation(JSHandle<SourceTextModule> &module, 4194514f5e3Sopenharmony_ci CVector<JSHandle<SourceTextModule>> &stack); 4204514f5e3Sopenharmony_ci static std::optional<int> HandleInnerModuleInstantiation(JSThread *thread, 4214514f5e3Sopenharmony_ci JSHandle<SourceTextModule> &module, 4224514f5e3Sopenharmony_ci JSMutableHandle<JSTaggedValue> &required, 4234514f5e3Sopenharmony_ci CVector<JSHandle<SourceTextModule>> &stack, 4244514f5e3Sopenharmony_ci int &index, bool executeFromJob); 4254514f5e3Sopenharmony_ci static int HandleInstantiateException(JSHandle<SourceTextModule> &module, 4264514f5e3Sopenharmony_ci const CVector<JSHandle<SourceTextModule>> &stack, int result); 4274514f5e3Sopenharmony_ci static void HandleEvaluateResult(JSThread *thread, JSHandle<SourceTextModule> &module, 4284514f5e3Sopenharmony_ci JSHandle<PromiseCapability> &capability, 4294514f5e3Sopenharmony_ci const CVector<JSHandle<SourceTextModule>> &stack, int result); 4304514f5e3Sopenharmony_ci static void HandleConcurrentEvaluateResult(JSThread *thread, JSHandle<SourceTextModule> &module, 4314514f5e3Sopenharmony_ci const CVector<JSHandle<SourceTextModule>> &stack, int result); 4324514f5e3Sopenharmony_ci bool IsAsyncEvaluating(); 4334514f5e3Sopenharmony_ci 4344514f5e3Sopenharmony_ci friend class EcmaModuleTest; 4354514f5e3Sopenharmony_ci friend class SharedModuleManager; 4364514f5e3Sopenharmony_ci}; 4374514f5e3Sopenharmony_ci 4384514f5e3Sopenharmony_ciclass ResolvedBinding final : public Record { 4394514f5e3Sopenharmony_cipublic: 4404514f5e3Sopenharmony_ci CAST_CHECK(ResolvedBinding, IsResolvedBinding); 4414514f5e3Sopenharmony_ci 4424514f5e3Sopenharmony_ci static constexpr size_t MODULE_OFFSET = Record::SIZE; 4434514f5e3Sopenharmony_ci ACCESSORS(Module, MODULE_OFFSET, BINDING_NAME_OFFSET); 4444514f5e3Sopenharmony_ci ACCESSORS(BindingName, BINDING_NAME_OFFSET, SIZE); 4454514f5e3Sopenharmony_ci 4464514f5e3Sopenharmony_ci DECL_DUMP() 4474514f5e3Sopenharmony_ci DECL_VISIT_OBJECT(MODULE_OFFSET, SIZE) 4484514f5e3Sopenharmony_ci}; 4494514f5e3Sopenharmony_ciclass ResolvedIndexBinding final : public Record { 4504514f5e3Sopenharmony_cipublic: 4514514f5e3Sopenharmony_ci CAST_CHECK(ResolvedIndexBinding, IsResolvedIndexBinding); 4524514f5e3Sopenharmony_ci 4534514f5e3Sopenharmony_ci static constexpr size_t MODULE_OFFSET = Record::SIZE; 4544514f5e3Sopenharmony_ci ACCESSORS(Module, MODULE_OFFSET, INDEX_OFFSET); 4554514f5e3Sopenharmony_ci ACCESSORS_PRIMITIVE_FIELD(Index, int32_t, INDEX_OFFSET, END_OFFSET); 4564514f5e3Sopenharmony_ci DEFINE_ALIGN_SIZE(END_OFFSET); 4574514f5e3Sopenharmony_ci 4584514f5e3Sopenharmony_ci DECL_DUMP() 4594514f5e3Sopenharmony_ci DECL_VISIT_OBJECT(MODULE_OFFSET, INDEX_OFFSET) 4604514f5e3Sopenharmony_ci}; 4614514f5e3Sopenharmony_ci} // namespace panda::ecmascript 4624514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MODULE_JS_MODULE_SOURCE_TEXT_H 463