Skip to content

Commit

Permalink
Merge pull request #48 from dhil/wasmfx-merge
Browse files Browse the repository at this point in the history
Merge with upstream
  • Loading branch information
dhil authored Jan 10, 2024
2 parents 8c38ba4 + 34c2da8 commit 802554d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const INDIRECT_TABLE_NAME: &str = "$imports";

pub mod docs;
mod wit;
pub use wit::{encode, encode_component, encode_world};
pub use wit::{encode, encode_world};

mod types;
use types::{InstanceTypeEncoder, RootTypeEncoder, ValtypeEncoder};
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn embed_component_metadata(
world: WorldId,
encoding: StringEncoding,
) -> Result<()> {
let encoded = metadata::encode(&wit_resolver, world, encoding, None, None)?;
let encoded = metadata::encode(&wit_resolver, world, encoding, None)?;

let section = wasm_encoder::CustomSection {
name: "component-type".into(),
Expand Down
114 changes: 27 additions & 87 deletions crates/wit-component/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use std::borrow::Cow;
use wasm_encoder::{
ComponentBuilder, ComponentExportKind, ComponentType, ComponentTypeRef, CustomSection, Encode,
ComponentBuilder, ComponentExportKind, ComponentType, ComponentTypeRef, CustomSection,
};
use wasm_metadata::Producers;
use wasmparser::types::ComponentAnyTypeId;
Expand Down Expand Up @@ -170,94 +170,34 @@ pub fn encode(
world: WorldId,
string_encoding: StringEncoding,
extra_producers: Option<&Producers>,
use_next_encoding: Option<bool>,
) -> Result<Vec<u8>> {
enum EncodingFormat {
// The encoding of the previous format was:
//
// * A version byte, at the time 0x03.
// * A string-encoding byte.
// * A string which is the name of a world.
// * A wasm-encoded WIT package which contains the previous world.
//
// Note that this branch will be deleted in the near future.
Previous,

// The current format.
Next,
let ty = crate::encoding::encode_world(resolve, world)?;

let world = &resolve.worlds[world];
let mut outer_ty = ComponentType::new();
outer_ty.ty().component(&ty);
outer_ty.export(
&resolve.id_of_name(world.package.unwrap(), &world.name),
ComponentTypeRef::Component(0),
);

let mut builder = ComponentBuilder::default();

let string_encoding = encode_string_encoding(string_encoding);
builder.custom_section(&CustomSection {
name: CUSTOM_SECTION_NAME.into(),
data: Cow::Borrowed(&[CURRENT_VERSION, string_encoding]),
});

let ty = builder.type_component(&outer_ty);
builder.export(&world.name, ComponentExportKind::Type, ty, None);

let mut producers = crate::base_producers();
if let Some(p) = extra_producers {
producers.merge(&p);
}

let format = match use_next_encoding {
Some(true) => EncodingFormat::Next,
Some(false) => EncodingFormat::Previous,
None => match std::env::var("WIT_COMPONENT_NEW_ENCODE") {
Ok(s) if s == "0" => EncodingFormat::Previous,
_ => EncodingFormat::Next,
},
};

let ret = match format {
EncodingFormat::Previous => {
let world = &resolve.worlds[world];
let pkg = &resolve.packages[world.package.unwrap()];
assert!(
resolve
.packages
.iter()
.filter(|(_, p)| p.name == pkg.name)
.count()
== 1
);

let mut ret = Vec::new();
ret.push(0x03);
ret.push(encode_string_encoding(string_encoding));
world.name.encode(&mut ret);
// This appends a wasm binary encoded Component to the ret:
let mut component_builder =
crate::encoding::encode_component(None, resolve, world.package.unwrap())?;

let mut producers = crate::base_producers();
if let Some(p) = extra_producers {
producers.merge(&p);
}
component_builder.raw_custom_section(&producers.raw_custom_section());

ret.extend(component_builder.finish());
ret
}
EncodingFormat::Next => {
let ty = crate::encoding::encode_world(resolve, world)?;

let world = &resolve.worlds[world];
let mut outer_ty = ComponentType::new();
outer_ty.ty().component(&ty);
outer_ty.export(
&resolve.id_of_name(world.package.unwrap(), &world.name),
ComponentTypeRef::Component(0),
);

let mut builder = ComponentBuilder::default();

let string_encoding = encode_string_encoding(string_encoding);
builder.custom_section(&CustomSection {
name: CUSTOM_SECTION_NAME.into(),
data: Cow::Borrowed(&[CURRENT_VERSION, string_encoding]),
});

let ty = builder.type_component(&outer_ty);
builder.export(&world.name, ComponentExportKind::Type, ty, None);

let mut producers = crate::base_producers();
if let Some(p) = extra_producers {
producers.merge(&p);
}
builder.raw_custom_section(&producers.raw_custom_section());
builder.finish()
}
};

Ok(ret)
builder.raw_custom_section(&producers.raw_custom_section());
Ok(builder.finish())
}

fn decode_custom_section(wasm: &[u8]) -> Result<(Resolve, WorldId, StringEncoding)> {
Expand Down
9 changes: 2 additions & 7 deletions crates/wit-component/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,8 @@ fn read_core_module(path: &Path, resolve: &Resolve, pkg: PackageId) -> Result<Ve
let mut producers = wasm_metadata::Producers::empty();
producers.add("processed-by", "my-fake-bindgen", "123.45");

let encoded = wit_component::metadata::encode(
resolve,
world,
StringEncoding::UTF8,
Some(&producers),
Some(true),
)?;
let encoded =
wit_component::metadata::encode(resolve, world, StringEncoding::UTF8, Some(&producers))?;

let section = wasm_encoder::CustomSection {
name: "component-type".into(),
Expand Down

0 comments on commit 802554d

Please sign in to comment.