Skip to content

Commit 1279ea2

Browse files
committed
Implement the basics of component model
1 parent 595a459 commit 1279ea2

19 files changed

Lines changed: 6509 additions & 1672 deletions

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,13 @@ if (BUILD_TOOLS)
641641
INSTALL
642642
)
643643

644+
# component
645+
wabt_executable(
646+
NAME component
647+
SOURCES src/tools/component.cc
648+
INSTALL
649+
)
650+
644651
if(BUILD_FUZZ_TOOLS)
645652
# wasm2wat-fuzz
646653
wabt_executable(

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: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,40 @@ 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+
ComponentBinarySort sort;
89+
ComponentBinaryCoreSort core_sort;
90+
Index index;
91+
};
92+
93+
struct ComponentExternalInfo {
94+
ComponentBinarySort sort;
95+
ComponentBinaryCoreSort core_sort;
96+
ComponentBinaryExternal external;
97+
Index index;
98+
};
99+
100+
struct ComponentExportInfo {
101+
ComponentBinarySort sort;
102+
ComponentBinaryCoreSort core_sort;
103+
Index index;
104+
};
105+
106+
struct ComponentNamedExportInfo {
107+
std::string_view name;
108+
std::string_view version_suffix;
109+
ComponentBinarySort sort;
110+
ComponentBinaryCoreSort core_sort;
111+
bool has_version_suffix;
112+
Index index;
113+
};
114+
81115
class BinaryReaderDelegate {
82116
public:
83117
struct State {
@@ -558,11 +592,92 @@ class BinaryReaderDelegate {
558592
const State* state = nullptr;
559593
};
560594

595+
class ComponentBinaryReaderDelegate {
596+
public:
597+
virtual ~ComponentBinaryReaderDelegate() {}
598+
599+
virtual bool OnError(const Error&) = 0;
600+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
601+
602+
virtual Result OnCoreModule(const void* data,
603+
size_t size,
604+
const ReadBinaryOptions& options) = 0;
605+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
606+
virtual Result EndComponent() = 0;
607+
608+
virtual Result BeginCoreInstanceSection(uint32_t count) = 0;
609+
virtual Result EndCoreInstanceSection() = 0;
610+
virtual Result OnCoreInstance(uint32_t module_index,
611+
uint32_t argument_count,
612+
ComponentNamedSort* arguments) = 0;
613+
virtual Result OnInlineCoreInstance(uint32_t argument_count,
614+
ComponentNamedSort* arguments) = 0;
615+
616+
virtual Result BeginInstanceSection(uint32_t count) = 0;
617+
virtual Result EndInstanceSection() = 0;
618+
virtual Result OnInstance(uint32_t module_index,
619+
uint32_t argument_count,
620+
ComponentNamedSort* arguments) = 0;
621+
virtual Result OnInlineInstance(uint32_t argument_count,
622+
ComponentNamedExportInfo* arguments) = 0;
623+
624+
virtual Result BeginAliasSection(uint32_t count) = 0;
625+
virtual Result EndAliasSection() = 0;
626+
virtual Result OnAliasExport(ComponentBinarySort sort,
627+
ComponentBinaryCoreSort core_sort,
628+
uint32_t instance_index,
629+
std::string_view export_name) = 0;
630+
virtual Result OnAliasOuter(ComponentBinarySort sort,
631+
ComponentBinaryCoreSort core_sort,
632+
uint32_t counter,
633+
uint32_t index) = 0;
634+
635+
virtual Result BeginTypeSection(uint32_t count) = 0;
636+
virtual Result EndTypeSection() = 0;
637+
virtual Result OnPrimitiveType(ComponentType type) = 0;
638+
virtual Result OnRecordType(uint32_t field_count,
639+
ComponentNamedType* fields) = 0;
640+
virtual Result OnVariantType(uint32_t case_count,
641+
ComponentNamedType* cases) = 0;
642+
virtual Result OnListType(ComponentType type) = 0;
643+
virtual Result OnListFixedType(ComponentType type, uint32_t size) = 0;
644+
virtual Result OnOptionType(ComponentType type) = 0;
645+
virtual Result OnResultType(ComponentType result, ComponentType error) = 0;
646+
virtual Result OnOwnType(Index index) = 0;
647+
virtual Result OnBorrowType(Index index) = 0;
648+
virtual Result OnFuncType(ComponentBinaryType type,
649+
uint32_t param_count,
650+
ComponentNamedType* params,
651+
ComponentType result) = 0;
652+
virtual Result BeginInstanceType(uint32_t count) = 0;
653+
virtual Result EndInstanceType() = 0;
654+
virtual Result BeginComponentType(uint32_t count) = 0;
655+
virtual Result EndComponentType() = 0;
656+
657+
virtual Result BeginImportSection(uint32_t count) = 0;
658+
virtual Result EndImportSection() = 0;
659+
virtual Result BeginExportSection(uint32_t count) = 0;
660+
virtual Result EndExportSection() = 0;
661+
virtual Result OnImport(std::string_view external_name,
662+
std::string_view* version_suffix,
663+
ComponentExternalInfo* external_info) = 0;
664+
virtual Result OnExport(std::string_view external_name,
665+
std::string_view* version_suffix,
666+
ComponentExternalInfo* external_info,
667+
ComponentExportInfo* export_info) = 0;
668+
const BinaryReaderDelegate::State* state = nullptr;
669+
};
670+
561671
Result ReadBinary(const void* data,
562672
size_t size,
563673
BinaryReaderDelegate* reader,
564674
const ReadBinaryOptions& options);
565675

676+
Result ReadBinaryComponent(const void* data,
677+
size_t size,
678+
ComponentBinaryReaderDelegate* component_delegate,
679+
const ReadBinaryOptions& options);
680+
566681
size_t ReadU32Leb128(const uint8_t* ptr,
567682
const uint8_t* end,
568683
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: 83 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,88 @@ 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+
CoreModule = 1,
111+
CoreInstance = 2,
112+
CoreType = 3,
113+
Component = 4,
114+
Instance = 5,
115+
Alias = 6,
116+
Type = 7,
117+
Import = 10,
118+
Export = 11,
119+
};
120+
121+
enum class ComponentBinarySort : uint8_t {
122+
Core = 0x00,
123+
Func = 0x01,
124+
Value = 0x02,
125+
Type = 0x03,
126+
Component = 0x04,
127+
Instance = 0x05,
128+
};
129+
130+
enum class ComponentBinaryCoreSort : uint8_t {
131+
Func = 0x00,
132+
Table = 0x01,
133+
Memory = 0x02,
134+
Global = 0x03,
135+
Type = 0x10,
136+
Module = 0x11,
137+
Instance = 0x12,
138+
NonCore = 0xff,
139+
};
140+
141+
enum class ComponentBinaryInstance : uint8_t {
142+
Reference = 0x00,
143+
Inline = 0x01,
144+
};
145+
146+
enum class ComponentBinaryType : uint8_t {
147+
None = 0,
148+
Some = 1,
149+
150+
ResultSome = 0,
151+
ResultNone = 1,
152+
153+
// Types
154+
Record = 0x72,
155+
Variant = 0x71,
156+
List = 0x70,
157+
Option = 0x6b,
158+
Result = 0x6a,
159+
Own = 0x69,
160+
Borrow = 0x68,
161+
ListFixed = 0x67,
162+
AsyncFunc = 0x43,
163+
Instance = 0x42,
164+
Component = 0x41,
165+
Func = 0x40,
166+
};
167+
168+
enum class ComponentBinaryInterface : uint8_t {
169+
CoreType = 0,
170+
Type = 1,
171+
Alias = 2,
172+
Import = 3,
173+
Export = 4,
174+
};
175+
176+
enum class ComponentBinaryAlias : uint8_t {
177+
Export = 0x00,
178+
CoreExport = 0x01,
179+
Outer = 0x02,
180+
};
181+
182+
enum class ComponentBinaryExternal : uint8_t {
183+
ValueEq = 0x00,
184+
ValueType = 0x01,
185+
TypeEq = 0x00,
186+
TypeSubRes = 0x01,
187+
Unused = 0xff,
188+
};
189+
107190
} // namespace wabt
108191

109192
#endif /* WABT_BINARY_H_ */

0 commit comments

Comments
 (0)