关于webassembly:WebAssembly通用运行时Wasmer

9次阅读

共计 2613 个字符,预计需要花费 7 分钟才能阅读完成。

Wasmer 反对基于 WebAssembly 的超轻量级容器,该容器能够在任何中央运行:从台式机到云和 IoT 设施,还能够以任何编程语言嵌入。

通过设计,WebAssembly 模块运行所在的环境与根底主机零碎的本机性能齐全隔离(或沙盒化)。这意味着默认状况下,Wasm 模块被设计为仅执行纯计算。

因而,通常无奈从 WASM 拜访“OS”级资源,例如文件描述符,网络套接字,零碎时钟和随机数。然而,在许多状况下,Wasm 模块须要执行的工作不仅仅是执行纯计算。它们必须与本机“OS”性能交互。

Wasmer 旨在提供三个要害性能:

  • 使程序可能以任何编程语言运行
  • 可移植的二进制文件可能在 Wasmer 反对的任何“OS”(例如 Linux,macOS,Windows 和 FreeBSD)上运行,且无需批改。
  • 充当 Wasm 模块通过诸如 WASIEmscripten之类的 ABI 与本机“OS”性能交互的平安桥。

特点

  • 疾速平安。Wasmer 在齐全沙盒化的环境中以靠近天然的速度运行 WebAssembly。
  • 可插拔。Wasmer 反对不同的编译框架以最适宜您的需要(LLVM,Cranelift …)。
  • 通用。您能够在简直任何平台(macOS,Linux 和 Windows)和芯片组上运行 Wasmer。
  • 规范。运行时通过了反对 WASI 和 Emscripten 的官网 WebAssembly 测试套件。

Wasmer 目前 release 版本为 0.17.1,不过 1.0.0-alpha02.0 版本曾经收回来了,基本上筹备生产就绪了。

生态

比照 wasmtime,其生态更加欠缺。提供了 wapm 和 WebAssembly.sh。

wapm 是 WebAssembly 包管理器。

WebAssembly shell 程序是一个在线 shell 程序,您能够在其中拖放 WebAssembly 模块以进行尝试,还能够执行 WAPM 中可用的 WASI 模块的所有命令。

反对的语言

Wasmer 运行时可用作嵌入不同语言的库,因而您能够在任何中央应用 WebAssembly。

目前反对以下语言:

装置

官网提供了装置脚本,整个装置比较简单:

curl https://get.wasmer.io -sSfL | sh
Installing Wasmer and WAPM!

               ww
               wwwww
        ww     wwwwww  w
        wwwww      wwwwwwwww
ww      wwwwww  w     wwwwwww
wwwww      wwwwwwwwww   wwwww
wwwwww  w      wwwwwww  wwwww
wwwwwwwwwwwwww   wwwww  wwwww
wwwwwwwwwwwwwww  wwwww  wwwww
wwwwwwwwwwwwwww  wwwww  wwwww
wwwwwwwwwwwwwww  wwwww  wwwww
wwwwwwwwwwwwwww  wwwww   wwww
wwwwwwwwwwwwwww  wwwww
   wwwwwwwwwwww   wwww
       wwwwwwww
           wwww

> Getting wasmer releases... ✓
> Downloading 0.17.1 release...
######################################################################## 100.0%-=#=#   #   #                                                   > Downloading 0.17.1 release... ✓####################################### 100.0%
> Unpacking contents... ✓
> Adding to bash profile... ✓
Note: We've added the following to your /Users/iyacontrol/.zshrc
If you have a different profile please add the following:

# Wasmer
export WASMER_DIR="/Users/iyacontrol/.wasmer"
[-s "$WASMER_DIR/wasmer.sh"] && source "$WASMER_DIR/wasmer.sh"

> Successfully installed wasmer 0.17.1!
wasmer & wapm will be available the next time you open the terminal.
If you want to have the commands available now please execute:
source /Users/iyacontrol/.wasmer/wasmer.sh

装置实现,通过执行 wasmer 命令检查一下:

wasmer
error: The following required arguments were not provided:
    <path>

USAGE:
    wasmer <path> --backend <backend>

For more information try --help

如果须要装置指定版本的 wasmer,能够通过如下的形式装置:

curl https://get.wasmer.io -sSfL | sh -s v0.17.0

Demo

rust 和 c /c++ 是目前反对 wasm 比拟好的语言,所以本 demo 应用 rust 实现。

1:应用 cargo 新建 hello-world 工程:

cargo new hello-world
cd hello-world

3:编写 main.rs 文件:

fn main() {println!("Hello, world!");
}

4:编译:

cargo  build --target wasm32-wasi --release
   Compiling hello v0.1.0 (/Users/iyacontrol/rust/hello-world)
    Finished release [optimized] target(s) in 1.01s

自己 rust 版本为最新版 1.46。曾经反对 wasm32-wasi 类型。默认状况下,没有装置该 target 反对,所以须要运行以下 命令:

rustup target add wasm32-wasi

5:执行

咱们侧重点在 wasi 的执行,所以应用 wasmer 运行时,去执行编译好的产物。

wasmer target/wasm32-wasi/release/hello-world.wasm
Hello, world!

总结

本文简略从个性,生态等方面介绍了 wasmer。demo 示例属于纯计算案例,wasi 的更多意义在于和主机 os 交互,所以下一篇文章,咱们会侧重于这点。

正文完
 0