From 1a0fe0ba4bbb39ada65011c65364b7d379a21cbf Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Sat, 2 Dec 2023 20:42:59 +1100 Subject: [PATCH] Allow if/else with different type of effects to be created --- lib/orb/ops.ex | 1 + test/if_else_test.exs | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/lib/orb/ops.ex b/lib/orb/ops.ex index e5ad96c..170b210 100644 --- a/lib/orb/ops.ex +++ b/lib/orb/ops.ex @@ -76,6 +76,7 @@ defmodule Orb.Ops do def extract_common_type(a, b) do case {typeof(a, :primitive), typeof(b, :primitive)} do {same, same} -> same + {a, b} when is_effect(a) and is_effect(b) -> :unknown_effect {type, Elixir.Integer} when type in @integer_types -> type {Elixir.Integer, type} when type in @integer_types -> type {type, Elixir.Float} when type in @float_types -> type diff --git a/test/if_else_test.exs b/test/if_else_test.exs index 677095b..89d04a4 100644 --- a/test/if_else_test.exs +++ b/test/if_else_test.exs @@ -225,4 +225,67 @@ defmodule IfElseTest do Instance.write_string_nul_terminated(inst, 0x00100, "&a=1&&&b=2") assert count.(0x00100) == 2 end + + test "local and unknown effect" do + defmodule URLSearchParams2 do + use Orb + + Memory.pages(1) + + defmodule Log do + use Orb.Import + + defw(found_alphanumeric(), nil) + end + + importw(Log, :log) + + defw url_search_params_count(url_params: I32.U8.UnsafePointer), I32, + char: I32.U8, + count: I32, + pair_char_len: I32 do + loop EachByte do + char = url_params[at!: 0] + + if I32.in?(char, [?&, ?\0]) do + count = count + (pair_char_len > 0) + pair_char_len = 0 + else + pair_char_len = pair_char_len + 1 + Log.found_alphanumeric() + end + + url_params = url_params + 1 + + EachByte.continue(if: char) + end + + count + end + end + + alias OrbWasmtime.Instance + inst = Instance.run(URLSearchParams2, [ + {:log, :found_alphanumeric, fn -> nil end} + ]) + count = Instance.capture(inst, :url_search_params_count, 1) + + Instance.write_string_nul_terminated(inst, 0x00100, "") + assert count.(0x00100) == 0 + + Instance.write_string_nul_terminated(inst, 0x00100, "a=1") + assert count.(0x00100) == 1 + + Instance.write_string_nul_terminated(inst, 0x00100, "a=1&b=1") + assert count.(0x00100) == 2 + + Instance.write_string_nul_terminated(inst, 0x00100, "a=1&") + assert count.(0x00100) == 1 + + Instance.write_string_nul_terminated(inst, 0x00100, "&a=1&") + assert count.(0x00100) == 1 + + Instance.write_string_nul_terminated(inst, 0x00100, "&a=1&&&b=2") + assert count.(0x00100) == 2 + end end