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 | shInstalling Wasmer and WAPM!               ww               wwwww        ww     wwwwww  w        wwwww      wwwwwwwwwww      wwwwww  w     wwwwwwwwwwww      wwwwwwwwww   wwwwwwwwwww  w      wwwwwww  wwwwwwwwwwwwwwwwwww   wwwww  wwwwwwwwwwwwwwwwwwww  wwwww  wwwwwwwwwwwwwwwwwwww  wwwww  wwwwwwwwwwwwwwwwwwww  wwwww  wwwwwwwwwwwwwwwwwwww  wwwww   wwwwwwwwwwwwwwwwwww  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/.zshrcIf you have a different profile please add the following:# Wasmerexport 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命令检查一下:

wasmererror: 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-worldcd 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.wasmHello, world!

总结

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