@@ -78,6 +78,41 @@ enum class TableInitExprStatus {
7878 TableWithoutInitExpression,
7979};
8080
81+ struct ComponentNamedType {
82+ std::string_view name;
83+ ComponentType type;
84+ };
85+
86+ struct ComponentNamedSort {
87+ std::string_view name;
88+ ComponentSort sort;
89+ Index index;
90+ };
91+
92+ struct ComponentCanonOption {
93+ ComponentBinaryCanonOption option;
94+ Index index;
95+ };
96+
97+ struct ComponentExternalInfo {
98+ ComponentSort sort;
99+ ComponentBinaryExternal external;
100+ Index index;
101+ };
102+
103+ struct ComponentExportInfo {
104+ ComponentSort sort;
105+ Index index;
106+ };
107+
108+ struct ComponentNamedExportInfo {
109+ std::string_view name;
110+ std::string_view version_suffix;
111+ ComponentSort sort;
112+ bool has_version_suffix;
113+ Index index;
114+ };
115+
81116class BinaryReaderDelegate {
82117 public:
83118 struct State {
@@ -558,11 +593,112 @@ class BinaryReaderDelegate {
558593 const State* state = nullptr ;
559594};
560595
596+ class ComponentBinaryReaderDelegate {
597+ public:
598+ virtual ~ComponentBinaryReaderDelegate () {}
599+
600+ virtual bool OnError (const Error&) = 0;
601+ virtual void OnSetState (const BinaryReaderDelegate::State* s) { state = s; }
602+
603+ virtual Result OnCoreModule (const void * data,
604+ size_t size,
605+ const ReadBinaryOptions& options) = 0;
606+ virtual Result BeginComponent (uint32_t version, size_t depth) = 0;
607+ virtual Result EndComponent () = 0;
608+
609+ virtual Result BeginCoreInstanceSection (uint32_t count) = 0;
610+ virtual Result EndCoreInstanceSection () = 0;
611+ virtual Result OnCoreInstance (uint32_t module_index,
612+ uint32_t argument_count,
613+ ComponentNamedSort* arguments) = 0;
614+ virtual Result OnInlineCoreInstance (uint32_t argument_count,
615+ ComponentNamedSort* arguments) = 0;
616+
617+ virtual Result BeginInstanceSection (uint32_t count) = 0;
618+ virtual Result EndInstanceSection () = 0;
619+ virtual Result OnInstance (uint32_t module_index,
620+ uint32_t argument_count,
621+ ComponentNamedSort* arguments) = 0;
622+ virtual Result OnInlineInstance (uint32_t argument_count,
623+ ComponentNamedExportInfo* arguments) = 0;
624+
625+ virtual Result BeginAliasSection (uint32_t count) = 0;
626+ virtual Result EndAliasSection () = 0;
627+ virtual Result OnAliasExport (ComponentSort sort,
628+ uint32_t instance_index,
629+ std::string_view name) = 0;
630+ virtual Result OnAliasCoreExport (ComponentSort sort,
631+ uint32_t core_instance_index,
632+ std::string_view name) = 0;
633+ virtual Result OnAliasOuter (ComponentSort sort,
634+ uint32_t counter,
635+ uint32_t index) = 0;
636+
637+ virtual Result BeginTypeSection (uint32_t count) = 0;
638+ virtual Result EndTypeSection () = 0;
639+ virtual Result OnPrimitiveType (ComponentType type) = 0;
640+ virtual Result OnRecordType (uint32_t field_count,
641+ ComponentNamedType* fields) = 0;
642+ virtual Result OnVariantType (uint32_t case_count,
643+ ComponentNamedType* cases) = 0;
644+ virtual Result OnListType (ComponentType type) = 0;
645+ virtual Result OnListFixedType (ComponentType type, uint32_t size) = 0;
646+ virtual Result OnTupleType (uint32_t type_count, ComponentType* types) = 0;
647+ virtual Result OnFlagsType (uint32_t flag_count, std::string_view* flags) = 0;
648+ virtual Result OnEnumType (uint32_t enum_count, std::string_view* enums) = 0;
649+ virtual Result OnOptionType (ComponentType type) = 0;
650+ virtual Result OnResultType (ComponentType result, ComponentType error) = 0;
651+ virtual Result OnOwnType (Index index) = 0;
652+ virtual Result OnBorrowType (Index index) = 0;
653+ virtual Result OnStreamType (ComponentType type) = 0;
654+ virtual Result OnFutureType (ComponentType type) = 0;
655+ virtual Result OnFuncType (ComponentBinaryType type,
656+ uint32_t param_count,
657+ ComponentNamedType* params,
658+ ComponentType result) = 0;
659+ virtual Result BeginInstanceType (uint32_t count) = 0;
660+ virtual Result EndInstanceType () = 0;
661+ virtual Result BeginComponentType (uint32_t count) = 0;
662+ virtual Result EndComponentType () = 0;
663+
664+ virtual Result BeginCanonSection (uint32_t count) = 0;
665+ virtual Result EndCanonSection () = 0;
666+ virtual Result OnCanonLift (Index core_func_index,
667+ uint32_t option_count,
668+ ComponentCanonOption* options,
669+ Index type_index) = 0;
670+ virtual Result OnCanonLower (Index func_index,
671+ uint32_t option_count,
672+ ComponentCanonOption* options) = 0;
673+ virtual Result OnCanonType (ComponentBinaryCanon canon, Index type_index) = 0;
674+
675+ virtual Result BeginImportSection (uint32_t count) = 0;
676+ virtual Result EndImportSection () = 0;
677+ virtual Result OnImport (std::string_view external_name,
678+ std::string_view* version_suffix,
679+ ComponentExternalInfo* external_info) = 0;
680+
681+ virtual Result BeginExportSection (uint32_t count) = 0;
682+ virtual Result EndExportSection () = 0;
683+ virtual Result OnExport (std::string_view external_name,
684+ std::string_view* version_suffix,
685+ ComponentExternalInfo* external_info,
686+ ComponentExportInfo* export_info) = 0;
687+ const BinaryReaderDelegate::State* state = nullptr ;
688+ };
689+
561690Result ReadBinary (const void * data,
562691 size_t size,
563692 BinaryReaderDelegate* reader,
564693 const ReadBinaryOptions& options);
565694
695+ Result ReadBinaryComponent (const void * data,
696+ size_t size,
697+ ComponentBinaryReaderDelegate* component_delegate,
698+ const ReadBinaryOptions& options);
699+
700+ bool ReadBinaryIsComponent (const void * data, size_t size);
701+
566702size_t ReadU32Leb128 (const uint8_t * ptr,
567703 const uint8_t * end,
568704 uint32_t * out_value);
0 commit comments