Skip to content

Commit 47680b0

Browse files
authored
Migrate type section encoding in wast to wasm-encoder (#1799)
Remove a few `Encode` impls and start laying some groundwork for other sections to get migrated as well with a new trait/method to use.
1 parent eb3d5e6 commit 47680b0

2 files changed

Lines changed: 47 additions & 112 deletions

File tree

crates/wast/src/component/binary.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use wasm_encoder::{
66
CanonicalFunctionSection, ComponentAliasSection, ComponentCoreTypeEncoder,
77
ComponentDefinedTypeEncoder, ComponentExportSection, ComponentImportSection,
88
ComponentInstanceSection, ComponentNameSection, ComponentSection, ComponentSectionId,
9-
ComponentStartSection, ComponentTypeEncoder, ComponentTypeSection, CompositeType,
10-
CoreTypeSection, InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId,
11-
SubType,
9+
ComponentStartSection, ComponentTypeEncoder, ComponentTypeSection, CoreTypeSection,
10+
InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId, SubType,
1211
};
1312

1413
pub fn encode(component: &Component<'_>, options: &EncodeOptions) -> Vec<u8> {
@@ -62,10 +61,7 @@ fn encode_core_type(encoder: ComponentCoreTypeEncoder, ty: &CoreTypeDef) {
6261
let sub_type = SubType {
6362
is_final: true,
6463
supertype_idx: None,
65-
composite_type: CompositeType {
66-
shared: def.shared,
67-
inner: (&def.kind).into(),
68-
},
64+
composite_type: def.to_composite_type(),
6965
};
7066
encoder.core().subtype(&sub_type);
7167
}
@@ -881,15 +877,7 @@ impl From<&ModuleType<'_>> for wasm_encoder::ModuleType {
881877
for decl in &ty.decls {
882878
match decl {
883879
ModuleTypeDecl::Type(t) => {
884-
let sub_type = SubType {
885-
is_final: t.final_type.unwrap_or(true),
886-
supertype_idx: t.parent.map(u32::from),
887-
composite_type: CompositeType {
888-
shared: t.def.shared,
889-
inner: (&t.def.kind).into(),
890-
},
891-
};
892-
encoded.ty().subtype(&sub_type);
880+
encoded.ty().subtype(&t.to_subtype());
893881
}
894882
ModuleTypeDecl::Alias(a) => match &a.target {
895883
AliasTarget::Outer {

crates/wast/src/core/binary.rs

Lines changed: 43 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub(crate) fn encode(
143143

144144
e.custom_sections(BeforeFirst);
145145

146-
e.section_list(SectionId::Type, Type, &types);
146+
e.typed_section(&types);
147147
e.section_list(SectionId::Import, Import, &imports);
148148

149149
let functys = funcs.iter().map(|f| &f.ty).collect::<Vec<_>>();
@@ -248,6 +248,21 @@ impl Encoder<'_> {
248248
self.custom_sections(CustomPlace::After(anchor));
249249
}
250250

251+
fn typed_section<T>(&mut self, list: &[T])
252+
where
253+
T: SectionItem,
254+
{
255+
self.custom_sections(CustomPlace::Before(T::ANCHOR));
256+
if !list.is_empty() {
257+
let mut section = T::Section::default();
258+
for item in list {
259+
item.encode(&mut section);
260+
}
261+
self.wasm.section(&section);
262+
}
263+
self.custom_sections(CustomPlace::After(T::ANCHOR));
264+
}
265+
251266
/// Encodes the code section of a wasm module module while additionally
252267
/// handling the branch hinting proposal.
253268
///
@@ -302,14 +317,11 @@ impl Encoder<'_> {
302317
}
303318
}
304319

305-
impl Encode for FunctionType<'_> {
306-
fn encode(&self, e: &mut Vec<u8>) {
307-
self.params.len().encode(e);
308-
for (_, _, ty) in self.params.iter() {
309-
ty.encode(e);
310-
}
311-
self.results.encode(e);
312-
}
320+
trait SectionItem {
321+
type Section: wasm_encoder::Section + Default;
322+
const ANCHOR: CustomPlaceAnchor;
323+
324+
fn encode(&self, section: &mut Self::Section);
313325
}
314326

315327
impl From<&FunctionType<'_>> for wasm_encoder::FuncType {
@@ -321,16 +333,6 @@ impl From<&FunctionType<'_>> for wasm_encoder::FuncType {
321333
}
322334
}
323335

324-
impl Encode for StructType<'_> {
325-
fn encode(&self, e: &mut Vec<u8>) {
326-
self.fields.len().encode(e);
327-
for field in self.fields.iter() {
328-
field.ty.encode(e);
329-
(field.mutable as i32).encode(e);
330-
}
331-
}
332-
}
333-
334336
impl From<&StructType<'_>> for wasm_encoder::StructType {
335337
fn from(st: &StructType) -> wasm_encoder::StructType {
336338
wasm_encoder::StructType {
@@ -348,13 +350,6 @@ impl From<&StructField<'_>> for wasm_encoder::FieldType {
348350
}
349351
}
350352

351-
impl Encode for ArrayType<'_> {
352-
fn encode(&self, e: &mut Vec<u8>) {
353-
self.ty.encode(e);
354-
(self.mutable as i32).encode(e);
355-
}
356-
}
357-
358353
impl From<&ArrayType<'_>> for wasm_encoder::ArrayType {
359354
fn from(at: &ArrayType) -> Self {
360355
let field = wasm_encoder::FieldType {
@@ -377,74 +372,38 @@ enum RecOrType<'a> {
377372
Rec(&'a Rec<'a>),
378373
}
379374

380-
impl Encode for RecOrType<'_> {
381-
fn encode(&self, e: &mut Vec<u8>) {
375+
impl SectionItem for RecOrType<'_> {
376+
type Section = wasm_encoder::TypeSection;
377+
const ANCHOR: CustomPlaceAnchor = CustomPlaceAnchor::Type;
378+
379+
fn encode(&self, types: &mut wasm_encoder::TypeSection) {
382380
match self {
383-
RecOrType::Type(ty) => ty.encode(e),
384-
RecOrType::Rec(rec) => rec.encode(e),
381+
RecOrType::Type(ty) => types.ty().subtype(&ty.to_subtype()),
382+
RecOrType::Rec(rec) => types.ty().rec(rec.types.iter().map(|t| t.to_subtype())),
385383
}
386384
}
387385
}
388386

389-
impl Encode for Type<'_> {
390-
fn encode(&self, e: &mut Vec<u8>) {
391-
match (&self.parent, self.final_type) {
392-
(Some(parent), Some(true)) => {
393-
// Type is final with a supertype
394-
e.push(0x4f);
395-
e.push(0x01);
396-
parent.encode(e);
397-
}
398-
(Some(parent), Some(false) | None) => {
399-
// Type is not final and has a declared supertype
400-
e.push(0x50);
401-
e.push(0x01);
402-
parent.encode(e);
403-
}
404-
(None, Some(false)) => {
405-
// Sub was used without any declared supertype
406-
e.push(0x50);
407-
e.push(0x00);
408-
}
409-
(None, _) => {} // No supertype, sub wasn't used
410-
}
411-
if self.def.shared {
412-
e.push(0x65);
413-
}
414-
match &self.def.kind {
415-
InnerTypeKind::Func(func) => {
416-
e.push(0x60);
417-
func.encode(e)
418-
}
419-
InnerTypeKind::Struct(r#struct) => {
420-
e.push(0x5f);
421-
r#struct.encode(e)
422-
}
423-
InnerTypeKind::Array(array) => {
424-
e.push(0x5e);
425-
array.encode(e)
426-
}
387+
impl Type<'_> {
388+
pub(crate) fn to_subtype(&self) -> wasm_encoder::SubType {
389+
wasm_encoder::SubType {
390+
composite_type: self.def.to_composite_type(),
391+
is_final: self.final_type.unwrap_or(true),
392+
supertype_idx: self.parent.map(|i| i.unwrap_u32()),
427393
}
428394
}
429395
}
430396

431-
impl From<&InnerTypeKind<'_>> for wasm_encoder::CompositeInnerType {
432-
fn from(kind: &InnerTypeKind) -> Self {
397+
impl TypeDef<'_> {
398+
pub(crate) fn to_composite_type(&self) -> wasm_encoder::CompositeType {
433399
use wasm_encoder::CompositeInnerType::*;
434-
match kind {
435-
InnerTypeKind::Func(ft) => Func(ft.into()),
436-
InnerTypeKind::Struct(st) => Struct(st.into()),
437-
InnerTypeKind::Array(at) => Array(at.into()),
438-
}
439-
}
440-
}
441-
442-
impl Encode for Rec<'_> {
443-
fn encode(&self, e: &mut Vec<u8>) {
444-
e.push(0x4e);
445-
self.types.len().encode(e);
446-
for ty in &self.types {
447-
ty.encode(e);
400+
wasm_encoder::CompositeType {
401+
inner: match &self.kind {
402+
InnerTypeKind::Func(ft) => Func(ft.into()),
403+
InnerTypeKind::Struct(st) => Struct(st.into()),
404+
InnerTypeKind::Array(at) => Array(at.into()),
405+
},
406+
shared: self.shared,
448407
}
449408
}
450409
}
@@ -542,18 +501,6 @@ impl<'a> Encode for RefType<'a> {
542501
}
543502
}
544503

545-
impl<'a> Encode for StorageType<'a> {
546-
fn encode(&self, e: &mut Vec<u8>) {
547-
match self {
548-
StorageType::I8 => e.push(0x78),
549-
StorageType::I16 => e.push(0x77),
550-
StorageType::Val(ty) => {
551-
ty.encode(e);
552-
}
553-
}
554-
}
555-
}
556-
557504
impl From<StorageType<'_>> for wasm_encoder::StorageType {
558505
fn from(st: StorageType) -> Self {
559506
use wasm_encoder::StorageType::*;

0 commit comments

Comments
 (0)