Skip to content

Commit

Permalink
wasmprinter folding: change some panics to errors or successful round…
Browse files Browse the repository at this point in the history
…trips (bytecodealliance#1840)

* wasmprinter folding: convert some panics to errors

* wasmprinter: roundtrip some unfoldable-but-valid `if` instructions
  • Loading branch information
keithw authored Oct 2, 2024
1 parent d4532cb commit d7f2827
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
11 changes: 7 additions & 4 deletions crates/wasmprinter/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1699,13 +1699,16 @@ impl<'printer, 'state, 'a, 'b> PrintOperatorFolded<'printer, 'state, 'a, 'b> {
}

fn push_if(&mut self, ty: BlockType, plain: String) -> Result<()> {
let mut predicate = vec![self
let mut predicate = Vec::new();
if let Some(phrase) = self
.control
.last_mut()
.expect("no enclosing block")
.ok_or_else(|| anyhow!("no enclosing block"))?
.folded
.pop()
.expect("no predicate")];
{
predicate.push(phrase)
}
if let Some(hint) = self.branch_hint.take() {
predicate.push(hint);
}
Expand Down Expand Up @@ -1826,7 +1829,7 @@ impl<'printer, 'state, 'a, 'b> PrintOperatorFolded<'printer, 'state, 'a, 'b> {

self.control
.last_mut()
.expect("end without outer block")
.ok_or_else(|| anyhow!("end without outer block"))?
.folded
.push(inst);
Ok(())
Expand Down
9 changes: 9 additions & 0 deletions tests/local/folding/fold-unfoldable-if.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(module
(func
i32.const 0
try_table (param i32)
if
end
end
)
)
11 changes: 11 additions & 0 deletions tests/snapshots/local/folding/fold-unfoldable-if.wat.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(module
(type (;0;) (func))
(type (;1;) (func (param i32)))
(func (;0;) (type 0)
i32.const 0
try_table (type 1) (param i32) ;; label = @1
if ;; label = @2
end
end
)
)
10 changes: 10 additions & 0 deletions tests/snapshots/local/folding/fold-unfoldable-if.wat.print-folded
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(module
(type (;0;) (func))
(type (;1;) (func (param i32)))
(func (;0;) (type 0)
(i32.const 0)
(try_table (type 1) (param i32) ;; label = @1
(if ;; label = @2
(then)))
)
)

0 comments on commit d7f2827

Please sign in to comment.