Skip to content

Commit

Permalink
Fix I32.cond including unreachable for true catch-all
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jul 11, 2024
1 parent 862885d commit f0321b7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/orb/i32.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ defmodule Orb.I32 do
Orb.IfElse.new(
unquote(match),
Orb.InstructionSequence.new(
unquote(__get_block_items(target)) ++ [Orb.Control.break(:i32_map)]
unquote(__get_block_items(target)) ++ [Orb.Control.break(:i32_cond)]
)
)
end
end
end

has_catchall? = Enum.any?(transform, &match?({:->, _, [[{:_, _, _}], _]}, &1))
has_catchall? = Enum.any?(transform, &match?({:->, _, [[true], _]}, &1))

final_instruction =
case has_catchall? do
Expand All @@ -177,7 +177,7 @@ defmodule Orb.I32 do
with do
require Orb.Control

Orb.Control.block :i32_map, Orb.I32 do
Orb.Control.block :i32_cond, Orb.I32 do
Orb.InstructionSequence.new(unquote(statements))
unquote(final_instruction)
end
Expand Down
81 changes: 81 additions & 0 deletions test/i32/conveniences_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,87 @@ defmodule I32ConveniencesTest do
assert Wasm.call(wat, :is_url_safe?, ?*) == 0
end

test "I32.cond without catch-all" do
assert (TestHelper.module_wat do
use Orb

defw get_path(), I32, state: I32 do
state = 0

I32.cond do
state === 0 -> i32(100)
state === 1 -> i32(200)
end
end
end) == """
(module $Sample
(func $get_path (export "get_path") (result i32)
(local $state i32)
(i32.const 0)
(local.set $state)
(block $i32_cond (result i32)
(i32.eq (local.get $state) (i32.const 0))
(if (result i32)
(then
(i32.const 100)
(br $i32_cond)
)
)
(i32.eq (local.get $state) (i32.const 1))
(if (result i32)
(then
(i32.const 200)
(br $i32_cond)
)
)
unreachable
)
)
)
"""
end

test "I32.cond with catch-all" do
assert (TestHelper.module_wat do
use Orb

defw get_path(), I32, state: I32 do
state = 0

I32.cond do
state === 0 -> i32(100)
state === 1 -> i32(200)
true -> i32(500)
end
end
end) == """
(module $Sample
(func $get_path (export "get_path") (result i32)
(local $state i32)
(i32.const 0)
(local.set $state)
(block $i32_cond (result i32)
(i32.eq (local.get $state) (i32.const 0))
(if (result i32)
(then
(i32.const 100)
(br $i32_cond)
)
)
(i32.eq (local.get $state) (i32.const 1))
(if (result i32)
(then
(i32.const 200)
(br $i32_cond)
)
)
(i32.const 500)
)
)
)
"""
end

test "I32.match output int" do
assert (TestHelper.module_wat do
use Orb
Expand Down

0 comments on commit f0321b7

Please sign in to comment.