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_SOURCE_TEXT_H 17 #define ECMASCRIPT_MODULE_JS_MODULE_SOURCE_TEXT_H 18 19 #include "ecmascript/base/string_helper.h" 20 #include "ecmascript/mem/c_containers.h" 21 #include "ecmascript/module/js_module_record.h" 22 #include "ecmascript/module/js_module_entry.h" 23 #include "ecmascript/tagged_array.h" 24 #include "ecmascript/sendable_env.h" 25 26 namespace panda::ecmascript { 27 struct StateVisit; 28 enum class ModuleStatus : uint8_t { 29 UNINSTANTIATED = 0x01, 30 INSTANTIATING, 31 INSTANTIATED, 32 EVALUATING, 33 EVALUATING_ASYNC, 34 EVALUATED 35 }; 36 37 enum class ModuleTypes : uint8_t { 38 ECMA_MODULE = 0x01, 39 CJS_MODULE, 40 JSON_MODULE, 41 NATIVE_MODULE, 42 OHOS_MODULE, 43 APP_MODULE, 44 INTERNAL_MODULE, 45 UNKNOWN 46 }; 47 48 enum class LoadingTypes : uint8_t { 49 STABLE_MODULE = 0x01, 50 DYNAMITC_MODULE, 51 OTHERS 52 }; 53 54 enum class SharedTypes : uint8_t { 55 UNSENDABLE_MODULE = 0x01, 56 SENDABLE_FUNCTION_MODULE, 57 SHARED_MODULE, 58 TOTAL_KINDS, 59 }; 60 61 class SourceTextModule final : public ModuleRecord { 62 public: 63 static constexpr int UNDEFINED_INDEX = -1; 64 static constexpr int MODULE_ERROR = 1; 65 static constexpr size_t DEFAULT_DICTIONART_CAPACITY = 2; 66 static constexpr size_t DEFAULT_ARRAY_CAPACITY = 2; 67 static constexpr uint8_t DEREGISTER_MODULE_TAG = 1; 68 static constexpr uint32_t FIRST_ASYNC_EVALUATING_ORDINAL = 2; 69 static constexpr uint32_t NOT_ASYNC_EVALUATED = 0; 70 static constexpr uint32_t ASYNC_EVALUATE_DID_FINISH = 1; 71 static constexpr bool SHARED_MODULE_TAG = true; 72 73 struct AsyncEvaluatingOrdinalCompare { operatorAsyncEvaluatingOrdinalCompare74 bool operator()(const JSHandle<SourceTextModule> &lhs, const JSHandle<SourceTextModule> &rhs) const 75 { 76 return lhs->GetAsyncEvaluatingOrdinal() < rhs->GetAsyncEvaluatingOrdinal(); 77 } 78 }; 79 using AsyncParentCompletionSet = 80 CSet<JSHandle<SourceTextModule>, AsyncEvaluatingOrdinalCompare>; 81 82 CAST_CHECK(SourceTextModule, IsSourceTextModule); 83 84 // 15.2.1.17 Runtime Semantics: HostResolveImportedModule ( referencingModule, specifier ) 85 static JSHandle<JSTaggedValue> HostResolveImportedModule(JSThread *thread, 86 const JSHandle<SourceTextModule> &module, 87 const JSHandle<JSTaggedValue> &moduleRequest, 88 bool executeFromJob = false); 89 static JSHandle<JSTaggedValue> HostResolveImportedModuleWithMerge(JSThread *thread, 90 const JSHandle<SourceTextModule> &module, 91 const JSHandle<JSTaggedValue> &moduleRequest, 92 bool executeFromJob = false); 93 94 static CString ReplaceModuleThroughFeature(JSThread *thread, const CString &requestName); 95 96 // 15.2.1.16.2 GetExportedNames(exportStarSet) 97 static CVector<std::string> GetExportedNames(JSThread *thread, const JSHandle<SourceTextModule> &module, 98 const JSHandle<TaggedArray> &exportStarSet); 99 100 // 15.2.1.16.3 ResolveExport(exportName, resolveVector) 101 static JSHandle<JSTaggedValue> ResolveExport(JSThread *thread, const JSHandle<SourceTextModule> &module, 102 const JSHandle<JSTaggedValue> &exportName, 103 CVector<std::pair<JSHandle<SourceTextModule>, JSHandle<JSTaggedValue>>> &resolveVector); 104 static JSHandle<JSTaggedValue> ResolveExportObject(JSThread *thread, const JSHandle<SourceTextModule> &module, 105 const JSHandle<JSTaggedValue> &exportObject, 106 const JSHandle<JSTaggedValue> &exportName); 107 static JSHandle<JSTaggedValue> ResolveNativeStarExport(JSThread *thread, 108 const JSHandle<SourceTextModule> &nativeModule, 109 const JSHandle<JSTaggedValue> &exportName); 110 static JSHandle<JSTaggedValue> ResolveCjsStarExport(JSThread *thread, 111 const JSHandle<SourceTextModule> &cjsModule, 112 const JSHandle<JSTaggedValue> &exportName); 113 // 15.2.1.16.4.1 InnerModuleInstantiation ( module, stack, index ) 114 static int InnerModuleInstantiation(JSThread *thread, 115 const JSHandle<ModuleRecord> &moduleRecord, CVector<JSHandle<SourceTextModule>> &stack, 116 int index, bool executeFromJob = false); 117 118 // 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup ( module ) 119 static void ModuleDeclarationEnvironmentSetup(JSThread *thread, const JSHandle<SourceTextModule> &module); 120 static void ModuleDeclarationArrayEnvironmentSetup(JSThread *thread, const JSHandle<SourceTextModule> &module); 121 122 // 15.2.1.16.5.1 InnerModuleEvaluation ( module, stack, index ) 123 static int InnerModuleEvaluation(JSThread *thread, const JSHandle<SourceTextModule> &moduleRecord, 124 CVector<JSHandle<SourceTextModule>> &stack, int index, const void *buffer = nullptr, 125 size_t size = 0, bool executeFromJob = false); 126 127 static int InnerModuleEvaluationUnsafe(JSThread *thread, 128 const JSHandle<ModuleRecord> &moduleRecord, CVector<JSHandle<SourceTextModule>> &stack, 129 int index, const void *buffer, size_t size, bool executeFromJob); 130 // 15.2.1.16.5.2 ModuleExecution ( module ) 131 static Expected<JSTaggedValue, bool> ModuleExecution(JSThread *thread, const JSHandle<SourceTextModule> &module, 132 const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 133 134 // 16.2.1.5.3.2 ExecuteAsyncModule ( module ) 135 static void ExecuteAsyncModule(JSThread *thread, const JSHandle<SourceTextModule> &module, 136 const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 137 138 // 16.2.1.5.3.3 GatherAvailableAncestors ( module, execList ) 139 static void GatherAvailableAncestors(JSThread *thread, const JSHandle<SourceTextModule> &module, 140 AsyncParentCompletionSet &execList); 141 142 // 16.2.1.5.3.4 AsyncModuleExecutionFulfilled ( module ) 143 static void AsyncModuleExecutionFulfilled(JSThread *thread, const JSHandle<SourceTextModule> &module); 144 145 // 16.2.1.5.3.5 AsyncModuleExecutionRejected ( module, error ) 146 static void AsyncModuleExecutionRejected(JSThread *thread, const JSHandle<SourceTextModule> &module, 147 JSTaggedValue error); 148 149 static JSTaggedValue AsyncModuleFulfilledFunc(EcmaRuntimeCallInfo *argv); 150 static JSTaggedValue AsyncModuleRejectedFunc(EcmaRuntimeCallInfo *argv); 151 static void AddAsyncParentModule(JSThread *thread, JSHandle<SourceTextModule> &module, 152 JSHandle<SourceTextModule> &parent); 153 // 15.2.1.18 Runtime Semantics: GetModuleNamespace ( module ) 154 static JSHandle<JSTaggedValue> GetModuleNamespace(JSThread *thread, const JSHandle<SourceTextModule> &module); 155 156 static void AddImportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 157 const JSHandle<ImportEntry> &importEntry, size_t idx, uint32_t len); 158 static void AddLocalExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 159 const JSHandle<LocalExportEntry> &exportEntry, size_t idx, uint32_t len); 160 static void AddIndirectExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 161 const JSHandle<IndirectExportEntry> &exportEntry, size_t idx, uint32_t len); 162 static void AddStarExportEntry(JSThread *thread, const JSHandle<SourceTextModule> &module, 163 const JSHandle<StarExportEntry> &exportEntry, size_t idx, uint32_t len); 164 static std::pair<bool, ModuleTypes> CheckNativeModule(const CString &moduleRequestName); 165 static Local<JSValueRef> GetRequireNativeModuleFunc(EcmaVM *vm, ModuleTypes moduleType); 166 static void MakeNormalizedAppArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 167 const CString &soPath, const CString &moduleName); 168 static void MakeAppArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 169 const CString &soPath, const CString &moduleName, const CString &requestName); 170 static void MakeInternalArgs(const EcmaVM *vm, std::vector<Local<JSValueRef>> &arguments, 171 const CString &moduleRequestName); 172 static Local<JSValueRef> LoadNativeModuleImpl(EcmaVM *vm, JSThread *thread, 173 const JSHandle<SourceTextModule> &requiredModule, ModuleTypes moduleType); 174 static Local<JSValueRef> LoadNativeModuleMayThrowError(JSThread *thread, 175 const JSHandle<SourceTextModule> &requiredModule, ModuleTypes moduleType); 176 static bool LoadNativeModule(JSThread *thread, const JSHandle<SourceTextModule> &requiredModule, 177 ModuleTypes moduleType); IsNativeModule(ModuleTypes moduleType)178 inline static bool IsNativeModule(ModuleTypes moduleType) 179 { 180 return moduleType == ModuleTypes::OHOS_MODULE || 181 moduleType == ModuleTypes::APP_MODULE || 182 moduleType == ModuleTypes::NATIVE_MODULE || 183 moduleType == ModuleTypes::INTERNAL_MODULE; 184 } 185 GetResolveErrorReason(const JSHandle<JSTaggedValue> & resolution)186 inline static CString GetResolveErrorReason(const JSHandle<JSTaggedValue> &resolution) 187 { 188 ASSERT(resolution->IsNull() || resolution->IsString()); 189 return resolution->IsNull() ? "' does not provide an export name '" 190 : "' provide an ambiguous export name '"; 191 } 192 IsCjsModule(ModuleTypes moduleType)193 inline static bool IsCjsModule(ModuleTypes moduleType) 194 { 195 return moduleType == ModuleTypes::CJS_MODULE; 196 } 197 IsJsonModule(ModuleTypes moduleType)198 inline static bool IsJsonModule(ModuleTypes moduleType) 199 { 200 return moduleType == ModuleTypes::JSON_MODULE; 201 } 202 IsModuleInSharedHeap(JSHandle<SourceTextModule> currentModule)203 inline static bool IsModuleInSharedHeap(JSHandle<SourceTextModule> currentModule) 204 { 205 return currentModule->GetSharedType() > SharedTypes::UNSENDABLE_MODULE; 206 } 207 IsSharedModule(JSHandle<SourceTextModule> currentModule)208 inline static bool IsSharedModule(JSHandle<SourceTextModule> currentModule) 209 { 210 return currentModule->GetSharedType() == SharedTypes::SHARED_MODULE; 211 } 212 213 static bool IsEvaluatedModule(JSThread *thread, StateVisit &stateVisit, 214 const JSHandle<SourceTextModule> &module); 215 216 static ModuleStatus GetModuleEvaluatingType(JSThread *thread, StateVisit &stateVisit, 217 const JSHandle<SourceTextModule> &module); 218 IsSendableFunctionModule(JSTaggedValue currentModule)219 inline static bool IsSendableFunctionModule(JSTaggedValue currentModule) 220 { 221 return SourceTextModule::Cast(currentModule.GetTaggedObject())->GetSharedType() == 222 SharedTypes::SENDABLE_FUNCTION_MODULE; 223 } 224 GetLazyImportStatusArray()225 inline bool *GetLazyImportStatusArray() 226 { 227 return reinterpret_cast<bool *>(GetLazyImportStatus()); 228 } 229 SetLazyImportArray(bool * lazyImportArray)230 inline void SetLazyImportArray(bool *lazyImportArray) 231 { 232 if (lazyImportArray != nullptr) { 233 DestoryLazyImportArray(); 234 } 235 SetLazyImportStatus(ToUintPtr(lazyImportArray)); 236 } 237 DestoryLazyImportArray()238 inline void DestoryLazyImportArray() 239 { 240 delete GetLazyImportStatusArray(); 241 SetLazyImportStatus(ToUintPtr(nullptr)); 242 } 243 IsLazyImportModule(size_t index)244 inline bool IsLazyImportModule(size_t index) 245 { 246 bool *lazyArray = GetLazyImportStatusArray(); 247 if (lazyArray == nullptr) { 248 return false; 249 } 250 return lazyArray[index]; 251 } 252 GetEcmaModuleFilenameString()253 inline CString GetEcmaModuleFilenameString() const 254 { 255 CString *fileName = reinterpret_cast<CString *>(GetEcmaModuleFilename()); 256 if (fileName == nullptr) { 257 return CString(); 258 } 259 return *fileName; 260 } 261 GetEcmaModuleRecordNameString()262 inline CString GetEcmaModuleRecordNameString() const 263 { 264 CString *recordName = reinterpret_cast<CString *>(GetEcmaModuleRecordName()); 265 if (recordName == nullptr) { 266 return CString(); 267 } 268 return *recordName; 269 } 270 SetEcmaModuleFilenameString(const CString & fileName)271 inline void SetEcmaModuleFilenameString(const CString &fileName) 272 { 273 CString *ptr = new CString(fileName); 274 DestoryEcmaModuleFilenameString(); 275 SetEcmaModuleFilename(ToUintPtr(ptr)); 276 } 277 SetEcmaModuleRecordNameString(const CString & recordName)278 inline void SetEcmaModuleRecordNameString(const CString &recordName) 279 { 280 CString *ptr = new CString(recordName); 281 DestoryEcmaModuleRecordNameString(); 282 SetEcmaModuleRecordName(ToUintPtr(ptr)); 283 } 284 DestoryEcmaModuleFilenameString()285 inline void DestoryEcmaModuleFilenameString() 286 { 287 CString *ptr = reinterpret_cast<CString *>(GetEcmaModuleFilename()); 288 delete ptr; 289 SetEcmaModuleFilename(ToUintPtr(nullptr)); 290 } 291 DestoryEcmaModuleRecordNameString()292 inline void DestoryEcmaModuleRecordNameString() 293 { 294 CString *ptr = reinterpret_cast<CString *>(GetEcmaModuleRecordName()); 295 delete ptr; 296 SetEcmaModuleRecordName(ToUintPtr(nullptr)); 297 } 298 299 static constexpr size_t SOURCE_TEXT_MODULE_OFFSET = ModuleRecord::SIZE; 300 ACCESSORS(Environment, SOURCE_TEXT_MODULE_OFFSET, NAMESPACE_OFFSET); 301 ACCESSORS(Namespace, NAMESPACE_OFFSET, REQUESTED_MODULES_OFFSET); 302 ACCESSORS(RequestedModules, REQUESTED_MODULES_OFFSET, IMPORT_ENTRIES_OFFSET); 303 ACCESSORS(ImportEntries, IMPORT_ENTRIES_OFFSET, LOCAL_EXPORT_ENTTRIES_OFFSET); 304 ACCESSORS(LocalExportEntries, LOCAL_EXPORT_ENTTRIES_OFFSET, INDIRECT_EXPORT_ENTTRIES_OFFSET); 305 ACCESSORS(IndirectExportEntries, INDIRECT_EXPORT_ENTTRIES_OFFSET, START_EXPORT_ENTTRIES_OFFSET); 306 ACCESSORS(StarExportEntries, START_EXPORT_ENTTRIES_OFFSET, NAME_DICTIONARY_OFFSET); 307 ACCESSORS(NameDictionary, NAME_DICTIONARY_OFFSET, CYCLE_ROOT_OFFSET); 308 ACCESSORS(CycleRoot, CYCLE_ROOT_OFFSET, TOP_LEVEL_CAPABILITY_OFFSET); 309 ACCESSORS(TopLevelCapability, TOP_LEVEL_CAPABILITY_OFFSET, ASYNC_PARENT_MODULES_OFFSET); 310 ACCESSORS(AsyncParentModules, ASYNC_PARENT_MODULES_OFFSET, SENDABLE_ENV_OFFSET); 311 ACCESSORS(SendableEnv, SENDABLE_ENV_OFFSET, EVALUATION_ERROR_OFFSET); 312 ACCESSORS_PRIMITIVE_FIELD(EvaluationError, int32_t, EVALUATION_ERROR_OFFSET, DFS_ANCESTOR_INDEX_OFFSET); 313 ACCESSORS_PRIMITIVE_FIELD(DFSAncestorIndex, int32_t, DFS_ANCESTOR_INDEX_OFFSET, DFS_INDEX_OFFSET); 314 ACCESSORS_PRIMITIVE_FIELD(DFSIndex, int32_t, DFS_INDEX_OFFSET, ASYNC_EVALUATION_OFFSET); 315 ACCESSORS_PRIMITIVE_FIELD(AsyncEvaluatingOrdinal, uint32_t, ASYNC_EVALUATION_OFFSET, PENDING_DEPENDENCIES_OFFSET); 316 ACCESSORS_PRIMITIVE_FIELD(PendingAsyncDependencies, 317 int32_t, PENDING_DEPENDENCIES_OFFSET, LAYZ_IMPORT_STATUS_OFFSET); 318 ACCESSORS_PRIMITIVE_FIELD(LazyImportStatus, uintptr_t, LAYZ_IMPORT_STATUS_OFFSET, ECMA_MODULE_FILENAME); 319 ACCESSORS_PRIMITIVE_FIELD(EcmaModuleFilename, uintptr_t, ECMA_MODULE_FILENAME, ECMA_MODULE_RECORDNAME); 320 ACCESSORS_PRIMITIVE_FIELD(EcmaModuleRecordName, uintptr_t, ECMA_MODULE_RECORDNAME, BIT_FIELD_OFFSET); 321 ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET) 322 323 DEFINE_ALIGN_SIZE(LAST_OFFSET); 324 325 // define BitField 326 static constexpr size_t STATUS_BITS = 3; 327 static constexpr size_t MODULE_TYPE_BITS = 4; 328 static constexpr size_t IS_NEW_BC_VERSION_BITS = 1; 329 static constexpr size_t HASTLA_BITS = 1; 330 static constexpr size_t LOADING_TYPE_BITS = 3; 331 static constexpr uint16_t REGISTER_COUNTS = 16; 332 static constexpr size_t IS_SHARED_TYPE_BITS = 2; 333 334 FIRST_BIT_FIELD(BitField, Status, ModuleStatus, STATUS_BITS) 335 NEXT_BIT_FIELD(BitField, Types, ModuleTypes, MODULE_TYPE_BITS, Status) 336 NEXT_BIT_FIELD(BitField, IsNewBcVersion, bool, IS_NEW_BC_VERSION_BITS, Types) 337 NEXT_BIT_FIELD(BitField, HasTLA, bool, HASTLA_BITS, IsNewBcVersion) 338 NEXT_BIT_FIELD(BitField, LoadingTypes, LoadingTypes, LOADING_TYPE_BITS, HasTLA) 339 NEXT_BIT_FIELD(BitField, RegisterCounts, uint16_t, REGISTER_COUNTS, LoadingTypes) 340 NEXT_BIT_FIELD(BitField, SharedType, SharedTypes, IS_SHARED_TYPE_BITS, RegisterCounts) 341 342 static_assert(static_cast<size_t>(SharedTypes::TOTAL_KINDS) <= (1 << IS_SHARED_TYPE_BITS)); 343 344 DECL_DUMP() 345 DECL_VISIT_OBJECT(SOURCE_TEXT_MODULE_OFFSET, EVALUATION_ERROR_OFFSET) 346 347 // 15.2.1.16.5 Evaluate() 348 static JSTaggedValue Evaluate(JSThread *thread, const JSHandle<SourceTextModule> &module, 349 const void *buffer = nullptr, size_t size = 0, bool executeFromJob = false); 350 351 // 15.2.1.16.4 Instantiate() 352 static int PUBLIC_API Instantiate(JSThread *thread, 353 const JSHandle<JSTaggedValue> &moduleHdl, 354 bool executeFromJob = false); 355 356 static void EvaluateNativeModule(JSThread *thread, JSHandle<SourceTextModule> nativeModule, 357 ModuleTypes moduleType); 358 359 JSTaggedValue GetModuleValue(JSThread *thread, int32_t index, bool isThrow); 360 void StoreModuleValue(JSThread *thread, int32_t index, const JSHandle<JSTaggedValue> &value); 361 362 JSTaggedValue GetModuleValue(JSThread *thread, JSTaggedValue key, bool isThrow); 363 void StoreModuleValue(JSThread *thread, const JSHandle<JSTaggedValue> &key, const JSHandle<JSTaggedValue> &value); 364 365 static JSTaggedValue GetValueFromExportObject(JSThread *thread, JSHandle<JSTaggedValue> &exportObject, 366 int32_t index); 367 368 static JSHandle<JSTaggedValue> ResolveIndirectExport(JSThread *thread, const JSHandle<JSTaggedValue> &exportEntry, 369 const JSHandle<JSTaggedValue> &exportName, 370 const JSHandle<SourceTextModule> &module, 371 CVector<std::pair<JSHandle<SourceTextModule>, 372 JSHandle<JSTaggedValue>>> &resolveVector); 373 static CString GetModuleName(JSTaggedValue currentModule); 374 375 static bool IsDynamicModule(LoadingTypes types); 376 377 // taskpool 378 static std::optional<std::set<uint32_t>> GetConcurrentRequestedModules(const JSHandle<Method> &method); 379 static int EvaluateForConcurrent(JSThread *thread, const JSHandle<SourceTextModule> &module, 380 const JSHandle<Method> &method); 381 static int ModuleEvaluation(JSThread *thread, const JSHandle<ModuleRecord> &moduleRecord, 382 int index, const JSHandle<Method> &method); 383 static void CheckCircularImportTool(JSThread *thread, const CString &circularModuleRecordName, 384 CList<CString> &referenceList, bool printOtherCircular = false); 385 static std::tuple<bool, JSHandle<SourceTextModule>> GetResolvedModule(JSThread *thread, 386 const JSHandle<SourceTextModule> &module, const JSHandle<JSTaggedValue> &moduleRequest); 387 static std::tuple<bool, JSHandle<SourceTextModule>> GetResolvedModuleWithMerge(JSThread *thread, 388 const JSHandle<SourceTextModule> &module, const JSHandle<JSTaggedValue> &moduleRequest); 389 390 private: 391 static void SetExportName(JSThread *thread, 392 const JSHandle<JSTaggedValue> &moduleRequest, const JSHandle<SourceTextModule> &module, 393 CVector<std::string> &exportedNames, JSHandle<TaggedArray> &newExportStarSet); 394 static JSHandle<JSTaggedValue> GetStarResolution(JSThread *thread, const JSHandle<JSTaggedValue> &exportName, 395 const JSHandle<JSTaggedValue> &moduleRequest, 396 const JSHandle<SourceTextModule> &module, 397 JSMutableHandle<JSTaggedValue> &starResolution, 398 CVector<std::pair<JSHandle<SourceTextModule>, 399 JSHandle<JSTaggedValue>>> &resolveVector); 400 template <typename T> 401 static void AddExportName(JSThread *thread, const JSTaggedValue &exportEntry, CVector<std::string> &exportedNames); 402 static JSHandle<JSTaggedValue> ResolveLocalExport(JSThread *thread, const JSHandle<JSTaggedValue> &exportEntry, 403 const JSHandle<JSTaggedValue> &exportName, 404 const JSHandle<SourceTextModule> &module); 405 static JSHandle<JSTaggedValue> ResolveElementOfObject(JSThread *thread, 406 const JSHandle<JSHClass> &hclass, 407 const JSHandle<JSTaggedValue> &exportName, 408 const JSHandle<SourceTextModule> &module); 409 static bool CheckCircularImport(const JSHandle<SourceTextModule> &module, 410 const JSHandle<JSTaggedValue> &exportName, 411 CVector<std::pair<JSHandle<SourceTextModule>, 412 JSHandle<JSTaggedValue>>> &resolveVector); 413 static void InitializeEnvironment(JSThread *thread, const JSHandle<SourceTextModule> ¤tModule, 414 CString &moduleName, JSHandle<JSTaggedValue> &exports, bool isBundle); 415 416 static void CheckResolvedBinding(JSThread *thread, const JSHandle<SourceTextModule> &module); 417 static void CheckResolvedIndexBinding(JSThread *thread, const JSHandle<SourceTextModule> &module); 418 static JSTaggedValue FindByExport(const JSTaggedValue &exportEntriesTv, const JSTaggedValue &key, 419 const JSTaggedValue &dictionary); 420 static JSHandle<SourceTextModule> GetModuleFromBinding(JSThread *thread, const JSTaggedValue &JSTaggedValue); 421 static void DFSModuleInstantiation(JSHandle<SourceTextModule> &module, 422 CVector<JSHandle<SourceTextModule>> &stack); 423 static std::optional<int> HandleInnerModuleInstantiation(JSThread *thread, 424 JSHandle<SourceTextModule> &module, 425 JSMutableHandle<JSTaggedValue> &required, 426 CVector<JSHandle<SourceTextModule>> &stack, 427 int &index, bool executeFromJob); 428 static int HandleInstantiateException(JSHandle<SourceTextModule> &module, 429 const CVector<JSHandle<SourceTextModule>> &stack, int result); 430 static void HandleEvaluateResult(JSThread *thread, JSHandle<SourceTextModule> &module, 431 JSHandle<PromiseCapability> &capability, 432 const CVector<JSHandle<SourceTextModule>> &stack, int result); 433 static void HandleConcurrentEvaluateResult(JSThread *thread, JSHandle<SourceTextModule> &module, 434 const CVector<JSHandle<SourceTextModule>> &stack, int result); 435 bool IsAsyncEvaluating(); 436 static void SearchCircularImport(JSThread *thread, const CString &circularModuleRecordName, 437 const JSHandle<SourceTextModule> &module, CList<CString> &referenceList, 438 CString &requiredModuleName, bool printOtherCircular); 439 static bool IsCircular(const CList<CString> &referenceList, const CString &requiredModuleName); 440 static void PrintCircular(const CList<CString> &referenceList, Level level); 441 442 friend class SharedModuleManager; 443 }; 444 445 class ResolvedBinding final : public Record { 446 public: 447 CAST_CHECK(ResolvedBinding, IsResolvedBinding); 448 449 static constexpr size_t MODULE_OFFSET = Record::SIZE; 450 ACCESSORS(Module, MODULE_OFFSET, BINDING_NAME_OFFSET); 451 ACCESSORS(BindingName, BINDING_NAME_OFFSET, SIZE); 452 453 DECL_DUMP() 454 DECL_VISIT_OBJECT(MODULE_OFFSET, SIZE) 455 }; 456 class ResolvedIndexBinding final : public Record { 457 public: 458 CAST_CHECK(ResolvedIndexBinding, IsResolvedIndexBinding); 459 460 static constexpr size_t MODULE_OFFSET = Record::SIZE; 461 ACCESSORS(Module, MODULE_OFFSET, INDEX_OFFSET); 462 ACCESSORS_PRIMITIVE_FIELD(Index, int32_t, INDEX_OFFSET, END_OFFSET); 463 DEFINE_ALIGN_SIZE(END_OFFSET); 464 465 DECL_DUMP() 466 DECL_VISIT_OBJECT(MODULE_OFFSET, INDEX_OFFSET) 467 }; 468 } // namespace panda::ecmascript 469 #endif // ECMASCRIPT_MODULE_JS_MODULE_SOURCE_TEXT_H 470