Skip to content

Commit 371aff5

Browse files
authored
Refactor some printing around component external kinds (#1501)
Minor cleanups noticed during an attempt to implement WebAssembly/component-model#341
1 parent 2231add commit 371aff5

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

crates/wasmprinter/src/lib.rs

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@ impl Printer {
18511851
ComponentTypeDeclaration::Export { name, ty } => {
18521852
self.start_group("export ");
18531853
self.print_component_kind_name(states.last_mut().unwrap(), ty.kind())?;
1854+
self.result.push(' ');
18541855
self.print_str(name.0)?;
18551856
self.result.push(' ');
18561857
self.print_component_import_ty(states.last_mut().unwrap(), &ty, false)?;
@@ -1885,6 +1886,7 @@ impl Printer {
18851886
InstanceTypeDeclaration::Export { name, ty } => {
18861887
self.start_group("export ");
18871888
self.print_component_kind_name(states.last_mut().unwrap(), ty.kind())?;
1889+
self.result.push(' ');
18881890
self.print_str(name.0)?;
18891891
self.result.push(' ');
18901892
self.print_component_import_ty(states.last_mut().unwrap(), &ty, false)?;
@@ -2163,6 +2165,7 @@ impl Printer {
21632165
self.start_group("export ");
21642166
if named {
21652167
self.print_component_kind_name(state, export.kind)?;
2168+
self.result.push(' ');
21662169
}
21672170
self.print_str(export.name.0)?;
21682171
self.result.push(' ');
@@ -2206,7 +2209,30 @@ impl Printer {
22062209
state.component.components += 1;
22072210
}
22082211
}
2209-
self.result.push(' ');
2212+
Ok(())
2213+
}
2214+
2215+
fn start_component_external_kind_group(&mut self, kind: ComponentExternalKind) -> Result<()> {
2216+
match kind {
2217+
ComponentExternalKind::Module => {
2218+
self.start_group("core module ");
2219+
}
2220+
ComponentExternalKind::Component => {
2221+
self.start_group("component ");
2222+
}
2223+
ComponentExternalKind::Instance => {
2224+
self.start_group("instance ");
2225+
}
2226+
ComponentExternalKind::Func => {
2227+
self.start_group("func ");
2228+
}
2229+
ComponentExternalKind::Value => {
2230+
self.start_group("value ");
2231+
}
2232+
ComponentExternalKind::Type => {
2233+
self.start_group("type ");
2234+
}
2235+
}
22102236
Ok(())
22112237
}
22122238

@@ -2216,29 +2242,24 @@ impl Printer {
22162242
kind: ComponentExternalKind,
22172243
index: u32,
22182244
) -> Result<()> {
2245+
self.start_component_external_kind_group(kind)?;
22192246
match kind {
22202247
ComponentExternalKind::Module => {
2221-
self.start_group("core module ");
22222248
self.print_idx(&state.core.module_names, index)?;
22232249
}
22242250
ComponentExternalKind::Component => {
2225-
self.start_group("component ");
22262251
self.print_idx(&state.component.component_names, index)?;
22272252
}
22282253
ComponentExternalKind::Instance => {
2229-
self.start_group("instance ");
22302254
self.print_idx(&state.component.instance_names, index)?;
22312255
}
22322256
ComponentExternalKind::Func => {
2233-
self.start_group("func ");
22342257
self.print_idx(&state.component.func_names, index)?;
22352258
}
22362259
ComponentExternalKind::Value => {
2237-
self.start_group("value ");
22382260
self.print_idx(&state.component.value_names, index)?;
22392261
}
22402262
ComponentExternalKind::Type => {
2241-
self.start_group("type ");
22422263
self.print_idx(&state.component.type_names, index)?;
22432264
}
22442265
}
@@ -2519,50 +2540,9 @@ impl Printer {
25192540
self.result.push(' ');
25202541
self.print_str(name)?;
25212542
self.result.push(' ');
2522-
match kind {
2523-
ComponentExternalKind::Module => {
2524-
self.start_group("core module ");
2525-
self.print_name(&state.core.module_names, state.core.modules)?;
2526-
self.end_group();
2527-
state.core.modules += 1;
2528-
}
2529-
ComponentExternalKind::Component => {
2530-
self.start_group("component ");
2531-
self.print_name(
2532-
&state.component.component_names,
2533-
state.component.components,
2534-
)?;
2535-
self.end_group();
2536-
state.component.components += 1;
2537-
}
2538-
ComponentExternalKind::Instance => {
2539-
self.start_group("instance ");
2540-
self.print_name(
2541-
&state.component.instance_names,
2542-
state.component.instances,
2543-
)?;
2544-
self.end_group();
2545-
state.component.instances += 1;
2546-
}
2547-
ComponentExternalKind::Func => {
2548-
self.start_group("func ");
2549-
self.print_name(&state.component.func_names, state.component.funcs)?;
2550-
self.end_group();
2551-
state.component.funcs += 1;
2552-
}
2553-
ComponentExternalKind::Value => {
2554-
self.start_group("value ");
2555-
self.print_name(&state.component.value_names, state.component.values)?;
2556-
self.end_group();
2557-
state.component.values += 1;
2558-
}
2559-
ComponentExternalKind::Type => {
2560-
self.start_group("type ");
2561-
self.print_name(&state.component.type_names, state.component.types)?;
2562-
self.end_group();
2563-
state.component.types += 1;
2564-
}
2565-
}
2543+
self.start_component_external_kind_group(kind)?;
2544+
self.print_component_kind_name(state, kind)?;
2545+
self.end_group();
25662546

25672547
self.end_group(); // alias export
25682548
}

0 commit comments

Comments
 (0)