Skip to content

Commit 4f8a1ed

Browse files
committed
Implement the basics of component model
1 parent 595a459 commit 4f8a1ed

20 files changed

Lines changed: 7331 additions & 1688 deletions

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: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
81116
class 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+
561690
Result 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+
566702
size_t ReadU32Leb128(const uint8_t* ptr,
567703
const uint8_t* end,
568704
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: 107 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,112 @@ enum class NameSectionSubsection {
104105
};
105106
const char* GetNameSectionSubsectionName(NameSectionSubsection subsec);
106107

108+
enum class ComponentBinarySection : uint8_t {
109+
// Same as ComponentDef::Type in ir.h.
110+
Custom = 0,
111+
CoreModule = 1,
112+
CoreInstance = 2,
113+
CoreType = 3,
114+
Component = 4,
115+
Instance = 5,
116+
Alias = 6,
117+
Type = 7,
118+
Canon = 8,
119+
Import = 10,
120+
Export = 11,
121+
};
122+
123+
enum class ComponentBinarySort : uint8_t {
124+
Core = 0x00,
125+
Func = 0x01,
126+
Value = 0x02,
127+
Type = 0x03,
128+
Component = 0x04,
129+
Instance = 0x05,
130+
};
131+
132+
enum class ComponentBinaryCoreSort : uint8_t {
133+
Func = 0x00,
134+
Table = 0x01,
135+
Memory = 0x02,
136+
Global = 0x03,
137+
Type = 0x10,
138+
Module = 0x11,
139+
Instance = 0x12,
140+
NonCore = 0xff,
141+
};
142+
143+
enum class ComponentBinaryInstance : uint8_t {
144+
Reference = 0x00,
145+
Inline = 0x01,
146+
};
147+
148+
enum class ComponentBinaryAlias : uint8_t {
149+
Export = 0x00,
150+
CoreExport = 0x01,
151+
Outer = 0x02,
152+
};
153+
154+
enum class ComponentBinaryType : uint8_t {
155+
None = 0,
156+
Some = 1,
157+
158+
ResultSome = 0,
159+
ResultNone = 1,
160+
161+
// Types
162+
Record = 0x72,
163+
Variant = 0x71,
164+
List = 0x70,
165+
Tuple = 0x6f,
166+
Flags = 0x6e,
167+
Enum = 0x6d,
168+
Option = 0x6b,
169+
Result = 0x6a,
170+
Own = 0x69,
171+
Borrow = 0x68,
172+
Stream = 0x66,
173+
Future = 0x65,
174+
ListFixed = 0x67,
175+
AsyncFunc = 0x43,
176+
Instance = 0x42,
177+
Component = 0x41,
178+
Func = 0x40,
179+
};
180+
181+
enum class ComponentBinaryInterface : uint8_t {
182+
CoreType = 0,
183+
Type = 1,
184+
Alias = 2,
185+
Import = 3,
186+
Export = 4,
187+
};
188+
189+
enum class ComponentBinaryCanon : uint8_t {
190+
Lift = 0x00,
191+
Lower = 0x01,
192+
ResourceDrop = 0x03,
193+
};
194+
195+
enum class ComponentBinaryCanonOption : uint8_t {
196+
StrEncUtf8 = 0x00,
197+
StrEncUtf16 = 0x01,
198+
StrEncLatin1Utf16 = 0x02,
199+
Memory = 0x03,
200+
Realloc = 0x04,
201+
PostReturn = 0x05,
202+
Async = 0x06,
203+
Callback = 0x07,
204+
};
205+
206+
enum class ComponentBinaryExternal : uint8_t {
207+
ValueEq = 0x00,
208+
ValueType = 0x01,
209+
TypeEq = 0x00,
210+
TypeSubRes = 0x01,
211+
Unused = 0xff,
212+
};
213+
107214
} // namespace wabt
108215

109216
#endif /* WABT_BINARY_H_ */

0 commit comments

Comments
 (0)