#ifndef V8_OBJECTS_SOURCE_TEXT_MODULE_H_
#define V8_OBJECTS_SOURCE_TEXT_MODULE_H_
#include "src/objects/contexts.h"
#include "src/objects/module.h"
#include "src/objects/promise.h"
#include "src/objects/string.h"
#include "src/zone/zone-containers.h"
#include "torque-generated/bit-fields.h"
#include "src/objects/object-macros.h"
namespace v8 {
namespace internal {
class UnorderedModuleSet;
class StructBodyDescriptor;
#include "torque-generated/src/objects/source-text-module-tq.inc"
class SourceTextModule
: public TorqueGeneratedSourceTextModule<SourceTextModule, Module> {
public:
DECL_VERIFIER(SourceTextModule)
DECL_PRINTER(SourceTextModule)
Tagged<SharedFunctionInfo> GetSharedFunctionInfo() const;
Tagged<Script> GetScript() const;
DECL_BOOLEAN_ACCESSORS(has_toplevel_await)
inline Tagged<SourceTextModuleInfo> info() const;
Tagged<Cell> GetCell(int cell_index);
static Handle<Object> LoadVariable(Isolate* isolate,
DirectHandle<SourceTextModule> module,
int cell_index);
static void StoreVariable(DirectHandle<SourceTextModule> module,
int cell_index, DirectHandle<Object> value);
static int ImportIndex(int cell_index);
static int ExportIndex(int cell_index);
static Maybe<bool> AsyncModuleExecutionFulfilled(
Isolate* isolate, Handle<SourceTextModule> module);
static void AsyncModuleExecutionRejected(
Isolate* isolate, DirectHandle<SourceTextModule> module,
DirectHandle<Object> exception);
static DirectHandle<JSModuleNamespace> GetModuleNamespace(
Isolate* isolate, DirectHandle<SourceTextModule> module,
int module_request);
V8_EXPORT_PRIVATE static MaybeHandle<JSObject> GetImportMeta(
Isolate* isolate, DirectHandle<SourceTextModule> module);
using BodyDescriptor =
SubclassBodyDescriptor<Module::BodyDescriptor,
FixedBodyDescriptor<kCodeOffset, kSize, kSize>>;
static constexpr unsigned kFirstAsyncEvaluationOrdinal = 2;
enum ExecuteAsyncModuleContextSlots {
kModule = Context::MIN_CONTEXT_SLOTS,
kContextLength,
};
V8_EXPORT_PRIVATE
std::pair<DirectHandleVector<SourceTextModule>,
DirectHandleVector<JSMessageObject>>
GetStalledTopLevelAwaitMessages(Isolate* isolate);
private:
friend class Factory;
friend class Module;
struct AsyncEvaluationOrdinalCompare;
using AvailableAncestorsSet =
ZoneSet<Handle<SourceTextModule>, AsyncEvaluationOrdinalCompare>;
inline static void AddAsyncParentModule(
Isolate* isolate, DirectHandle<SourceTextModule> module,
DirectHandle<SourceTextModule> parent);
inline Handle<SourceTextModule> GetCycleRoot(Isolate* isolate) const;
inline Handle<SourceTextModule> GetAsyncParentModule(Isolate* isolate,
int index);
inline int AsyncParentModuleCount();
inline bool HasAsyncEvaluationOrdinal() const;
inline bool HasPendingAsyncDependencies();
inline void IncrementPendingAsyncDependencies();
inline void DecrementPendingAsyncDependencies();
DEFINE_TORQUE_GENERATED_SOURCE_TEXT_MODULE_FLAGS()
static constexpr unsigned kNotAsyncEvaluated = 0;
static constexpr unsigned kAsyncEvaluateDidFinish = 1;
static_assert(kNotAsyncEvaluated < kAsyncEvaluateDidFinish);
static_assert(kAsyncEvaluateDidFinish < kFirstAsyncEvaluationOrdinal);
DECL_PRIMITIVE_ACCESSORS(async_evaluation_ordinal, unsigned)
DECL_ACCESSORS(async_parent_modules, Tagged<ArrayList>)
static void CreateExport(Isolate* isolate,
DirectHandle<SourceTextModule> module,
int cell_index, DirectHandle<FixedArray> names);
static void CreateIndirectExport(
Isolate* isolate, DirectHandle<SourceTextModule> module,
DirectHandle<String> name, DirectHandle<SourceTextModuleInfoEntry> entry);
static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveExport(
Isolate* isolate, Handle<SourceTextModule> module,
DirectHandle<String> module_specifier, Handle<String> export_name,
MessageLocation loc, bool must_resolve, ResolveSet* resolve_set);
static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveImport(
Isolate* isolate, DirectHandle<SourceTextModule> module,
Handle<String> name, int module_request_index, MessageLocation loc,
bool must_resolve, ResolveSet* resolve_set);
static V8_WARN_UNUSED_RESULT MaybeHandle<Cell> ResolveExportUsingStarExports(
Isolate* isolate, DirectHandle<SourceTextModule> module,
DirectHandle<String> module_specifier, Handle<String> export_name,
MessageLocation loc, bool must_resolve, ResolveSet* resolve_set);
static V8_WARN_UNUSED_RESULT bool PrepareInstantiate(
Isolate* isolate, DirectHandle<SourceTextModule> module,
v8::Local<v8::Context> context,
const Module::UserResolveCallbacks& callbacks);
static V8_WARN_UNUSED_RESULT bool FinishInstantiate(
Isolate* isolate, Handle<SourceTextModule> module,
ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index,
Zone* zone);
static V8_WARN_UNUSED_RESULT bool RunInitializationCode(
Isolate* isolate, DirectHandle<SourceTextModule> module);
static void FetchStarExports(Isolate* isolate,
Handle<SourceTextModule> module, Zone* zone,
UnorderedModuleSet* visited);
static void GatherAvailableAncestors(Isolate* isolate, Zone* zone,
Handle<SourceTextModule> start,
AvailableAncestorsSet* exec_list);
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> Evaluate(
Isolate* isolate, Handle<SourceTextModule> module);
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> InnerModuleEvaluation(
Isolate* isolate, Handle<SourceTextModule> module,
ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index);
bool MaybeHandleEvaluationException(
Isolate* isolate, ZoneForwardList<Handle<SourceTextModule>>* stack);
static V8_WARN_UNUSED_RESULT bool MaybeTransitionComponent(
Isolate* isolate, DirectHandle<SourceTextModule> module,
ZoneForwardList<Handle<SourceTextModule>>* stack, Status new_status);
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object>
InnerExecuteAsyncModule(Isolate* isolate,
DirectHandle<SourceTextModule> module,
DirectHandle<JSPromise> capability);
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> ExecuteModule(
Isolate* isolate, DirectHandle<SourceTextModule> module,
MaybeDirectHandle<Object>* exception_out);
static V8_WARN_UNUSED_RESULT Maybe<bool> ExecuteAsyncModule(
Isolate* isolate, DirectHandle<SourceTextModule> module);
static void Reset(Isolate* isolate, DirectHandle<SourceTextModule> module);
V8_EXPORT_PRIVATE void InnerGetStalledTopLevelAwaitModule(
Isolate* isolate, UnorderedModuleSet* visited,
DirectHandleVector<SourceTextModule>* result);
TQ_OBJECT_CONSTRUCTORS(SourceTextModule)
};
class SourceTextModuleInfo : public FixedArray {
public:
template <typename IsolateT>
static DirectHandle<SourceTextModuleInfo> New(
IsolateT* isolate, Zone* zone, SourceTextModuleDescriptor* descr);
inline Tagged<FixedArray> module_requests() const;
inline Tagged<FixedArray> special_exports() const;
inline Tagged<FixedArray> regular_exports() const;
inline Tagged<FixedArray> regular_imports() const;
inline Tagged<FixedArray> namespace_imports() const;
int RegularExportCount() const;
Tagged<String> RegularExportLocalName(int i) const;
int RegularExportCellIndex(int i) const;
Tagged<FixedArray> RegularExportExportNames(int i) const;
inline bool Equals(Tagged<SourceTextModuleInfo> other) const;
private:
template <typename Impl>
friend class FactoryBase;
friend class SourceTextModuleDescriptor;
enum {
kModuleRequestsIndex,
kSpecialExportsIndex,
kRegularExportsIndex,
kNamespaceImportsIndex,
kRegularImportsIndex,
kLength
};
enum {
kRegularExportLocalNameOffset,
kRegularExportCellIndexOffset,
kRegularExportExportNamesOffset,
kRegularExportLength
};
};
class ModuleRequest
: public TorqueGeneratedModuleRequest<ModuleRequest, Struct> {
public:
DECL_VERIFIER(ModuleRequest)
template <typename IsolateT>
static Handle<ModuleRequest> New(IsolateT* isolate,
DirectHandle<String> specifier,
ModuleImportPhase phase,
DirectHandle<FixedArray> import_attributes,
int position);
static const size_t kAttributeEntrySize = 3;
DEFINE_TORQUE_GENERATED_MODULE_REQUEST_FLAGS()
static_assert(PositionBits::kMax >= String::kMaxLength,
"String::kMaxLength should fit in PositionBits::kMax");
DECL_PRIMITIVE_ACCESSORS(position, unsigned)
inline void set_phase(ModuleImportPhase phase);
inline ModuleImportPhase phase() const;
using BodyDescriptor = StructBodyDescriptor;
TQ_OBJECT_CONSTRUCTORS(ModuleRequest)
};
class SourceTextModuleInfoEntry
: public TorqueGeneratedSourceTextModuleInfoEntry<SourceTextModuleInfoEntry,
Struct> {
public:
DECL_VERIFIER(SourceTextModuleInfoEntry)
template <typename IsolateT>
static Handle<SourceTextModuleInfoEntry> New(
IsolateT* isolate, DirectHandle<UnionOf<String, Undefined>> export_name,
DirectHandle<UnionOf<String, Undefined>> local_name,
DirectHandle<UnionOf<String, Undefined>> import_name, int module_request,
int cell_index, int beg_pos, int end_pos);
using BodyDescriptor = StructBodyDescriptor;
TQ_OBJECT_CONSTRUCTORS(SourceTextModuleInfoEntry)
};
}
}
#include "src/objects/object-macros-undef.h"
#endif