Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge with upstream #48

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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