Skip to content

Commit 0f3bbbe

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

19 files changed

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

81+
struct ComponentFuncParam {
82+
std::string_view name;
83+
ComponentType type;
84+
};
85+
8186
class BinaryReaderDelegate {
8287
public:
8388
struct State {
@@ -555,11 +560,47 @@ class BinaryReaderDelegate {
555560
const State* state = nullptr;
556561
};
557562

563+
class ComponentBinaryReaderDelegate {
564+
public:
565+
virtual ~ComponentBinaryReaderDelegate() {}
566+
567+
virtual bool OnError(const Error&) = 0;
568+
virtual void OnSetState(const BinaryReaderDelegate::State* s) { state = s; }
569+
570+
virtual Result OnCoreModule(const void* data,
571+
size_t size,
572+
const ReadBinaryOptions& options) = 0;
573+
virtual Result BeginComponent(uint32_t version, size_t depth) = 0;
574+
virtual Result EndComponent() = 0;
575+
576+
virtual Result BeginTypeSection(uint32_t count) = 0;
577+
virtual Result EndTypeSection() = 0;
578+
579+
virtual Result OnPrimitiveType(ComponentType type) = 0;
580+
virtual Result OnOptionType(ComponentType type) = 0;
581+
virtual Result OnResultType(ComponentType result, ComponentType error) = 0;
582+
virtual Result OnFuncType(ComponentBinaryType type,
583+
uint32_t param_count,
584+
ComponentFuncParam* params,
585+
ComponentType result) = 0;
586+
virtual Result BeginInstanceType(uint32_t count) = 0;
587+
virtual Result EndInstanceType() = 0;
588+
virtual Result BeginComponentType(uint32_t count) = 0;
589+
virtual Result EndComponentType() = 0;
590+
591+
const BinaryReaderDelegate::State* state = nullptr;
592+
};
593+
558594
Result ReadBinary(const void* data,
559595
size_t size,
560596
BinaryReaderDelegate* reader,
561597
const ReadBinaryOptions& options);
562598

599+
Result ReadBinaryComponent(const void* data,
600+
size_t size,
601+
ComponentBinaryReaderDelegate* component_delegate,
602+
const ReadBinaryOptions& options);
603+
563604
size_t ReadU32Leb128(const uint8_t* ptr,
564605
const uint8_t* end,
565606
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: 35 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,40 @@ enum class NameSectionSubsection {
104105
};
105106
const char* GetNameSectionSubsectionName(NameSectionSubsection subsec);
106107

108+
enum class ComponentBinarySection : uint8_t {
109+
// Same as ComponentDefinition::Type in ir.h.
110+
CoreModule = 1,
111+
CoreInstance = 2,
112+
CoreType = 3,
113+
Component = 4,
114+
Instance = 5,
115+
Type = 7,
116+
};
117+
118+
enum class ComponentBinaryType : uint8_t {
119+
None = 0,
120+
Some = 1,
121+
122+
ResultSome = 0,
123+
ResultNone = 1,
124+
125+
// Types
126+
Option = 0x6b,
127+
Result = 0x6a,
128+
AsyncFunc = 0x43,
129+
Instance = 0x42,
130+
Component = 0x41,
131+
Func = 0x40,
132+
};
133+
134+
enum class ComponentBinaryInterface : uint8_t {
135+
CoreType = 0,
136+
Type = 1,
137+
Alias = 2,
138+
Import = 3,
139+
Export = 4,
140+
};
141+
107142
} // namespace wabt
108143

109144
#endif /* WABT_BINARY_H_ */

include/wabt/ir.h

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

1705+
class Component;
1706+
class ComponentTypeContainer;
1707+
class ComponentCoreModule;
1708+
class ComponentValueType;
1709+
class ComponentTypeResult;
1710+
class ComponentTypeFunc;
1711+
1712+
class ComponentDefinition {
1713+
public:
1714+
enum class Section : uint8_t {
1715+
// Same as ComponentBinarySection in binary.h.
1716+
// Section types.
1717+
CoreModule = 1,
1718+
CoreInstance = 2,
1719+
CoreType = 3,
1720+
Component = 4,
1721+
Instance = 5,
1722+
Type = 7,
1723+
};
1724+
1725+
// Subtype for Section::Type
1726+
enum class Type : uint8_t {
1727+
Unused = 0,
1728+
ValueType = 1,
1729+
1730+
// Types
1731+
Option = 0x6b,
1732+
Result = 0x6a,
1733+
AsyncFunc = 0x43,
1734+
Instance = 0x42,
1735+
Component = 0x41,
1736+
Func = 0x40,
1737+
};
1738+
1739+
virtual ~ComponentDefinition() {}
1740+
1741+
Section section() const {
1742+
return section_;
1743+
}
1744+
1745+
Type type() const {
1746+
return type_;
1747+
}
1748+
1749+
const Component* AsComponent() const {
1750+
assert(section() == Section::Component);
1751+
return reinterpret_cast<const Component*>(this);
1752+
}
1753+
1754+
const ComponentTypeContainer* AsTypeContainer() const {
1755+
assert(section() == Section::Type &&
1756+
(type() == Type::Instance || type() == Type::Component));
1757+
return reinterpret_cast<const ComponentTypeContainer*>(this);
1758+
}
1759+
1760+
const ComponentCoreModule* AsCoreModule() const {
1761+
assert(section() == Section::CoreModule);
1762+
return reinterpret_cast<const ComponentCoreModule*>(this);
1763+
}
1764+
1765+
const ComponentValueType* AsValueType() const {
1766+
assert(IsValueType());
1767+
return reinterpret_cast<const ComponentValueType*>(this);
1768+
}
1769+
1770+
const ComponentTypeResult* AsTypeResult() const {
1771+
assert(section() == Section::Type && type() == Type::Result);
1772+
return reinterpret_cast<const ComponentTypeResult*>(this);
1773+
}
1774+
1775+
const ComponentTypeFunc* AsTypeFunc() const {
1776+
assert(section() == Section::Type && type() == Type::Func);
1777+
return reinterpret_cast<const ComponentTypeFunc*>(this);
1778+
}
1779+
1780+
bool IsValueType() const {
1781+
return type_ == Type::ValueType || type_ == Type::Option;
1782+
}
1783+
1784+
bool IsValueTypeOpt() const {
1785+
return false;
1786+
}
1787+
1788+
protected:
1789+
ComponentDefinition(Section section, Type type = Type::Unused)
1790+
: section_(section), type_(type) {
1791+
assert(section == Section::Type ? type != Type::Unused : type == Type::Unused);
1792+
}
1793+
1794+
private:
1795+
Section section_;
1796+
// Secondary, optional type field.
1797+
Type type_;
1798+
};
1799+
1800+
class ComponentContainer : public ComponentDefinition {
1801+
public:
1802+
const ComponentContainer* GetParent() const {
1803+
return parent_;
1804+
}
1805+
bool IsComponent() const {
1806+
return section() == Section::Component;
1807+
}
1808+
bool IsInstanceType() const {
1809+
return section() == Section::Type && type() == Type::Instance;
1810+
}
1811+
bool IsComponentType() const {
1812+
return section() == Section::Type && type() == Type::Component;
1813+
}
1814+
1815+
size_t Size() const {
1816+
return list_.size();
1817+
}
1818+
1819+
const ComponentDefinition* Get(size_t idx) const {
1820+
return list_[idx].get();
1821+
}
1822+
1823+
void Append(std::unique_ptr<ComponentDefinition> type) {
1824+
list_.push_back(std::move(type));
1825+
}
1826+
1827+
Index AppendType(std::unique_ptr<ComponentDefinition> type);
1828+
1829+
protected:
1830+
ComponentContainer(const ComponentContainer* parent,
1831+
Section section,
1832+
Type type = Type::Unused)
1833+
: ComponentDefinition(section, type), parent_(parent) {
1834+
assert(IsComponent() || IsInstanceType() || IsComponentType());
1835+
}
1836+
1837+
private:
1838+
const ComponentContainer* parent_;
1839+
std::vector<std::unique_ptr<ComponentDefinition>> list_;
1840+
std::vector<ComponentDefinition*> type_list_;
1841+
};
1842+
1843+
class ComponentTypeContainer : public ComponentContainer {
1844+
public:
1845+
ComponentTypeContainer(const ComponentContainer* parent, bool is_component)
1846+
: ComponentContainer(parent, Section::Type,
1847+
is_component ? Type::Component : Type::Instance) {}
1848+
};
1849+
1850+
class ComponentCoreModule : public ComponentDefinition {
1851+
public:
1852+
ComponentCoreModule()
1853+
: ComponentDefinition(Section::CoreModule) {}
1854+
1855+
const Module* module() const {
1856+
return &module_;
1857+
}
1858+
1859+
Module* module() {
1860+
return &module_;
1861+
}
1862+
1863+
private:
1864+
Module module_;
1865+
};
1866+
1867+
class ComponentValueType : public ComponentDefinition {
1868+
public:
1869+
ComponentValueType(Type type, ComponentType value_type)
1870+
: ComponentDefinition(Section::Type, type), value_type_(value_type) {
1871+
assert(IsValueType() && !value_type_.IsNone());
1872+
}
1873+
1874+
const ComponentType& ValueType() const {
1875+
return value_type_;
1876+
}
1877+
1878+
private:
1879+
ComponentType value_type_;
1880+
};
1881+
1882+
class ComponentTypeResult : public ComponentDefinition {
1883+
public:
1884+
ComponentTypeResult(ComponentType result, ComponentType error)
1885+
: ComponentDefinition(Section::Type, Type::Result), result_(result), error_(error) {}
1886+
1887+
const ComponentType& result() const { return result_; }
1888+
const ComponentType& error() const { return error_; }
1889+
1890+
private:
1891+
ComponentType result_;
1892+
ComponentType error_;
1893+
};
1894+
1895+
class ComponentTypeFunc : public ComponentDefinition {
1896+
public:
1897+
struct Param {
1898+
std::string name;
1899+
ComponentType type;
1900+
};
1901+
1902+
ComponentTypeFunc(bool is_async, std::vector<Param>* params,
1903+
ComponentType& result)
1904+
: ComponentDefinition(Section::Type,
1905+
is_async ? Type::AsyncFunc : Type::Func),
1906+
result_(result) {
1907+
params_ = std::move(*params);
1908+
}
1909+
1910+
const std::vector<Param>& params() const {
1911+
return params_;
1912+
}
1913+
1914+
const ComponentType& result() const {
1915+
return result_;
1916+
}
1917+
1918+
private:
1919+
std::vector<Param> params_;
1920+
ComponentType result_;
1921+
};
1922+
1923+
class Component : public ComponentContainer {
1924+
public:
1925+
Component(const Component* parent)
1926+
: ComponentContainer(parent, Section::Component) {}
1927+
1928+
const Component* GetParentComponent() const {
1929+
return GetParent()->AsComponent();
1930+
}
1931+
};
1932+
17051933
void MakeTypeBindingReverseMapping(
17061934
size_t num_types,
17071935
const BindingHash& bindings,

0 commit comments

Comments
 (0)