Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
0adc251
wasm-encoder: update to the latest component model changes.
May 11, 2022
0d95394
wasm-mutate: update for wasm-encoder changes.
May 20, 2022
8567df2
wasm-smith: update for wasm-encoder changes.
May 24, 2022
636ed93
wasmparser: update for the component model spec changes.
May 24, 2022
3b06f9e
wasmparser-dump: update for component model changes.
May 26, 2022
fd3c178
wasm-mutate: update to latest wasmparser changes.
May 26, 2022
2b76150
wasm-smith: update for wasmparser changes.
May 26, 2022
d96330c
wasmprinter: update for latest component model spec changes.
May 26, 2022
232d0d5
fuzz-targets: update fuzz targets for wasmparser changes.
May 26, 2022
b87b70a
objdump: fix the objdump command for latest `wasmparser` changes.
May 26, 2022
ad0124e
Add core types to component and instance types.
Jun 3, 2022
bcdacf1
wasm-encoder: fix incorrect section id for component alias section.
Jun 3, 2022
ac19950
wasm-encoder: fix missing increments when adding alias items.
Jun 3, 2022
f86c942
wasmparser: fix incorrect canonical option checking.
Jun 5, 2022
448faea
wasmprinter: fixes to output to conform to text format.
Jun 5, 2022
f85b717
wasmparser: fix validation for post-return option.
Jun 6, 2022
da2938e
wasmprinter: fix output for component start functions.
Jun 6, 2022
21a1603
wasmparser-dump: fix output to make core vs. component items clearer.
Jun 6, 2022
6a4e65a
wast: update for the component model proposal changes.
May 28, 2022
29a4687
Update wat tests.
Jun 3, 2022
e6025b8
Fix doc comment relating to `post-return` option.
Jun 6, 2022
e0f4912
wasm-encoder: move component-related core sections.
Jun 6, 2022
0e2b1c8
wasmparser: move component-related core sections.
Jun 7, 2022
ab81b9f
wast: fix parsing and encoding of aliases.
Jun 7, 2022
a30fed6
wasm-smith: encode arbitrary outer aliases for core types.
Jun 7, 2022
e3325ca
wasm-encoder: remove unnecessary `Export` type.
Jun 7, 2022
14c1a3e
wast: add missing version for wasm-encoder reference.
Jun 7, 2022
2e1f736
wasmparser: remove `Module` from `TypeRef`.
Jun 7, 2022
31e4ead
Replace any references to "defaults_to" with "refines".
Jun 7, 2022
a3e18e7
wast: implement identifier resolution for variant cases.
Jun 7, 2022
db1f468
wasmparser: move component core type reading to a separate reader.
Jun 7, 2022
6544891
wasm-encoder: move component core type encoding to a separate section.
Jun 7, 2022
314509c
wast: allow variant case refines clause to reference by index.
Jun 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 170 additions & 97 deletions crates/dump/src/lib.rs

Large diffs are not rendered by default.

66 changes: 47 additions & 19 deletions crates/wasm-encoder/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,79 @@
mod aliases;
mod canonicals;
mod components;
mod exports;
mod functions;
mod imports;
mod instances;
mod modules;
mod start;
mod types;

pub use self::aliases::*;
pub use self::canonicals::*;
pub use self::components::*;
pub use self::exports::*;
pub use self::functions::*;
pub use self::imports::*;
pub use self::instances::*;
pub use self::modules::*;
pub use self::start::*;
pub use self::types::*;

use crate::Encode;
use crate::{CustomSection, Encode};

// Core sorts extended by the component model
const CORE_TYPE_SORT: u8 = 0x10;
const CORE_MODULE_SORT: u8 = 0x11;
const CORE_INSTANCE_SORT: u8 = 0x12;

const CORE_SORT: u8 = 0x00;
const FUNCTION_SORT: u8 = 0x01;
const VALUE_SORT: u8 = 0x02;
const TYPE_SORT: u8 = 0x03;
const COMPONENT_SORT: u8 = 0x04;
const INSTANCE_SORT: u8 = 0x05;

/// A WebAssembly component section.
///
/// Various builders defined in this crate already implement this trait, but you
/// can also implement it yourself for your own custom section builders, or use
/// `RawSection` to use a bunch of raw bytes as a section.
pub trait ComponentSection: Encode {}
pub trait ComponentSection: Encode {
/// Gets the section identifier for this section.
fn id(&self) -> u8;
}

/// Known section identifiers of WebAssembly components.
///
/// These sections are supported by the component model proposal.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
#[repr(u8)]
pub enum ComponentSectionId {
/// The section is a custom section.
Custom = 0,
/// The section is a type section.
Type = 1,
/// The section is an import section.
Import = 2,
/// The section is a function section.
Function = 3,
/// The section is a module section.
Module = 4,
/// The section is a core custom section.
CoreCustom = 0,
/// The section is a core module section.
CoreModule = 1,
/// The section is a core instance section.
CoreInstance = 2,
/// The section is a core alias section.
CoreAlias = 3,
/// The section is a core type section.
CoreType = 4,
/// The section is a component section.
Component = 5,
/// The section is an instance section.
Instance = 6,
/// The section is an export section.
Export = 7,
/// The section is a start section.
Start = 8,
/// The section is an alias section.
Alias = 9,
Alias = 7,
/// The section is a type section.
Type = 8,
/// The section is a canonical function section.
CanonicalFunction = 9,
/// The section is a start section.
Start = 10,
/// The section is an import section.
Import = 11,
/// The section is an export section.
Export = 12,
}

impl From<ComponentSectionId> for u8 {
Expand Down Expand Up @@ -97,6 +118,7 @@ impl Component {

/// Write a section to this component.
pub fn section(&mut self, section: &impl ComponentSection) -> &mut Self {
self.bytes.push(section.id());
section.encode(&mut self.bytes);
self
}
Expand All @@ -107,3 +129,9 @@ impl Default for Component {
Self::new()
}
}

impl ComponentSection for CustomSection<'_> {
fn id(&self) -> u8 {
ComponentSectionId::CoreCustom.into()
}
}
193 changes: 114 additions & 79 deletions crates/wasm-encoder/src/component/aliases.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
use crate::{encode_section, ComponentSection, ComponentSectionId, Encode};
use super::{COMPONENT_SORT, CORE_MODULE_SORT, CORE_SORT, CORE_TYPE_SORT, TYPE_SORT};
use crate::{
encode_section, ComponentExportKind, ComponentSection, ComponentSectionId, Encode, ExportKind,
};

/// Represents the expected export kind for an alias.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AliasExportKind {
/// The alias is to a module.
Module,
/// The alias is to a component.
Component,
/// The alias is to an instance.
Instance,
/// The alias is to a component function.
ComponentFunction,
/// The alias is to a value.
Value,
/// The alias is to a core WebAssembly function.
Function,
/// The alias is to a table.
Table,
/// The alias is to a memory.
Memory,
/// The alias is to a global.
Global,
/// The alias is to a tag.
Tag,
}

impl Encode for AliasExportKind {
fn encode(&self, sink: &mut Vec<u8>) {
let (preamble, value) = match self {
AliasExportKind::Module => (0x00, 0x00),
AliasExportKind::Component => (0x00, 0x01),
AliasExportKind::Instance => (0x00, 0x02),
AliasExportKind::ComponentFunction => (0x00, 0x03),
AliasExportKind::Value => (0x00, 0x04),
AliasExportKind::Function => (0x01, 0x00),
AliasExportKind::Table => (0x01, 0x01),
AliasExportKind::Memory => (0x01, 0x02),
AliasExportKind::Global => (0x01, 0x03),
AliasExportKind::Tag => (0x01, 0x04),
};

sink.push(preamble);
sink.push(value);
}
}

/// An encoder for the alias section of WebAssembly component.
/// An encoder for the core alias section of WebAssembly components.
///
/// # Example
///
/// ```rust
/// use wasm_encoder::{Component, AliasSection, AliasExportKind};
/// use wasm_encoder::{Component, AliasSection, ExportKind};
///
/// let mut aliases = AliasSection::new();
/// aliases.outer_type(0, 2);
/// aliases.instance_export(0, AliasExportKind::Function, "foo");
/// aliases.instance_export(0, ExportKind::Func, "f");
///
/// let mut component = Component::new();
/// component.section(&aliases);
Expand All @@ -68,7 +25,7 @@ pub struct AliasSection {
}

impl AliasSection {
/// Create a new alias section encoder.
/// Create a new core alias section encoder.
pub fn new() -> Self {
Self::default()
}
Expand All @@ -83,49 +40,123 @@ impl AliasSection {
self.num_added == 0
}

/// Define an alias that references the export of a defined instance.
/// Define an alias to an instance's export.
pub fn instance_export(
&mut self,
instance: u32,
kind: AliasExportKind,
instance_index: u32,
kind: ExportKind,
name: &str,
) -> &mut Self {
kind.encode(&mut self.bytes);
instance.encode(&mut self.bytes);
self.bytes.push(0x00);
instance_index.encode(&mut self.bytes);
name.encode(&mut self.bytes);
self.num_added += 1;
self
}
}

/// Define an alias to an outer type.
///
/// The count starts at 0 to represent the current component.
pub fn outer_type(&mut self, count: u32, index: u32) -> &mut Self {
self.bytes.push(0x02);
self.bytes.push(0x05);
count.encode(&mut self.bytes);
index.encode(&mut self.bytes);
self.num_added += 1;
self
impl Encode for AliasSection {
fn encode(&self, sink: &mut Vec<u8>) {
encode_section(sink, self.num_added, &self.bytes);
}
}

/// Define an alias to an outer module.
///
/// The count starts at 0 to represent the current component.
pub fn outer_module(&mut self, count: u32, index: u32) -> &mut Self {
self.bytes.push(0x02);
impl ComponentSection for AliasSection {
fn id(&self) -> u8 {
ComponentSectionId::CoreAlias.into()
}
}

/// Represents the kinds of outer aliasable items in a component.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ComponentOuterAliasKind {
/// The alias is to a core module.
CoreModule,
/// The alias is to a core type.
CoreType,
/// The alias is to a type.
Type,
/// The alias is to a component.
Component,
}

impl Encode for ComponentOuterAliasKind {
fn encode(&self, sink: &mut Vec<u8>) {
match self {
Self::CoreModule => {
sink.push(CORE_SORT);
sink.push(CORE_MODULE_SORT);
}
Self::CoreType => {
sink.push(CORE_SORT);
sink.push(CORE_TYPE_SORT);
}
Self::Type => sink.push(TYPE_SORT),
Self::Component => sink.push(COMPONENT_SORT),
}
}
}

/// An encoder for the alias section of WebAssembly component.
///
/// # Example
///
/// ```rust
/// use wasm_encoder::{Component, ComponentAliasSection, ComponentExportKind, ComponentOuterAliasKind};
///
/// let mut aliases = ComponentAliasSection::new();
/// aliases.instance_export(0, ComponentExportKind::Func, "f");
/// aliases.outer(0, ComponentOuterAliasKind::Type, 1);
///
/// let mut component = Component::new();
/// component.section(&aliases);
///
/// let bytes = component.finish();
/// ```
#[derive(Clone, Debug, Default)]
pub struct ComponentAliasSection {
bytes: Vec<u8>,
num_added: u32,
}

impl ComponentAliasSection {
/// Create a new alias section encoder.
pub fn new() -> Self {
Self::default()
}

/// The number of aliases in the section.
pub fn len(&self) -> u32 {
self.num_added
}

/// Determines if the section is empty.
pub fn is_empty(&self) -> bool {
self.num_added == 0
}

/// Define an alias to an instance's export.
pub fn instance_export(
&mut self,
instance_index: u32,
kind: ComponentExportKind,
name: &str,
) -> &mut Self {
kind.encode(&mut self.bytes);
self.bytes.push(0x00);
count.encode(&mut self.bytes);
index.encode(&mut self.bytes);
instance_index.encode(&mut self.bytes);
name.encode(&mut self.bytes);
self.num_added += 1;
self
}

/// Define an alias to an outer component.
/// Define an alias to an outer component item.
///
/// The count starts at 0 to represent the current component.
pub fn outer_component(&mut self, count: u32, index: u32) -> &mut Self {
self.bytes.push(0x02);
/// The count starts at 0 to indicate the current component, 1 indicates the direct
/// parent, 2 the grandparent, etc.
pub fn outer(&mut self, count: u32, kind: ComponentOuterAliasKind, index: u32) -> &mut Self {
kind.encode(&mut self.bytes);
self.bytes.push(0x01);
count.encode(&mut self.bytes);
index.encode(&mut self.bytes);
Expand All @@ -134,10 +165,14 @@ impl AliasSection {
}
}

impl Encode for AliasSection {
impl Encode for ComponentAliasSection {
fn encode(&self, sink: &mut Vec<u8>) {
encode_section(sink, ComponentSectionId::Alias, self.num_added, &self.bytes);
encode_section(sink, self.num_added, &self.bytes);
}
}

impl ComponentSection for AliasSection {}
impl ComponentSection for ComponentAliasSection {
fn id(&self) -> u8 {
ComponentSectionId::Alias.into()
}
}
Loading