Skip to content

Commit 00bc9bf

Browse files
committed
Implement the basics of component model
1 parent b7ef665 commit 00bc9bf

27 files changed

Lines changed: 9650 additions & 1703 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: 125 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,135 @@ 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& component_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 ComponentType& 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_type,
632+
const ComponentTypeLoc& error_type) = 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) = 0;
639+
virtual Result OnFuncParam(ComponentStringLoc name,
640+
ComponentTypeLoc type) = 0;
641+
virtual Result OnFuncResult(const ComponentTypeLoc& type) = 0;
642+
virtual Result OnResourceType(ComponentResourceRep rep,
643+
const ComponentIndexLoc& dtor) = 0;
644+
virtual Result OnResourceAsyncType(ComponentResourceRep rep,
645+
const ComponentIndexLoc& dtor,
646+
const ComponentIndexLoc& callback) = 0;
647+
virtual Result BeginInstanceType(uint32_t count) = 0;
648+
virtual Result EndInstanceType() = 0;
649+
virtual Result BeginComponentType(uint32_t count) = 0;
650+
virtual Result EndComponentType() = 0;
651+
652+
virtual Result BeginCanonSection(uint32_t count) = 0;
653+
virtual Result EndCanonSection() = 0;
654+
virtual Result OnCanonLift(const ComponentIndexLoc& core_func_index,
655+
uint32_t option_count,
656+
ComponentCanonOption* options,
657+
const ComponentIndexLoc& type_index) = 0;
658+
virtual Result OnCanonLower(const ComponentIndexLoc& func_index,
659+
uint32_t option_count,
660+
ComponentCanonOption* options) = 0;
661+
virtual Result OnCanonType(ComponentCanon canon,
662+
const ComponentIndexLoc& type_index) = 0;
663+
664+
virtual Result BeginImportSection(uint32_t count) = 0;
665+
virtual Result EndImportSection() = 0;
666+
virtual Result OnImport(const ComponentStringLoc& name,
667+
std::string_view* version_suffix,
668+
const ComponentExternalInfo& external_info) = 0;
669+
670+
virtual Result BeginExportSection(uint32_t count) = 0;
671+
virtual Result EndExportSection() = 0;
672+
virtual Result OnExport(const ComponentStringLoc& name,
673+
std::string_view* version_suffix,
674+
ComponentExternalInfo* external_info,
675+
ComponentExportInfo* export_info) = 0;
676+
const BinaryReaderDelegate::State* state = nullptr;
677+
};
678+
561679
Result ReadBinary(const void* data,
562680
size_t size,
563681
BinaryReaderDelegate* reader,
564682
const ReadBinaryOptions& options);
565683

684+
Result ReadBinaryComponent(const void* data,
685+
size_t size,
686+
ComponentBinaryReaderDelegate* component_delegate,
687+
const ReadBinaryOptions& options);
688+
689+
bool ReadBinaryIsComponent(const void* data, size_t size);
690+
566691
size_t ReadU32Leb128(const uint8_t* ptr,
567692
const uint8_t* end,
568693
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)