Skip to content

Commit c2710a6

Browse files
committed
Implement the basics of component model
1 parent afe6c80 commit c2710a6

27 files changed

Lines changed: 8433 additions & 1739 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ set(WABT_LIBRARY_CC
300300
src/binding-hash.cc
301301
src/color.cc
302302
src/common.cc
303+
src/component.cc
303304
src/config.cc
304305
src/config.h.in
305306
src/decompiler.cc

include/wabt/binary-reader-ir.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
namespace wabt {
2424

25+
class Component;
2526
struct Module;
2627
struct ReadBinaryOptions;
2728

@@ -32,6 +33,13 @@ Result ReadBinaryIr(const char* filename,
3233
Errors*,
3334
Module* out_module);
3435

36+
Result ReadBinaryComponentIr(const char* filename,
37+
const void* data,
38+
size_t size,
39+
const ReadBinaryOptions& options,
40+
Errors*,
41+
Component* out_component);
42+
3543
} // namespace wabt
3644

3745
#endif /* WABT_BINARY_READER_IR_H_ */

include/wabt/binary-reader.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "wabt/binary.h"
2525
#include "wabt/common.h"
26+
#include "wabt/component.h"
2627
#include "wabt/error.h"
2728
#include "wabt/feature.h"
2829
#include "wabt/opcode.h"
@@ -558,11 +559,132 @@ class BinaryReaderDelegate {
558559
const State* state = nullptr;
559560
};
560561

562+
class ComponentBinaryReaderDelegate {
563+
public:
564+
virtual ~ComponentBinaryReaderDelegate() {}
565+
566+
virtual bool OnError(const Error&) = 0;
567+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
568+
569+
virtual Result OnCoreModule(const void* data,
570+
size_t size,
571+
const ReadBinaryOptions& options) = 0;
572+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
573+
virtual Result EndComponent() = 0;
574+
575+
virtual Result BeginCoreInstanceSection(uint32_t count) = 0;
576+
virtual Result EndCoreInstanceSection() = 0;
577+
virtual Result OnCoreInstance(const ComponentIndexLoc& module_index,
578+
uint32_t argument_count) = 0;
579+
virtual Result OnCoreInstanceArg(const ComponentStringLoc& name,
580+
ComponentSort sort,
581+
const ComponentIndexLoc& index) = 0;
582+
virtual Result OnInlineCoreInstance(uint32_t argument_count) = 0;
583+
virtual Result OnInlineCoreInstanceArg(const ComponentStringLoc& name,
584+
ComponentSort sort,
585+
const ComponentIndexLoc& index) = 0;
586+
587+
virtual Result BeginInstanceSection(uint32_t count) = 0;
588+
virtual Result EndInstanceSection() = 0;
589+
virtual Result OnInstance(const ComponentIndexLoc& module_index,
590+
uint32_t argument_count) = 0;
591+
virtual Result OnInstanceArg(const ComponentStringLoc& name,
592+
ComponentSort sort,
593+
const ComponentIndexLoc& index) = 0;
594+
virtual Result OnInlineInstance(uint32_t argument_count) = 0;
595+
virtual Result OnInlineInstanceArg(const ComponentStringLoc& name,
596+
std::string_view* version_suffix,
597+
ComponentSort sort,
598+
const ComponentIndexLoc& index) = 0;
599+
600+
virtual Result BeginAliasSection(uint32_t count) = 0;
601+
virtual Result EndAliasSection() = 0;
602+
virtual Result OnAliasExport(ComponentSort sort,
603+
const ComponentIndexLoc& instance_index,
604+
const ComponentStringLoc& name) = 0;
605+
virtual Result OnAliasCoreExport(ComponentSort sort,
606+
const ComponentIndexLoc& core_instance_index,
607+
const ComponentStringLoc& name) = 0;
608+
virtual Result OnAliasOuter(ComponentSort sort,
609+
uint32_t counter,
610+
uint32_t index) = 0;
611+
612+
virtual Result BeginTypeSection(uint32_t count) = 0;
613+
virtual Result EndTypeSection() = 0;
614+
virtual Result OnPrimitiveType(const ComponentTypeLoc type) = 0;
615+
virtual Result OnRecordType(uint32_t field_count) = 0;
616+
virtual Result OnRecordField(const ComponentStringLoc& field_name,
617+
const ComponentTypeLoc& field_type) = 0;
618+
virtual Result OnVariantType(uint32_t case_count) = 0;
619+
virtual Result OnVariantCase(const ComponentStringLoc& case_name,
620+
const ComponentTypeLoc& case_type) = 0;
621+
virtual Result OnListType(const ComponentTypeLoc& type) = 0;
622+
virtual Result OnListFixedType(const ComponentTypeLoc& type,
623+
uint32_t size) = 0;
624+
virtual Result OnTupleType(uint32_t item_count) = 0;
625+
virtual Result OnTupleItem(const ComponentTypeLoc& item) = 0;
626+
virtual Result OnFlagsType(uint32_t label_count) = 0;
627+
virtual Result OnFlagsLabel(const ComponentStringLoc& label) = 0;
628+
virtual Result OnEnumType(uint32_t label_count) = 0;
629+
virtual Result OnEnumLabel(const ComponentStringLoc& label) = 0;
630+
virtual Result OnOptionType(const ComponentTypeLoc& type) = 0;
631+
virtual Result OnResultType(const ComponentTypeLoc& result,
632+
const ComponentTypeLoc& error) = 0;
633+
virtual Result OnOwnType(const ComponentIndexLoc& index) = 0;
634+
virtual Result OnBorrowType(const ComponentIndexLoc& index) = 0;
635+
virtual Result OnStreamType(const ComponentTypeLoc& type) = 0;
636+
virtual Result OnFutureType(const ComponentTypeLoc& type) = 0;
637+
virtual Result OnFuncType(ComponentTypeDef type,
638+
uint32_t param_count,
639+
const ComponentNamedType* params,
640+
const ComponentTypeLoc& result) = 0;
641+
virtual Result OnResourceType(const ComponentIndexLoc& dtor) = 0;
642+
virtual Result OnResourceAsyncType(const ComponentIndexLoc& dtor,
643+
const ComponentIndexLoc& callback) = 0;
644+
virtual Result BeginInstanceType(uint32_t count) = 0;
645+
virtual Result EndInstanceType() = 0;
646+
virtual Result BeginComponentType(uint32_t count) = 0;
647+
virtual Result EndComponentType() = 0;
648+
649+
virtual Result BeginCanonSection(uint32_t count) = 0;
650+
virtual Result EndCanonSection() = 0;
651+
virtual Result OnCanonLift(const ComponentIndexLoc& core_func_index,
652+
uint32_t option_count,
653+
ComponentCanonOption* options,
654+
const ComponentIndexLoc& type_index) = 0;
655+
virtual Result OnCanonLower(const ComponentIndexLoc& func_index,
656+
uint32_t option_count,
657+
ComponentCanonOption* options) = 0;
658+
virtual Result OnCanonType(ComponentCanon canon,
659+
const ComponentIndexLoc& type_index) = 0;
660+
661+
virtual Result BeginImportSection(uint32_t count) = 0;
662+
virtual Result EndImportSection() = 0;
663+
virtual Result OnImport(const ComponentStringLoc& external_name,
664+
std::string_view* version_suffix,
665+
ComponentExternalInfo* external_info) = 0;
666+
667+
virtual Result BeginExportSection(uint32_t count) = 0;
668+
virtual Result EndExportSection() = 0;
669+
virtual Result OnExport(const ComponentStringLoc& external_name,
670+
std::string_view* version_suffix,
671+
ComponentExternalInfo* external_info,
672+
ComponentExportInfo* export_info) = 0;
673+
const BinaryReaderDelegate::State* state = nullptr;
674+
};
675+
561676
Result ReadBinary(const void* data,
562677
size_t size,
563678
BinaryReaderDelegate* reader,
564679
const ReadBinaryOptions& options);
565680

681+
Result ReadBinaryComponent(const void* data,
682+
size_t size,
683+
ComponentBinaryReaderDelegate* component_delegate,
684+
const ReadBinaryOptions& options);
685+
686+
bool ReadBinaryIsComponent(const void* data, size_t size);
687+
566688
size_t ReadU32Leb128(const uint8_t* ptr,
567689
const uint8_t* end,
568690
uint32_t* out_value);

include/wabt/binary-writer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace wabt {
2626

27+
class Component;
2728
struct Module;
2829
struct Script;
2930

@@ -45,6 +46,7 @@ struct WriteBinaryOptions {
4546
};
4647

4748
Result WriteBinaryModule(Stream*, const Module*, const WriteBinaryOptions&);
49+
Result WriteBinaryComponent(Stream*, const Component*, const WriteBinaryOptions&);
4850

4951
void WriteType(Stream* stream, Type type, const char* desc = nullptr);
5052

include/wabt/binary.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#define WABT_BINARY_MAGIC 0x6d736100
2323
#define WABT_BINARY_VERSION 1
24+
#define WABT_BINARY_COMPONENT_VERSION 0xd
2425
#define WABT_BINARY_LAYER_MODULE 0
2526
#define WABT_BINARY_LAYER_COMPONENT 1
2627
#define WABT_BINARY_LIMITS_HAS_MAX_FLAG 0x1
@@ -104,6 +105,53 @@ enum class NameSectionSubsection {
104105
};
105106
const char* GetNameSectionSubsectionName(NameSectionSubsection subsec);
106107

108+
enum class ComponentBinarySort : uint8_t {
109+
Core = 0x00,
110+
Func = 0x01,
111+
Value = 0x02,
112+
Type = 0x03,
113+
Component = 0x04,
114+
Instance = 0x05,
115+
};
116+
117+
enum class ComponentBinaryCoreSort : uint8_t {
118+
Func = 0x00,
119+
Table = 0x01,
120+
Memory = 0x02,
121+
Global = 0x03,
122+
Type = 0x10,
123+
Module = 0x11,
124+
Instance = 0x12,
125+
NonCore = 0xff,
126+
};
127+
128+
enum class ComponentBinaryInstance : uint8_t {
129+
Reference = 0x00,
130+
Inline = 0x01,
131+
};
132+
133+
enum class ComponentBinaryAlias : uint8_t {
134+
Export = 0x00,
135+
CoreExport = 0x01,
136+
Outer = 0x02,
137+
};
138+
139+
enum class ComponentBinaryType : uint8_t {
140+
None = 0,
141+
Some = 1,
142+
143+
ResultSome = 0,
144+
ResultNone = 1,
145+
};
146+
147+
enum class ComponentBinaryInterface : uint8_t {
148+
CoreType = 0,
149+
Type = 1,
150+
Alias = 2,
151+
Import = 3,
152+
Export = 4,
153+
};
154+
107155
} // namespace wabt
108156

109157
#endif /* WABT_BINARY_H_ */

0 commit comments

Comments
 (0)