Skip to content

Commit

Permalink
wasm-metadata: allow languages to be versioned (bytecodealliance#1926)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts authored Dec 3, 2024
1 parent 549c283 commit c2fdb5c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions crates/wasm-metadata/src/add_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct AddMetadata {
pub name: Option<String>,

/// Add a programming language to the producers section
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
pub language: Vec<String>,
#[cfg_attr(feature = "clap", clap(long, value_parser = parse_key_value, value_name = "NAME=VERSION"))]
pub language: Vec<(String, String)>,

/// Add a tool and its version to the producers section
#[cfg_attr(feature = "clap", clap(long = "processed-by", value_parser = parse_key_value, value_name="NAME=VERSION"))]
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-metadata/src/producers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ impl Producers {
/// Construct the fields specified by [`AddMetadata`]
pub(crate) fn from_meta(add: &AddMetadata) -> Self {
let mut s = Self::empty();
for lang in add.language.iter() {
s.add("language", &lang, "");
for (lang, version) in add.language.iter() {
s.add("language", &lang, &version);
}
for (name, version) in add.processed_by.iter() {
s.add("processed-by", &name, &version);
Expand Down
18 changes: 12 additions & 6 deletions crates/wasm-metadata/tests/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn add_to_empty_component() {
let component = Component::new().finish();
let add = AddMetadata {
name: Some("foo".to_owned()),
language: vec!["bar".to_owned()],
language: vec![("bar".to_owned(), "1.0".to_owned())],
processed_by: vec![("baz".to_owned(), "1.0".to_owned())],
sdk: vec![],
registry_metadata: Some(RegistryMetadata {
Expand Down Expand Up @@ -48,7 +48,10 @@ fn add_to_empty_component() {
assert!(children.is_empty());
assert_eq!(name, Some("foo".to_owned()));
let producers = producers.expect("some producers");
assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
Expand Down Expand Up @@ -96,7 +99,7 @@ fn add_to_empty_component() {
);

assert_eq!(range.start, 0);
assert_eq!(range.end, 432);
assert_eq!(range.end, 435);
}
_ => panic!("metadata should be component"),
}
Expand All @@ -108,7 +111,7 @@ fn add_to_nested_component() {
let module = Module::new().finish();
let add = AddMetadata {
name: Some("foo".to_owned()),
language: vec!["bar".to_owned()],
language: vec![("bar".to_owned(), "1.0".to_owned())],
processed_by: vec![("baz".to_owned(), "1.0".to_owned())],
sdk: vec![],
registry_metadata: Some(RegistryMetadata {
Expand Down Expand Up @@ -161,7 +164,10 @@ fn add_to_nested_component() {
} => {
assert_eq!(name, &Some("foo".to_owned()));
let producers = producers.as_ref().expect("some producers");
assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
Expand All @@ -174,7 +180,7 @@ fn add_to_nested_component() {
);

assert_eq!(range.start, 10);
assert_eq!(range.end, 120);
assert_eq!(range.end, 123);
}
_ => panic!("child is a module"),
}
Expand Down
9 changes: 6 additions & 3 deletions crates/wasm-metadata/tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn add_to_empty_module() {
let module = Module::new().finish();
let add = AddMetadata {
name: Some("foo".to_owned()),
language: vec!["bar".to_owned()],
language: vec![("bar".to_owned(), "1.0".to_owned())],
processed_by: vec![("baz".to_owned(), "1.0".to_owned())],
sdk: vec![],
registry_metadata: Some(RegistryMetadata {
Expand Down Expand Up @@ -46,7 +46,10 @@ fn add_to_empty_module() {
} => {
assert_eq!(name, Some("foo".to_owned()));
let producers = producers.expect("some producers");
assert_eq!(producers.get("language").unwrap().get("bar").unwrap(), "");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
Expand Down Expand Up @@ -94,7 +97,7 @@ fn add_to_empty_module() {
);

assert_eq!(range.start, 0);
assert_eq!(range.end, 422);
assert_eq!(range.end, 425);
}
_ => panic!("metadata should be module"),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/add-metadata-merge-sections.wat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
;; RUN: metadata add --language foo % | metadata add --language bar | metadata add --sdk foo=2 | metadata show
;; RUN: metadata add --language foo=3 % | metadata add --language bar=1 | metadata add --sdk foo=2 | metadata show
(module)
4 changes: 2 additions & 2 deletions tests/cli/add-metadata-merge-sections.wat.stdout
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module:
language:
foo
bar
foo: 3
bar: 1
sdk:
foo: 2
2 changes: 1 addition & 1 deletion tests/cli/add-metadata.wat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
;; RUN: metadata add % --name foo --language bar --processed-by baz=1 --sdk my-sdk=2 | metadata show
;; RUN: metadata add % --name foo --language bar=1 --processed-by baz=1 --sdk my-sdk=2 | metadata show
(module)
2 changes: 1 addition & 1 deletion tests/cli/add-metadata.wat.stdout
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module foo:
language:
bar
bar: 1
processed-by:
baz: 1
sdk:
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/metadata-add-component.wat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;; RUN: metadata add --language foo % | metadata show
;; RUN: metadata add --language foo=1 % | metadata show
(component $foo
(core module
(func $foo)
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/metadata-add-component.wat.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
component foo:
language:
foo
foo: 1
module:

0 comments on commit c2fdb5c

Please sign in to comment.