Skip to content

Commit d04709a

Browse files
committed
Implement the basics of component model
1 parent 692da15 commit d04709a

17 files changed

+2170
-1468
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+
struct 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,32 @@ class BinaryReaderDelegate {
555555
const State* state = nullptr;
556556
};
557557

558+
class ComponentBinaryReaderDelegate {
559+
public:
560+
virtual ~ComponentBinaryReaderDelegate() {}
561+
562+
virtual bool OnError(const Error&) = 0;
563+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
564+
565+
virtual Result OnCoreModule(const void* data,
566+
size_t size,
567+
const ReadBinaryOptions& options) = 0;
568+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
569+
virtual Result EndComponent() = 0;
570+
571+
const BinaryReaderDelegate::State* state = nullptr;
572+
};
573+
558574
Result ReadBinary(const void* data,
559575
size_t size,
560576
BinaryReaderDelegate* reader,
561577
const ReadBinaryOptions& options);
562578

579+
Result ReadBinaryComponent(const void* data,
580+
size_t size,
581+
ComponentBinaryReaderDelegate* component_delegate,
582+
const ReadBinaryOptions& options);
583+
563584
size_t ReadU32Leb128(const uint8_t* ptr,
564585
const uint8_t* end,
565586
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+
struct 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: 10 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,15 @@ enum class NameSectionSubsection {
104105
};
105106
const char* GetNameSectionSubsectionName(NameSectionSubsection subsec);
106107

108+
enum class ComponentBinarySection : uint8_t {
109+
// Same as ComponentType in ir.h
110+
CoreModule = 1,
111+
CoreInstance = 2,
112+
CoreType = 3,
113+
Component = 4,
114+
Instance = 5,
115+
};
116+
107117
} // namespace wabt
108118

109119
#endif /* WABT_BINARY_H_ */

include/wabt/ir.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,78 @@ struct Script {
17021702
BindingHash module_bindings;
17031703
};
17041704

1705+
struct Component;
1706+
class ComponentCoreModule;
1707+
class ComponentComponent;
1708+
1709+
class ComponentDefinition {
1710+
public:
1711+
enum class Type {
1712+
// Same as ComponentBinarySection in binary.h
1713+
CoreModule = 1,
1714+
CoreInstance = 2,
1715+
CoreType = 3,
1716+
Component = 4,
1717+
Instance = 5,
1718+
};
1719+
1720+
virtual ~ComponentDefinition() {}
1721+
1722+
Type type() const {
1723+
return type_;
1724+
}
1725+
1726+
ComponentCoreModule* AsCoreModule() {
1727+
assert(type() == Type::CoreModule);
1728+
return reinterpret_cast<ComponentCoreModule*>(this);
1729+
}
1730+
1731+
ComponentComponent* AsComponent() {
1732+
assert(type() == Type::Component);
1733+
return reinterpret_cast<ComponentComponent*>(this);
1734+
}
1735+
1736+
protected:
1737+
ComponentDefinition(Type type)
1738+
: type_(type) {}
1739+
1740+
private:
1741+
Type type_;
1742+
};
1743+
1744+
class ComponentCoreModule : public ComponentDefinition {
1745+
public:
1746+
ComponentCoreModule()
1747+
: ComponentDefinition(Type::CoreModule) {}
1748+
1749+
Module* module() {
1750+
return &module_;
1751+
}
1752+
1753+
private:
1754+
Module module_;
1755+
};
1756+
1757+
struct Component {
1758+
Component* parent = nullptr;
1759+
std::vector<std::unique_ptr<ComponentDefinition>> definitions;
1760+
};
1761+
1762+
class ComponentComponent : public ComponentDefinition {
1763+
public:
1764+
ComponentComponent(Component* parent)
1765+
: ComponentDefinition(Type::Component) {
1766+
component_.parent = parent;
1767+
}
1768+
1769+
Component* component() {
1770+
return &component_;
1771+
}
1772+
1773+
private:
1774+
Component component_;
1775+
};
1776+
17051777
void MakeTypeBindingReverseMapping(
17061778
size_t num_types,
17071779
const BindingHash& bindings,

include/wabt/token.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ WABT_TOKEN(AssertUnlinkable, "assert_unlinkable")
3131
WABT_TOKEN(Before, "before")
3232
WABT_TOKEN(Bin, "bin")
3333
WABT_TOKEN(Item, "item")
34+
WABT_TOKEN(Component, "component")
35+
WABT_TOKEN(Core, "core")
3436
WABT_TOKEN(Data, "data")
3537
WABT_TOKEN(Declare, "declare")
3638
WABT_TOKEN(Delegate, "delegate")
@@ -48,6 +50,7 @@ WABT_TOKEN(Global, "global")
4850
WABT_TOKEN(Import, "import")
4951
WABT_TOKEN(Invoke, "invoke")
5052
WABT_TOKEN(Input, "input")
53+
WABT_TOKEN(Instance, "instance")
5154
WABT_TOKEN(Local, "local")
5255
WABT_TOKEN(Lpar, "(")
5356
WABT_TOKEN(Memory, "memory")

include/wabt/wast-parser.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class WastParser {
4747
void WABT_PRINTF_FORMAT(3, 4) Error(Location, const char* format, ...);
4848
Result ParseModule(std::unique_ptr<Module>* out_module);
4949
Result ParseScript(std::unique_ptr<Script>* out_script);
50+
Result ParseComponent(std::unique_ptr<Component>* out_component);
5051

5152
std::unique_ptr<Script> ReleaseScript();
5253

@@ -281,6 +282,7 @@ class WastParser {
281282

282283
Result ParseCommandList(Script*, CommandPtrVector*);
283284
Result ParseCommand(Script*, CommandPtr*);
285+
Result ParseComponent(Component*);
284286
Result ParseAssertExceptionCommand(CommandPtr*);
285287
Result ParseAssertExhaustionCommand(CommandPtr*);
286288
Result ParseAssertInvalidCommand(CommandPtr*);
@@ -367,6 +369,11 @@ Result ParseWastScript(WastLexer* lexer,
367369
Errors*,
368370
WastParseOptions* options);
369371

372+
Result ParseWatComponent(WastLexer* lexer,
373+
std::unique_ptr<Component>* out_component,
374+
Errors*,
375+
WastParseOptions* options);
376+
370377
} // namespace wabt
371378

372379
#endif /* WABT_WAST_PARSER_H_ */

include/wabt/wat-writer.h

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

2323
namespace wabt {
2424

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

@@ -35,6 +36,7 @@ struct WriteWatOptions {
3536
};
3637

3738
Result WriteWat(Stream*, const Module*, const WriteWatOptions&);
39+
Result WriteComponentWat(Stream*, const Component*, const WriteWatOptions&);
3840

3941
} // namespace wabt
4042

src/binary-reader-ir.cc

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,58 @@ Result BinaryReaderIR::OnGenericCustomSection(std::string_view name,
20392039
return Result::Ok;
20402040
}
20412041

2042+
class BinaryReaderComponentIR : public ComponentBinaryReaderDelegate {
2043+
public:
2044+
BinaryReaderComponentIR(Component* out_component,
2045+
const char* filename,
2046+
Errors* errors);
2047+
2048+
private:
2049+
bool OnError(const Error&) override { return false; }
2050+
2051+
Result OnCoreModule(const void* data,
2052+
size_t size,
2053+
const ReadBinaryOptions& options) override;
2054+
Result BeginComponent(uint32_t version, size_t depth) override;
2055+
Result EndComponent() override;
2056+
2057+
Errors* errors_ = nullptr;
2058+
Component* component_ = nullptr;
2059+
2060+
const char* filename_;
2061+
};
2062+
2063+
BinaryReaderComponentIR::BinaryReaderComponentIR(Component* out_component,
2064+
const char* filename,
2065+
Errors* errors)
2066+
: errors_(errors), component_(out_component), filename_(filename) {}
2067+
2068+
Result BinaryReaderComponentIR::OnCoreModule(const void* data,
2069+
size_t size,
2070+
const ReadBinaryOptions& options) {
2071+
auto coreModule = std::make_unique<ComponentCoreModule>();
2072+
BinaryReaderIR reader(coreModule->module(), filename_, errors_);
2073+
CHECK_RESULT(ReadBinary(data, size, &reader, options));
2074+
component_->definitions.push_back(std::move(coreModule));
2075+
return Result::Ok;
2076+
}
2077+
2078+
Result BinaryReaderComponentIR::BeginComponent(uint32_t version, size_t depth) {
2079+
if (depth == 0) {
2080+
return Result::Ok;
2081+
}
2082+
auto innerComponent = std::make_unique<ComponentComponent>(component_);
2083+
Component* parent = component_;
2084+
component_ = innerComponent->component();
2085+
parent->definitions.push_back(std::move(innerComponent));
2086+
return Result::Ok;
2087+
}
2088+
2089+
Result BinaryReaderComponentIR::EndComponent() {
2090+
component_ = component_->parent;
2091+
return Result::Ok;
2092+
}
2093+
20422094
} // end anonymous namespace
20432095

20442096
Result ReadBinaryIr(const char* filename,
@@ -2051,4 +2103,14 @@ Result ReadBinaryIr(const char* filename,
20512103
return ReadBinary(data, size, &reader, options);
20522104
}
20532105

2106+
Result ReadBinaryComponentIr(const char* filename,
2107+
const void* data,
2108+
size_t size,
2109+
const ReadBinaryOptions& options,
2110+
Errors* errors,
2111+
Component* out_component) {
2112+
BinaryReaderComponentIR reader(out_component, filename, errors);
2113+
return ReadBinaryComponent(data, size, &reader, options);
2114+
}
2115+
20542116
} // namespace wabt

0 commit comments

Comments
 (0)