Skip to content

Commit a2635eb

Browse files
authored
added conversions for component specific enums (#1655)
* added conversions for component specific enums * added component_type_index * used correct type indices
1 parent 947c152 commit a2635eb

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

crates/wasm-encoder/src/reencode.rs

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub trait Reencode {
4141
utils::type_index(self, ty)
4242
}
4343

44+
fn component_type_index(&mut self, ty: u32) -> u32 {
45+
utils::component_type_index(self, ty)
46+
}
47+
4448
fn abstract_heap_type(
4549
&mut self,
4650
value: wasmparser::AbstractHeapType,
@@ -69,6 +73,45 @@ pub trait Reencode {
6973
utils::component_primitive_val_type(self, ty)
7074
}
7175

76+
fn component_export_kind(
77+
&mut self,
78+
ty: wasmparser::ComponentExternalKind,
79+
) -> crate::component::ComponentExportKind {
80+
utils::component_export_kind(self, ty)
81+
}
82+
83+
fn component_outer_alias_kind(
84+
&mut self,
85+
kind: wasmparser::ComponentOuterAliasKind,
86+
) -> crate::component::ComponentOuterAliasKind {
87+
utils::component_outer_alias_kind(self, kind)
88+
}
89+
90+
fn component_val_type(
91+
&mut self,
92+
ty: wasmparser::ComponentValType,
93+
) -> crate::component::ComponentValType {
94+
utils::component_val_type(self, ty)
95+
}
96+
97+
fn type_bounds(&mut self, ty: wasmparser::TypeBounds) -> crate::component::TypeBounds {
98+
utils::type_bounds(self, ty)
99+
}
100+
101+
fn canonical_option(
102+
&mut self,
103+
ty: wasmparser::CanonicalOption,
104+
) -> crate::component::CanonicalOption {
105+
utils::canonical_option(self, ty)
106+
}
107+
108+
fn component_type_ref(
109+
&mut self,
110+
ty: wasmparser::ComponentTypeRef,
111+
) -> crate::component::ComponentTypeRef {
112+
utils::component_type_ref(self, ty)
113+
}
114+
72115
fn const_expr(
73116
&mut self,
74117
const_expr: wasmparser::ConstExpr,
@@ -806,6 +849,116 @@ pub mod utils {
806849
}
807850
}
808851

852+
pub fn component_export_kind<T: ?Sized + Reencode>(
853+
_reencoder: &mut T,
854+
ty: wasmparser::ComponentExternalKind,
855+
) -> crate::component::ComponentExportKind {
856+
match ty {
857+
wasmparser::ComponentExternalKind::Module => crate::ComponentExportKind::Module,
858+
wasmparser::ComponentExternalKind::Func => crate::ComponentExportKind::Func,
859+
wasmparser::ComponentExternalKind::Value => crate::ComponentExportKind::Value,
860+
wasmparser::ComponentExternalKind::Type => crate::ComponentExportKind::Type,
861+
wasmparser::ComponentExternalKind::Instance => crate::ComponentExportKind::Instance,
862+
wasmparser::ComponentExternalKind::Component => crate::ComponentExportKind::Component,
863+
}
864+
}
865+
866+
pub fn component_outer_alias_kind<T: ?Sized + Reencode>(
867+
_reencoder: &mut T,
868+
ty: wasmparser::ComponentOuterAliasKind,
869+
) -> crate::component::ComponentOuterAliasKind {
870+
match ty {
871+
wasmparser::ComponentOuterAliasKind::CoreModule => {
872+
crate::component::ComponentOuterAliasKind::CoreModule
873+
}
874+
wasmparser::ComponentOuterAliasKind::CoreType => {
875+
crate::component::ComponentOuterAliasKind::CoreType
876+
}
877+
wasmparser::ComponentOuterAliasKind::Type => {
878+
crate::component::ComponentOuterAliasKind::Type
879+
}
880+
wasmparser::ComponentOuterAliasKind::Component => {
881+
crate::ComponentOuterAliasKind::Component
882+
}
883+
}
884+
}
885+
886+
pub fn component_val_type<T: ?Sized + Reencode>(
887+
reencoder: &mut T,
888+
ty: wasmparser::ComponentValType,
889+
) -> crate::component::ComponentValType {
890+
match ty {
891+
wasmparser::ComponentValType::Type(u) => {
892+
crate::component::ComponentValType::Type(reencoder.component_type_index(u))
893+
}
894+
wasmparser::ComponentValType::Primitive(pty) => {
895+
crate::component::ComponentValType::Primitive(
896+
crate::component::PrimitiveValType::from(pty),
897+
)
898+
}
899+
}
900+
}
901+
902+
pub fn type_bounds<T: ?Sized + Reencode>(
903+
reencoder: &mut T,
904+
ty: wasmparser::TypeBounds,
905+
) -> crate::component::TypeBounds {
906+
match ty {
907+
wasmparser::TypeBounds::Eq(u) => {
908+
crate::component::TypeBounds::Eq(reencoder.component_type_index(u))
909+
}
910+
wasmparser::TypeBounds::SubResource => crate::component::TypeBounds::SubResource,
911+
}
912+
}
913+
914+
pub fn component_type_ref<T: ?Sized + Reencode>(
915+
reencoder: &mut T,
916+
ty: wasmparser::ComponentTypeRef,
917+
) -> crate::component::ComponentTypeRef {
918+
match ty {
919+
wasmparser::ComponentTypeRef::Module(u) => {
920+
crate::component::ComponentTypeRef::Module(reencoder.component_type_index(u))
921+
}
922+
wasmparser::ComponentTypeRef::Func(u) => {
923+
crate::component::ComponentTypeRef::Func(reencoder.component_type_index(u))
924+
}
925+
wasmparser::ComponentTypeRef::Value(valty) => {
926+
crate::component::ComponentTypeRef::Value(reencoder.component_val_type(valty))
927+
}
928+
wasmparser::ComponentTypeRef::Type(bounds) => {
929+
crate::component::ComponentTypeRef::Type(reencoder.type_bounds(bounds))
930+
}
931+
wasmparser::ComponentTypeRef::Instance(u) => {
932+
crate::component::ComponentTypeRef::Instance(reencoder.component_type_index(u))
933+
}
934+
wasmparser::ComponentTypeRef::Component(u) => {
935+
crate::component::ComponentTypeRef::Component(reencoder.component_type_index(u))
936+
}
937+
}
938+
}
939+
940+
pub fn canonical_option<T: ?Sized + Reencode>(
941+
reencoder: &mut T,
942+
ty: wasmparser::CanonicalOption,
943+
) -> crate::component::CanonicalOption {
944+
match ty {
945+
wasmparser::CanonicalOption::UTF8 => crate::component::CanonicalOption::UTF8,
946+
wasmparser::CanonicalOption::UTF16 => crate::component::CanonicalOption::UTF16,
947+
wasmparser::CanonicalOption::CompactUTF16 => {
948+
crate::component::CanonicalOption::CompactUTF16
949+
}
950+
wasmparser::CanonicalOption::Memory(u) => {
951+
crate::component::CanonicalOption::Memory(reencoder.memory_index(u))
952+
}
953+
wasmparser::CanonicalOption::Realloc(u) => {
954+
crate::component::CanonicalOption::Realloc(reencoder.function_index(u))
955+
}
956+
wasmparser::CanonicalOption::PostReturn(u) => {
957+
crate::component::CanonicalOption::PostReturn(reencoder.function_index(u))
958+
}
959+
}
960+
}
961+
809962
pub fn memory_index<T: ?Sized + Reencode>(_reencoder: &mut T, memory: u32) -> u32 {
810963
memory
811964
}
@@ -916,6 +1069,10 @@ pub mod utils {
9161069
ty
9171070
}
9181071

1072+
pub fn component_type_index<T: ?Sized + Reencode>(_reencoder: &mut T, ty: u32) -> u32 {
1073+
ty
1074+
}
1075+
9191076
pub fn tag_type<T: ?Sized + Reencode>(
9201077
reencoder: &mut T,
9211078
tag_ty: wasmparser::TagType,
@@ -1620,6 +1777,42 @@ pub mod utils {
16201777
}
16211778
}
16221779

1780+
impl From<wasmparser::ComponentValType> for crate::ComponentValType {
1781+
fn from(ty: wasmparser::ComponentValType) -> Self {
1782+
RoundtripReencoder.component_val_type(ty)
1783+
}
1784+
}
1785+
1786+
impl From<wasmparser::TypeBounds> for crate::TypeBounds {
1787+
fn from(ty: wasmparser::TypeBounds) -> Self {
1788+
RoundtripReencoder.type_bounds(ty)
1789+
}
1790+
}
1791+
1792+
impl From<wasmparser::CanonicalOption> for crate::CanonicalOption {
1793+
fn from(opt: wasmparser::CanonicalOption) -> Self {
1794+
RoundtripReencoder.canonical_option(opt)
1795+
}
1796+
}
1797+
1798+
impl From<wasmparser::ComponentExternalKind> for crate::ComponentExportKind {
1799+
fn from(kind: wasmparser::ComponentExternalKind) -> Self {
1800+
RoundtripReencoder.component_export_kind(kind)
1801+
}
1802+
}
1803+
1804+
impl From<wasmparser::ComponentOuterAliasKind> for crate::ComponentOuterAliasKind {
1805+
fn from(kind: wasmparser::ComponentOuterAliasKind) -> Self {
1806+
RoundtripReencoder.component_outer_alias_kind(kind)
1807+
}
1808+
}
1809+
1810+
impl From<wasmparser::ComponentTypeRef> for crate::ComponentTypeRef {
1811+
fn from(ty: wasmparser::ComponentTypeRef) -> Self {
1812+
RoundtripReencoder.component_type_ref(ty)
1813+
}
1814+
}
1815+
16231816
impl From<wasmparser::PrimitiveValType> for crate::PrimitiveValType {
16241817
fn from(ty: wasmparser::PrimitiveValType) -> Self {
16251818
RoundtripReencoder.component_primitive_val_type(ty)

0 commit comments

Comments
 (0)