diff --git a/.gitignore b/.gitignore index a5c5a3b..41b433c 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ priv/native/libwasmex.so test/wasm_source/target/* test/wasm_link_test/target/* test/wasm_link_dep_test/target/* +test/wasm_link_import_test/target/* .mix_tasks **/.DS_Store diff --git a/native/wasmex/src/instance.rs b/native/wasmex/src/instance.rs index cd1b16d..df9b3cb 100644 --- a/native/wasmex/src/instance.rs +++ b/native/wasmex/src/instance.rs @@ -75,8 +75,8 @@ fn link_and_create_instance( wasmtime_wasi::add_to_linker(&mut linker, |s: &mut StoreData| s.wasi.as_mut().unwrap()) .map_err(|err| Error::Term(Box::new(err.to_string())))?; } - link_modules(&mut linker, store_or_caller, linked_modules)?; link_imports(&mut linker, imports)?; + link_modules(&mut linker, store_or_caller, linked_modules)?; linker .instantiate(store_or_caller, module) .map_err(|err| Error::Term(Box::new(err.to_string()))) diff --git a/test/test_helper.exs b/test/test_helper.exs index bf6623a..3af1029 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -2,6 +2,7 @@ defmodule TestHelper do @wasm_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_test" @wasm_link_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_test" @wasm_link_dep_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_dep_test" + @wasm_link_import_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_link_import_test" @wasm_import_test_source_dir "#{Path.dirname(__ENV__.file)}/wasm_import_test" @wasi_test_source_dir "#{Path.dirname(__ENV__.file)}/wasi_test" @@ -15,6 +16,10 @@ defmodule TestHelper do do: "#{@wasm_link_dep_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_link_dep_test.wasm" + def wasm_link_import_test_file_path, + do: + "#{@wasm_link_import_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_link_import_test.wasm" + def wasm_import_test_file_path, do: "#{@wasm_import_test_source_dir}/target/wasm32-unknown-unknown/debug/wasmex_test.wasm" @@ -24,6 +29,7 @@ defmodule TestHelper do def precompile_wasm_files do {"", 0} = System.cmd("cargo", ["build"], cd: @wasm_test_source_dir) {"", 0} = System.cmd("cargo", ["build"], cd: @wasm_import_test_source_dir) + {"", 0} = System.cmd("cargo", ["build"], cd: @wasm_link_import_test_source_dir) {"", 0} = System.cmd("cargo", ["build"], cd: @wasi_test_source_dir) {"", 0} = @@ -80,6 +86,15 @@ defmodule TestHelper do %{store: store, module: wasm_module} end + def wasm_link_import_module do + {:ok, store} = Wasmex.Store.new() + + {:ok, wasm_module} = + Wasmex.Module.compile(store, File.read!(TestHelper.wasm_link_import_test_file_path())) + + %{store: store, module: wasm_module} + end + def wasm_import_module do {:ok, store} = Wasmex.Store.new() diff --git a/test/wasm_link_import_test/.cargo/config.toml b/test/wasm_link_import_test/.cargo/config.toml new file mode 100644 index 0000000..f4e8c00 --- /dev/null +++ b/test/wasm_link_import_test/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/test/wasm_link_import_test/Cargo.lock b/test/wasm_link_import_test/Cargo.lock new file mode 100644 index 0000000..af12231 --- /dev/null +++ b/test/wasm_link_import_test/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "wasmex_link_import_test" +version = "0.1.0" diff --git a/test/wasm_link_import_test/Cargo.toml b/test/wasm_link_import_test/Cargo.toml new file mode 100644 index 0000000..87089b8 --- /dev/null +++ b/test/wasm_link_import_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "wasmex_link_import_test" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[lib] +crate-type =["cdylib"] diff --git a/test/wasm_link_import_test/src/lib.rs b/test/wasm_link_import_test/src/lib.rs new file mode 100644 index 0000000..8a0e289 --- /dev/null +++ b/test/wasm_link_import_test/src/lib.rs @@ -0,0 +1,9 @@ +extern "C" { + fn imported_sum(a: i32, b: i32) -> i32; +} + +#[no_mangle] +pub extern "C" fn sum(from: i32, to: i32) -> i32 { + let result = unsafe { imported_sum(from, to) }; + result +} diff --git a/test/wasm_link_test.exs b/test/wasm_link_test.exs index 72a1919..0e7f0fd 100644 --- a/test/wasm_link_test.exs +++ b/test/wasm_link_test.exs @@ -68,4 +68,20 @@ defmodule WasmLinkTest do assert Wasmex.call_function(pid, "calc_seq", [1, 5]) == {:ok, [15]} end + + test "linking wasm modules with imports" do + calculator_wasm = File.read!(TestHelper.wasm_link_test_file_path()) + utils_wasm = File.read!(TestHelper.wasm_link_import_test_file_path()) + + imports = %{ + env: %{ + imported_sum: {:fn, [:i32, :i32], [:i32], fn _context, a, b -> a + b end} + } + } + + links = %{utils: %{bytes: utils_wasm}} + {:ok, pid} = Wasmex.start_link(%{bytes: calculator_wasm, links: links, imports: imports}) + + assert Wasmex.call_function(pid, "sum_range", [1, 5]) == {:ok, [15]} + end end