WebAssembly通用运行时--Wasmer

码农天地 -
WebAssembly通用运行时--Wasmer

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交互,所以下一篇文章,我们会侧重于这点。

特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。
上一篇: VMware安装OpenWrt

Tags 标签

加个好友,技术交流

1628738909466805.jpg