Repository: bytecodealliance/wasm-tools
Pull request #1893 (2024-11-04 17:52:22)

Improve `json-from-wast -h` output
Don't render a long variable name placeholder as
`ALLOW_CONFUSING_UNICODE` and instead render it just as `ALLOW`.


Patch:
diff --git a/src/bin/wasm-tools/component.rs b/src/bin/wasm-tools/component.rs
index 701b9a73..9a1b4cd7 100644
--- a/src/bin/wasm-tools/component.rs
+++ b/src/bin/wasm-tools/component.rs
@@ -13,7 +13,8 @@ use wasmparser::types::{CoreTypeId, EntityType, Types};
 use wasmparser::{Payload, ValidPayload};
 use wat::Detect;
 use wit_component::{
-    embed_component_metadata, ComponentEncoder, DecodedWasm, Linker, StringEncoding, WitPrinter,
+    embed_component_metadata, metadata, ComponentEncoder, DecodedWasm, Linker, StringEncoding,
+    WitPrinter,
 };
 use wit_parser::{Mangling, PackageId, Resolve};
 
@@ -308,6 +309,15 @@ pub struct EmbedOpts {
     /// Print the output in the WebAssembly text format instead of binary.
     #[clap(long, short = 't')]
     wat: bool,
+
+    /// Print the wasm custom section only.
+    #[clap(
+        long,
+        conflicts_with = "wat",
+        conflicts_with = "dummy",
+        conflicts_with = "dummy_names"
+    )]
+    only_custom: bool,
 }
 
 impl EmbedOpts {
@@ -318,7 +328,21 @@ impl EmbedOpts {
     /// Executes the application.
     fn run(self) -> Result<()> {
         let (resolve, pkg_id) = self.resolve.load()?;
+
         let world = resolve.select_world(pkg_id, self.world.as_deref())?;
+
+        if self.only_custom {
+            let encoded = metadata::encode(
+                &resolve,
+                world,
+                self.encoding.unwrap_or(StringEncoding::UTF8),
+                None,
+            )?;
+
+            self.io.output_wasm(&encoded, false)?;
+            return Ok(());
+        }
+
         let mut wasm = if self.dummy {
             wit_component::dummy_module(&resolve, world, Mangling::Standard32)
         } else if let Some(mangling) = self.dummy_names {
diff --git a/src/bin/wasm-tools/json_from_wast.rs b/src/bin/wasm-tools/json_from_wast.rs
index 67ffa91b..20208f09 100644
--- a/src/bin/wasm-tools/json_from_wast.rs
+++ b/src/bin/wasm-tools/json_from_wast.rs
@@ -54,7 +54,7 @@ pub struct Opts {
     ///
     /// This is defaulted to `true` to enable parsing all upstream spec tests
     /// but can be disabled if desired too.
-    #[clap(long)]
+    #[clap(long, value_name = "true|false")]
     allow_confusing_unicode: Option<bool>,
 }
 


Test patch:
diff --git a/tests/cli/component-embed-only-custom.wit b/tests/cli/component-embed-only-custom.wit
new file mode 100644
index 00000000..c6eadbcf
--- /dev/null
+++ b/tests/cli/component-embed-only-custom.wit
@@ -0,0 +1,8 @@
+// RUN: component embed --only-custom -w foo % | print
+
+package a:b;
+
+world foo {
+  import x: func();
+  export y: func();
+}
diff --git a/tests/cli/component-embed-only-custom.wit.stdout b/tests/cli/component-embed-only-custom.wit.stdout
new file mode 100644
index 00000000..9efa7479
--- /dev/null
+++ b/tests/cli/component-embed-only-custom.wit.stdout
@@ -0,0 +1,19 @@
+(component
+  (@custom "wit-component-encoding" "/04/00")
+  (type (;0;)
+    (component
+      (type (;0;)
+        (component
+          (type (;0;) (func))
+          (import "x" (func (;0;) (type 0)))
+          (export (;1;) "y" (func (type 0)))
+        )
+      )
+      (export (;0;) "a:b/foo" (component (type 0)))
+    )
+  )
+  (export (;1;) "foo" (type 0))
+  (@producers
+    (processed-by "wit-component" "0.219.1")
+  )
+)