|
1 | 1 | use crate::component::*;
|
2 | 2 | use crate::core;
|
3 | 3 | use crate::core::EncodeOptions;
|
4 |
| -use crate::token::{Id, Index, NameAnnotation, Span}; |
| 4 | +use crate::token::{Id, Index, NameAnnotation}; |
5 | 5 | use wasm_encoder::{
|
6 | 6 | CanonicalFunctionSection, ComponentAliasSection, ComponentCoreTypeEncoder,
|
7 | 7 | ComponentDefinedTypeEncoder, ComponentExportSection, ComponentImportSection,
|
8 | 8 | ComponentInstanceSection, ComponentNameSection, ComponentSection, ComponentSectionId,
|
9 | 9 | ComponentStartSection, ComponentTypeEncoder, ComponentTypeSection, CoreTypeSection,
|
10 |
| - InstanceSection, NameMap, NestedComponentSection, RawSection, SectionId, SubType, |
| 10 | + InstanceSection, NameMap, NestedComponentSection, RawSection, SubType, |
11 | 11 | };
|
12 | 12 |
|
13 | 13 | pub fn encode(component: &Component<'_>, options: &EncodeOptions) -> Vec<u8> {
|
@@ -177,19 +177,12 @@ impl<'a> Encoder<'a> {
|
177 | 177 | fn encode_custom(&mut self, custom: &Custom) {
|
178 | 178 | // Flush any in-progress section before encoding the customs section
|
179 | 179 | self.flush(None);
|
180 |
| - self.component.section(custom); |
| 180 | + self.component.section(&custom.to_section()); |
181 | 181 | }
|
182 | 182 |
|
183 | 183 | fn encode_producers(&mut self, custom: &core::Producers) {
|
184 |
| - use crate::encode::Encode; |
185 |
| - |
186 |
| - let mut data = Vec::new(); |
187 |
| - custom.encode(&mut data); |
188 |
| - self.encode_custom(&Custom { |
189 |
| - name: "producers", |
190 |
| - span: Span::from_offset(0), |
191 |
| - data: vec![&data], |
192 |
| - }) |
| 184 | + self.flush(None); |
| 185 | + self.component.section(&custom.to_section()); |
193 | 186 | }
|
194 | 187 |
|
195 | 188 | fn encode_core_module(&mut self, module: &CoreModule<'a>, options: &EncodeOptions) {
|
@@ -549,32 +542,16 @@ fn get_name<'a>(id: &Option<Id<'a>>, name: &Option<NameAnnotation<'a>>) -> Optio
|
549 | 542 | })
|
550 | 543 | }
|
551 | 544 |
|
552 |
| -// This implementation is much like `wasm_encoder::CustomSection`, except |
553 |
| -// that it extends via a list of slices instead of a single slice. |
554 |
| -impl wasm_encoder::Encode for Custom<'_> { |
555 |
| - fn encode(&self, sink: &mut Vec<u8>) { |
556 |
| - let mut buf = [0u8; 5]; |
557 |
| - let encoded_name_len = |
558 |
| - leb128::write::unsigned(&mut &mut buf[..], u64::try_from(self.name.len()).unwrap()) |
559 |
| - .unwrap(); |
560 |
| - let data_len = self.data.iter().fold(0, |acc, s| acc + s.len()); |
561 |
| - |
562 |
| - // name length |
563 |
| - (encoded_name_len + self.name.len() + data_len).encode(sink); |
564 |
| - |
565 |
| - // name |
566 |
| - self.name.encode(sink); |
567 |
| - |
568 |
| - // data |
569 |
| - for s in &self.data { |
570 |
| - sink.extend(*s); |
| 545 | +impl Custom<'_> { |
| 546 | + fn to_section(&self) -> wasm_encoder::CustomSection<'_> { |
| 547 | + let mut ret = Vec::new(); |
| 548 | + for list in self.data.iter() { |
| 549 | + ret.extend_from_slice(list); |
| 550 | + } |
| 551 | + wasm_encoder::CustomSection { |
| 552 | + name: self.name.into(), |
| 553 | + data: ret.into(), |
571 | 554 | }
|
572 |
| - } |
573 |
| -} |
574 |
| - |
575 |
| -impl wasm_encoder::ComponentSection for Custom<'_> { |
576 |
| - fn id(&self) -> u8 { |
577 |
| - SectionId::Custom.into() |
578 | 555 | }
|
579 | 556 | }
|
580 | 557 |
|
@@ -629,63 +606,6 @@ impl From<core::HeapType<'_>> for wasm_encoder::HeapType {
|
629 | 606 | }
|
630 | 607 | }
|
631 | 608 |
|
632 |
| -impl From<&core::ItemKind<'_>> for wasm_encoder::EntityType { |
633 |
| - fn from(kind: &core::ItemKind) -> Self { |
634 |
| - match kind { |
635 |
| - core::ItemKind::Func(t) => Self::Function(t.into()), |
636 |
| - core::ItemKind::Table(t) => Self::Table((*t).into()), |
637 |
| - core::ItemKind::Memory(t) => Self::Memory((*t).into()), |
638 |
| - core::ItemKind::Global(t) => Self::Global((*t).into()), |
639 |
| - core::ItemKind::Tag(t) => Self::Tag(t.into()), |
640 |
| - } |
641 |
| - } |
642 |
| -} |
643 |
| - |
644 |
| -impl From<core::TableType<'_>> for wasm_encoder::TableType { |
645 |
| - fn from(ty: core::TableType) -> Self { |
646 |
| - Self { |
647 |
| - element_type: ty.elem.into(), |
648 |
| - minimum: ty.limits.min, |
649 |
| - maximum: ty.limits.max, |
650 |
| - table64: ty.limits.is64, |
651 |
| - shared: ty.shared, |
652 |
| - } |
653 |
| - } |
654 |
| -} |
655 |
| - |
656 |
| -impl From<core::MemoryType> for wasm_encoder::MemoryType { |
657 |
| - fn from(ty: core::MemoryType) -> Self { |
658 |
| - Self { |
659 |
| - minimum: ty.limits.min, |
660 |
| - maximum: ty.limits.max, |
661 |
| - memory64: ty.limits.is64, |
662 |
| - shared: ty.shared, |
663 |
| - page_size_log2: ty.page_size_log2, |
664 |
| - } |
665 |
| - } |
666 |
| -} |
667 |
| - |
668 |
| -impl From<core::GlobalType<'_>> for wasm_encoder::GlobalType { |
669 |
| - fn from(ty: core::GlobalType) -> Self { |
670 |
| - Self { |
671 |
| - val_type: ty.ty.into(), |
672 |
| - mutable: ty.mutable, |
673 |
| - shared: ty.shared, |
674 |
| - } |
675 |
| - } |
676 |
| -} |
677 |
| - |
678 |
| -impl From<&core::TagType<'_>> for wasm_encoder::TagType { |
679 |
| - fn from(ty: &core::TagType) -> Self { |
680 |
| - match ty { |
681 |
| - core::TagType::Exception(r) => Self { |
682 |
| - kind: wasm_encoder::TagKind::Exception, |
683 |
| - func_type_idx: r.into(), |
684 |
| - }, |
685 |
| - } |
686 |
| - } |
687 |
| -} |
688 |
| - |
689 | 609 | impl<T: std::fmt::Debug> From<&core::TypeUse<'_, T>> for u32 {
|
690 | 610 | fn from(u: &core::TypeUse<'_, T>) -> Self {
|
691 | 611 | match &u.index {
|
@@ -898,10 +818,10 @@ impl From<&ModuleType<'_>> for wasm_encoder::ModuleType {
|
898 | 818 | _ => unreachable!("only outer type aliases are supported"),
|
899 | 819 | },
|
900 | 820 | ModuleTypeDecl::Import(i) => {
|
901 |
| - encoded.import(i.module, i.field, (&i.item.kind).into()); |
| 821 | + encoded.import(i.module, i.field, i.item.to_entity_type()); |
902 | 822 | }
|
903 | 823 | ModuleTypeDecl::Export(name, item) => {
|
904 |
| - encoded.export(name, (&item.kind).into()); |
| 824 | + encoded.export(name, item.to_entity_type()); |
905 | 825 | }
|
906 | 826 | }
|
907 | 827 | }
|
|
0 commit comments