Skip to content

Commit 1842fb6

Browse files
authored
feature: Output wasm custom section (bytecodealliance#1879)
* feature: Output wasm custom section Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * chore: moving to a separate command Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * chore: move under `embed` command Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * chore: Addressing PR comments Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * fix: oops. changed wrong output_wasm Signed-off-by: Lucas Fontes <lucas@cosmonic.com> --------- Signed-off-by: Lucas Fontes <lucas@cosmonic.com>
1 parent 537ca81 commit 1842fb6

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/bin/wasm-tools/component.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use wasmparser::types::{CoreTypeId, EntityType, Types};
1313
use wasmparser::{Payload, ValidPayload};
1414
use wat::Detect;
1515
use wit_component::{
16-
embed_component_metadata, ComponentEncoder, DecodedWasm, Linker, StringEncoding, WitPrinter,
16+
embed_component_metadata, metadata, ComponentEncoder, DecodedWasm, Linker, StringEncoding,
17+
WitPrinter,
1718
};
1819
use wit_parser::{Mangling, PackageId, Resolve};
1920

@@ -308,6 +309,15 @@ pub struct EmbedOpts {
308309
/// Print the output in the WebAssembly text format instead of binary.
309310
#[clap(long, short = 't')]
310311
wat: bool,
312+
313+
/// Print the wasm custom section only.
314+
#[clap(
315+
long,
316+
conflicts_with = "wat",
317+
conflicts_with = "dummy",
318+
conflicts_with = "dummy_names"
319+
)]
320+
only_custom: bool,
311321
}
312322

313323
impl EmbedOpts {
@@ -318,7 +328,21 @@ impl EmbedOpts {
318328
/// Executes the application.
319329
fn run(self) -> Result<()> {
320330
let (resolve, pkg_id) = self.resolve.load()?;
331+
321332
let world = resolve.select_world(pkg_id, self.world.as_deref())?;
333+
334+
if self.only_custom {
335+
let encoded = metadata::encode(
336+
&resolve,
337+
world,
338+
self.encoding.unwrap_or(StringEncoding::UTF8),
339+
None,
340+
)?;
341+
342+
self.io.output_wasm(&encoded, false)?;
343+
return Ok(());
344+
}
345+
322346
let mut wasm = if self.dummy {
323347
wit_component::dummy_module(&resolve, world, Mangling::Standard32)
324348
} else if let Some(mangling) = self.dummy_names {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: component embed --only-custom -w foo % | print
2+
3+
package a:b;
4+
5+
world foo {
6+
import x: func();
7+
export y: func();
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(component
2+
(@custom "wit-component-encoding" "/04/00")
3+
(type (;0;)
4+
(component
5+
(type (;0;)
6+
(component
7+
(type (;0;) (func))
8+
(import "x" (func (;0;) (type 0)))
9+
(export (;1;) "y" (func (type 0)))
10+
)
11+
)
12+
(export (;0;) "a:b/foo" (component (type 0)))
13+
)
14+
)
15+
(export (;1;) "foo" (type 0))
16+
(@producers
17+
(processed-by "wit-component" "0.219.1")
18+
)
19+
)

0 commit comments

Comments
 (0)