@@ -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+
17051933void MakeTypeBindingReverseMapping (
17061934 size_t num_types,
17071935 const BindingHash& bindings,
0 commit comments