从 Elixir 执行 WebAssembly
Wasmex 是一个快速、安全的 Elixir WebAssembly 和 WASI 运行时。它使得轻量级的 WebAssembly 容器能够在 Elixir 后端中运行。
test/fixture_projects/wasm_test/src/lib.rs 中有一个名为 WASM 的程序,它是用 Rust 编写的(但也可以是编译为 WebAssembly 的任何其他语言)。
它定义了我们用于端到端测试的许多函数,同时也作为示例代码。例如:
#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
x + y
}一旦将此程序编译为 WebAssembly (每次运行测试时都会发生这种情况),
我们得到一个名为 wasmex_test.WASM 的二进制文件。
此 WASM 文件可以在 Elixir 中执行:
bytes =
File.read!(
"test/fixture_projects/wasm_test/target/wasm32-unknown-unknown/debug/wasmex_test.wasm"
)
{:ok, pid} = Wasmex.start_link(%{bytes: bytes}) # starts a GenServer running a Wasm instance
{:ok, [42]} == Wasmex.call_function(pid, "sum", [50, -8])引用 WebAssembly 站点:
WebAssembly(简称为 WASM)是一种基于堆栈的虚拟机的二进制指令格式。WASM 设计为高级语言(如 C/C++/Rust)编译的可移植目标,可在 Web 上部署客户端和服务器应用程序。
暂无开放 Issues,或尚未同步最近议题。