Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix zero return imports, and support for result types #726

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ wapm.lock
# ignore local builds
checksum-Elixir.Wasmex.Native.exs
*.so

package.json
package-lock.json
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
erlang 27.1.2
elixir 1.17.3-otp-27
erlang 27.2.4
elixir 1.18.2-otp-27
Comment on lines +1 to +2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💜

rust 1.82.0
8 changes: 7 additions & 1 deletion native/wasmex/src/component_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ fn call_elixir_import(
return Err(anyhow::anyhow!("Callback failed"));
}

result_values[0] = returned_values[0].clone();
if !returned_values.is_empty() {
result_values[0] = returned_values[0].clone();
}
Ok(())
}

Expand Down Expand Up @@ -341,6 +343,10 @@ fn populate_return_values(

let wit_type = match results {
wit_parser::Results::Anon(wit_type) => wit_type,
wit_parser::Results::Named(vec) if vec.is_empty() => {
*return_values = Some((true, vals));
return Ok(());
}
Comment on lines +346 to +349
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

wit_parser::Results::Named(_vec) => {
return Err(anyhow!("Named return values not supported"))
}
Expand Down
26 changes: 20 additions & 6 deletions test/component_fixtures/importer/importer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import getSecretWord from 'get-secret-word';
import getNumber from 'get-number';
import getList from 'get-list';
import getPoint from 'get-point';
import getTuple from 'get-tuple';
import getSecretWord from "get-secret-word";
import getNumber from "get-number";
import getList from "get-list";
import getPoint from "get-point";
import getTuple from "get-tuple";
import print from "print";

export function printOrError(msg) {
print(msg);
if (msg === "error") {
throw "error";
}

return msg;
}

export function printSecretWord() {
const secret = getSecretWord(7, "foo");
print(secret);
}
export function revealSecretWord() {
const secretWord = getSecretWord(7, "foo");
const {x, y} = getPoint();
const { x, y } = getPoint();
return `${secretWord} ${getNumber()} ${getList().join()} x: ${x} y: ${y}`;
}

Expand Down
Binary file modified test/component_fixtures/importer/importer.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions test/component_fixtures/importer/importer.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ world importer {
import get-list: func() -> list<string>;
import get-point: func() -> point;
import get-tuple: func() -> tuple<u32, string>;
import print: func(word: string);

export print-or-error: func(msg: string) -> result<string, string>;
export print-secret-word: func();
export reveal-secret-word: func() -> string;
export show-tuple: func() -> string;
}
11 changes: 10 additions & 1 deletion test/components/import_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule Wasmex.Components.ImportTest do
"get-number" => {:fn, fn -> 42 end},
"get-list" => {:fn, fn -> ["hi", "there"] end},
"get-point" => {:fn, fn -> %{x: 1, y: 2} end},
"get-tuple" => {:fn, fn -> {1, "foo"} end}
"get-tuple" => {:fn, fn -> {1, "foo"} end},
"print" => {:fn, fn x -> IO.puts(x) end}
}

component_pid =
Expand All @@ -26,5 +27,13 @@ defmodule Wasmex.Components.ImportTest do

assert {:ok, "1 foo"} =
Wasmex.Components.call_function(component_pid, "show-tuple", [])

assert {:ok, _} = Wasmex.Components.call_function(component_pid, "print-secret-word", [])

assert {:ok, {:ok, "bananas"}} =
Wasmex.Components.call_function(component_pid, "print-or-error", ["bananas"])

assert {:ok, {:error, "error"}} =
Wasmex.Components.call_function(component_pid, "print-or-error", ["error"])
end
end