@@ -33,6 +33,144 @@ impl Opts {
3333
3434/// Read metadata (module name, producers) from a WebAssembly file.
3535#[ derive( clap:: Parser ) ]
36+ #[ clap( after_help = "\
37+ Examples:
38+
39+ Suppose foo.wasm has the following textual representation:
40+
41+ (component $my-name
42+ (core module $submodule)
43+
44+ (core module (@name \" another submodule\" ))
45+ )
46+
47+ This Wasm file represents a component containing two core modules.
48+
49+ # Show the metadata for foo.wasm in a tabular format.
50+ $ wasm-tools metadata show foo.wasm
51+
52+ ╭───────────┬───────────────────┬──────┬───────┬───────────┬─────────╮
53+ │ KIND ┆ NAME ┆ SIZE ┆ SIZE% ┆ LANGUAGES ┆ PARENT │
54+ ╞═══════════╪═══════════════════╪══════╪═══════╪═══════════╪═════════╡
55+ │ component ┆ my-name ┆ 136B ┆ 100% ┆ - ┆ <root> │
56+ ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
57+ │ module ┆ submodule ┆ 27B ┆ 20% ┆ - ┆ my-name │
58+ ├╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
59+ │ module ┆ another submodule ┆ 35B ┆ 26% ┆ - ┆ my-name │
60+ ╰───────────┴───────────────────┴──────┴───────┴───────────┴─────────╯
61+ ╭───────┬────────────────────────────╮
62+ │ KIND ┆ VALUE │
63+ ╞═══════╪════════════════════════════╡
64+ │ name ┆ my-name │
65+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
66+ │ kind ┆ component │
67+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
68+ │ range ┆ 0x0..0x88 │
69+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
70+ │ child ┆ submodule [module] │
71+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
72+ │ child ┆ another submodule [module] │
73+ ╰───────┴────────────────────────────╯
74+ ╭───────┬───────────╮
75+ │ KIND ┆ VALUE │
76+ ╞═══════╪═══════════╡
77+ │ name ┆ submodule │
78+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
79+ │ kind ┆ module │
80+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┤
81+ │ range ┆ 0xa..0x25 │
82+ ╰───────┴───────────╯
83+ ╭───────┬───────────────────╮
84+ │ KIND ┆ VALUE │
85+ ╞═══════╪═══════════════════╡
86+ │ name ┆ another submodule │
87+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
88+ │ kind ┆ module │
89+ ├╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
90+ │ range ┆ 0x27..0x4a │
91+ ╰───────┴───────────────────╯
92+
93+ The first table summarizes the contents of the file: a component and two
94+ core modules.
95+
96+ Next, one table is shown for each component and module. The first table
97+ shows that a component named `my-name` is represented by the bytes in
98+ foo.wasm between the offsets 0x0 and 0x88. The \" child\" rows of the table
99+ show the names of each of its contained modules. The second two tables
100+ show the names, kinds, and ranges of each of the modules in the file.
101+
102+ # Show the metadata for foo.wasm in JSON format.
103+ # The output is not pretty-printed. For clarity, it has been pretty-printed
104+ # here.
105+ $ wasm-tools metadata show foo.wasm --json
106+ {
107+ \" component\" : {
108+ \" metadata\" : {
109+ \" name\" : \" my-name\" ,
110+ \" producers\" : null,
111+ \" authors\" : null,
112+ \" description\" : null,
113+ \" licenses\" : null,
114+ \" source\" : null,
115+ \" homepage\" : null,
116+ \" revision\" : null,
117+ \" version\" : null,
118+ \" range\" : {
119+ \" start\" : 0,
120+ \" end\" : 136
121+ },
122+ \" dependencies\" : null
123+ },
124+ \" children\" : [
125+ {
126+ \" module\" : {
127+ \" name\" : \" submodule\" ,
128+ \" producers\" : null,
129+ \" authors\" : null,
130+ \" description\" : null,
131+ \" licenses\" : null,
132+ \" source\" : null,
133+ \" homepage\" : null,
134+ \" revision\" : null,
135+ \" version\" : null,
136+ \" range\" : {
137+ \" start\" : 10,
138+ \" end\" : 37
139+ },
140+ \" dependencies\" : null
141+ }
142+ },
143+ {
144+ \" module\" : {
145+ \" name\" : \" another submodule\" ,
146+ \" producers\" : null,
147+ \" authors\" : null,
148+ \" description\" : null,
149+ \" licenses\" : null,
150+ \" source\" : null,
151+ \" homepage\" : null,
152+ \" revision\" : null,
153+ \" version\" : null,
154+ \" range\" : {
155+ \" start\" : 39,
156+ \" end\" : 74
157+ },
158+ \" dependencies\" : null
159+ }
160+ }
161+ ]
162+ }
163+ }
164+
165+ The JSON format contains some additional fields. In this example, the values of
166+ those fields are set to `null` because they are not present in the input file.
167+ In the tabular format, these fields were simply omitted.
168+ The offset ranges are also shown in decimal instead of hexadecimal.
169+
170+ Exit status:
171+ 0 on success,
172+ nonzero if the input file fails to parse.
173+ " ) ]
36174pub struct ShowOpts {
37175 #[ clap( flatten) ]
38176 io : wasm_tools:: InputOutput ,
@@ -64,6 +202,47 @@ impl ShowOpts {
64202
65203/// Add metadata (module name, producers) to a WebAssembly file
66204#[ derive( clap:: Parser ) ]
205+ #[ clap( after_help = "\
206+ Examples:
207+
208+ Suppose foo.wasm has the following textual representation:
209+
210+ (component $foo
211+ (core module
212+ (func $foo)
213+ )
214+ )
215+
216+ This Wasm file represents a component containing one core module.
217+
218+ # Add a programming language \" foo\" whose version is 1
219+ # to the `producers` section, an \" authors\" string,
220+ # and a homepage URL and print the textual representation
221+ # to stdout
222+ $ wasm-tools metadata add --language foo=1 \\
223+ --authors \" J. Random Hacker\" \\
224+ --homepage \" https://example.com/\" \\
225+ foo.wasm -t
226+ (component $foo
227+ (core module (;0;)
228+ (type (;0;) (func))
229+ (func $foo (;0;) (type 0))
230+ )
231+ (@producers
232+ (language \" foo\" \" 1\" )
233+ )
234+ (@custom \" authors\" \" J. Random Hacker\" )
235+ (@custom \" homepage\" \" https://example.com/\" )
236+ )
237+
238+ # Remove the homepage section from the file foo1.wasm
239+ # and write the binary output to foo2.wasm
240+ $ wasm-tools metadata add --clear-homepage foo1.wasm -o foo2.wasm
241+
242+ Exit status:
243+ 0 on success,
244+ nonzero if the input file fails to parse.
245+ " ) ]
67246pub struct AddOpts {
68247 #[ clap( flatten) ]
69248 generate_dwarf : wasm_tools:: GenerateDwarfArg ,
0 commit comments