快速开始

WasmEdge 最简单的使用方式是通过 WasmEdge CLI。 开发者们能使用这个命令行工具来运行我们的 WebAssembly 和 JavaScript 示例程序。 之后,我们也可以使用该工具来创建新的 WasmEdge 程序,并将这些程序部署到不同的应用或者框架中运行。

安装

你可以使用以下的单行命令来安装 WasmEdge。 你的系统必须预先安装 git 和 curl。

1. curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash

如果你正在使用Windows 10,你可以使用Windows Package Manager Client (也称winget.exe)来安装WasmEdge。

1. winget install wasmedge

如果你希望一并安装 Tensorflow 和图像处理扩展,请执行以下命令。它将尝试在你的系统上安装 Tensorflow 和图像共享库。

1. curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- -e all

执行以下命令能使已安装的二进制文件在当前会话中可用。

1. source $HOME/.wasmedge/env

使用 Docker 进行安装

如果你使用的是 Docker,你可以直接运行 WasmEdge 应用开发镜像(x86 和 arm64)。这些镜像里包含快速开发 WasmEdge 所需的所有工具。

1. $ docker pull wasmedge/appdev_x86_64:0.9.0
2. $ docker run --rm -v $(pwd):/app -it wasmedge/appdev_x86_64:0.9.0
3. (docker) #

WebAssembly 示例

这里有几个 WebAssembly 字节码的示例供您试用新安装的 WasmEdge CLI。

Hello world

hello.wasm 这个 WebAssembly 程序中包含一个 main() 函数。 查看该程序的 Rust 源码项目。 它将打印 hello,以及所有的命令行参数。

1. $ wasmedge hello.wasm second state
2. hello
3. second
4. state

调用一个 Rust 函数

add.wasm 这个 WebAssembly 程序包含一个 add() 函数。 查看该程序的 Rust 源码项目。 我们在反应器模式下使用 WasmEdge 来调用 add(),并给它 2 个整数作为输入参数。

1. $ wasmedge --reactor add.wasm add 2 2
2. 4

调用一个 WAT 函数

我们手动创建了 fibonacci.wat 程序,并使用了 wat2wasm 编译器来构建 fibonacci.wasm 这个 WebAssembly 程序。 它包含了一个 fib() 函数,这个函数以一个整数作为输入参数。我们在反应器模式下使用 WasmEdge 来调用这个导出函数。

1. $ wasmedge --reactor fibonacci.wasm fib 10
2. 89

开启统计

CLI工具支持 --enable-all-statistics 标志,用于开启统计和 gas meter 的相关配置。

1. $ wasmedge --enable-all-statistics hello.wasm second state
2. hello
3. second
4. state
5. [2021-12-09 16:03:33.261] [info] ====================  Statistics  ====================
6. [2021-12-09 16:03:33.261] [info]  Total execution time: 268266 ns
7. [2021-12-09 16:03:33.261] [info]  Wasm instructions execution time: 251610 ns
8. [2021-12-09 16:03:33.261] [info]  Host functions execution time: 16656 ns
9. [2021-12-09 16:03:33.261] [info]  Executed wasm instructions count: 20425
10. [2021-12-09 16:03:33.261] [info]  Gas costs: 20425
11. [2021-12-09 16:03:33.261] [info]  Instructions per second: 81177218
12. [2021-12-09 16:03:33.261] [info] =======================   End   ======================

开启 gas-limit

CLI工具支持 --gas-limit 标志,用于控制执行的成本。

1. # cd <path/to/WasmEdge>
2. $ cd examples/wasm
3. # gas 足够时
4. $ wasmedge --enable-all-statistics --gas-limit 20425 hello.wasm second state
5. hello
6. second
7. state
8. [2021-12-09 16:03:33.261] [info] ====================  Statistics  ====================
9. [2021-12-09 16:03:33.261] [info]  Total execution time: 268266 ns
10. [2021-12-09 16:03:33.261] [info]  Wasm instructions execution time: 251610 ns
11. [2021-12-09 16:03:33.261] [info]  Host functions execution time: 16656 ns
12. [2021-12-09 16:03:33.261] [info]  Executed wasm instructions count: 20425
13. [2021-12-09 16:03:33.261] [info]  Gas costs: 20425
14. [2021-12-09 16:03:33.261] [info]  Instructions per second: 81177218
15. [2021-12-09 16:03:33.261] [info] =======================   End   ======================

17. # gas 不足时
18. $ wasmedge --enable-all-statistics --gas-limit 20 hello.wasm second state
19. [2021-12-23 15:19:06.690] [error] Cost exceeded limit. Force terminate the execution.
20. [2021-12-23 15:19:06.690] [error]     In instruction: ref.func (0xd2) , Bytecode offset: 0x00000000
21. [2021-12-23 15:19:06.690] [error]     At AST node: expression
22. [2021-12-23 15:19:06.690] [error]     At AST node: element segment
23. [2021-12-23 15:19:06.690] [error]     At AST node: element section
24. [2021-12-23 15:19:06.690] [error]     At AST node: module
25. [2021-12-23 15:19:06.690] [info] ====================  Statistics  ====================
26. [2021-12-23 15:19:06.690] [info]  Total execution time: 0 ns
27. [2021-12-23 15:19:06.690] [info]  Wasm instructions execution time: 0 ns
28. [2021-12-23 15:19:06.690] [info]  Host functions execution time: 0 ns
29. [2021-12-23 15:19:06.690] [info]  Executed wasm instructions count: 21
30. [2021-12-23 15:19:06.690] [info]  Gas costs: 20

JavaScript 示例

WasmEdge 也可以作为一个高性能、安全、可扩展、易于部署且遵循 Kubernetes 的 JavaScript 运行时。

qjs.wasm 是一个被编译为 WebAssembly 的 JavaScript 解释器。 hello.js 是一个非常简单的 JavaScript 程序。

1. $ wasmedge --dir .:. qjs.wasm hello.js 1 2 3
2. Hello 1 2 3

qjs_tf.wasm 则是一个 WebAssembly 版本的 JavaScript 解释器(带有 Tensorflow 扩展)。 要想运行 qjs_tf.wasm,你必须使用 wasmedge-tensorflow-lite 这个命令行工具;这个工具里内置了包含 Tensorflow 扩展的 WasmEdge 构建版本。 你可以下载一个基于 Tensorflow 的完整 JavaScript 示例来对图像进行分类。

1. # Download the Tensorflow example
2. $ wget https://raw.githubusercontent.com/second-state/wasmedge-quickjs/main/example_js/tensorflow_lite_demo/aiy_food_V1_labelmap.txt
3. $ wget https://raw.githubusercontent.com/second-state/wasmedge-quickjs/main/example_js/tensorflow_lite_demo/food.jpg
4. $ wget https://raw.githubusercontent.com/second-state/wasmedge-quickjs/main/example_js/tensorflow_lite_demo/lite-model_aiy_vision_classifier_food_V1_1.tflite
5. $ wget https://raw.githubusercontent.com/second-state/wasmedge-quickjs/main/example_js/tensorflow_lite_demo/main.js

7. $ wasmedge-tensorflow-lite --dir .:. qjs_tf.wasm main.js
8. label: Hot dog
9. confidence: 0.8941176470588236

继续阅读并学习 WasmEdge。