Skip to content

Commit 40a107b

Browse files
committed
Implement the basics of component model
1 parent 595a459 commit 40a107b

19 files changed

+5629
-1531
lines changed

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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ enum class TableInitExprStatus {
7878
TableWithoutInitExpression,
7979
};
8080

81+
struct ComponentNamedType {
82+
std::string_view name;
83+
ComponentType type;
84+
};
85+
86+
struct ComponentExternalInfo {
87+
ComponentBinarySort sort;
88+
ComponentBinaryCoreSort core_sort;
89+
ComponentBinaryExternal external;
90+
Index index;
91+
};
92+
93+
struct ComponentExportInfo {
94+
ComponentBinarySort sort;
95+
ComponentBinaryCoreSort core_sort;
96+
Index index;
97+
};
98+
8199
class BinaryReaderDelegate {
82100
public:
83101
struct State {
@@ -558,11 +576,76 @@ class BinaryReaderDelegate {
558576
const State* state = nullptr;
559577
};
560578

579+
class ComponentBinaryReaderDelegate {
580+
public:
581+
virtual ~ComponentBinaryReaderDelegate() {}
582+
583+
virtual bool OnError(const Error&) = 0;
584+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
585+
586+
virtual Result OnCoreModule(const void* data,
587+
size_t size,
588+
const ReadBinaryOptions& options) = 0;
589+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
590+
virtual Result EndComponent() = 0;
591+
592+
virtual Result BeginAliasSection(uint32_t count) = 0;
593+
virtual Result EndAliasSection() = 0;
594+
virtual Result OnAliasExport(ComponentBinarySort sort,
595+
ComponentBinaryCoreSort core_sort,
596+
uint32_t instance_index,
597+
std::string_view export_name) = 0;
598+
virtual Result OnAliasOuter(ComponentBinarySort sort,
599+
ComponentBinaryCoreSort core_sort,
600+
uint32_t counter,
601+
uint32_t index) = 0;
602+
603+
virtual Result BeginTypeSection(uint32_t count) = 0;
604+
virtual Result EndTypeSection() = 0;
605+
virtual Result OnPrimitiveType(ComponentType type) = 0;
606+
virtual Result OnRecordType(uint32_t field_count,
607+
ComponentNamedType* fields) = 0;
608+
virtual Result OnVariantType(uint32_t case_count,
609+
ComponentNamedType* cases) = 0;
610+
virtual Result OnListType(ComponentType type) = 0;
611+
virtual Result OnListFixedType(ComponentType type, uint32_t size) = 0;
612+
virtual Result OnOptionType(ComponentType type) = 0;
613+
virtual Result OnResultType(ComponentType result, ComponentType error) = 0;
614+
virtual Result OnOwnType(Index index) = 0;
615+
virtual Result OnBorrowType(Index index) = 0;
616+
virtual Result OnFuncType(ComponentBinaryType type,
617+
uint32_t param_count,
618+
ComponentNamedType* params,
619+
ComponentType result) = 0;
620+
virtual Result BeginInstanceType(uint32_t count) = 0;
621+
virtual Result EndInstanceType() = 0;
622+
virtual Result BeginComponentType(uint32_t count) = 0;
623+
virtual Result EndComponentType() = 0;
624+
625+
virtual Result BeginImportSection(uint32_t count) = 0;
626+
virtual Result EndImportSection() = 0;
627+
virtual Result BeginExportSection(uint32_t count) = 0;
628+
virtual Result EndExportSection() = 0;
629+
virtual Result OnImport(std::string_view external_name,
630+
std::string_view* version_suffix,
631+
ComponentExternalInfo* external_info) = 0;
632+
virtual Result OnExport(std::string_view external_name,
633+
std::string_view* version_suffix,
634+
ComponentExternalInfo* external_info,
635+
ComponentExportInfo* export_info) = 0;
636+
const BinaryReaderDelegate::State* state = nullptr;
637+
};
638+
561639
Result ReadBinary(const void* data,
562640
size_t size,
563641
BinaryReaderDelegate* reader,
564642
const ReadBinaryOptions& options);
565643

644+
Result ReadBinaryComponent(const void* data,
645+
size_t size,
646+
ComponentBinaryReaderDelegate* component_delegate,
647+
const ReadBinaryOptions& options);
648+
566649
size_t ReadU32Leb128(const uint8_t* ptr,
567650
const uint8_t* end,
568651
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: 78 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,83 @@ 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 ComponentBinaryAlias : uint8_t {
142+
Export = 0x00,
143+
CoreExport = 0x01,
144+
Outer = 0x02,
145+
};
146+
147+
enum class ComponentBinaryType : uint8_t {
148+
None = 0,
149+
Some = 1,
150+
151+
ResultSome = 0,
152+
ResultNone = 1,
153+
154+
// Types
155+
Record = 0x72,
156+
Variant = 0x71,
157+
List = 0x70,
158+
Option = 0x6b,
159+
Result = 0x6a,
160+
Own = 0x69,
161+
Borrow = 0x68,
162+
ListFixed = 0x67,
163+
AsyncFunc = 0x43,
164+
Instance = 0x42,
165+
Component = 0x41,
166+
Func = 0x40,
167+
};
168+
169+
enum class ComponentBinaryInterface : uint8_t {
170+
CoreType = 0,
171+
Type = 1,
172+
Alias = 2,
173+
Import = 3,
174+
Export = 4,
175+
};
176+
177+
enum class ComponentBinaryExternal : uint8_t {
178+
ValueEq = 0x00,
179+
ValueType = 0x01,
180+
TypeEq = 0x00,
181+
TypeSubRes = 0x01,
182+
Unused = 0xff,
183+
};
184+
107185
} // namespace wabt
108186

109187
#endif /* WABT_BINARY_H_ */

0 commit comments

Comments
 (0)