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