Getting started

The easiest way to get started with WasmEdge is to use its command line tools (CLI). You can then run our example WebAssembly and JavaScript programs in the WasmEdge CLI. After that, you can create new programs for WasmEdge and run them in different host applications or frameworks.

Install

You can install WasmEdge using our one-line installer. Your system should have git and curl as prerequisites.

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

For Windows 10, you could use Windows Package Manager Client (aka winget.exe) to install WasmEdge with a single command in your terminal.

  1. winget install wasmedge

If you would like to install WasmEdge with its Tensorflow and image processing extensions, please run the following command. It will attempt to install Tensorflow and image shared libraries on your system.

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

Run the following command to make the installed binary available in the current session.

  1. source $HOME/.wasmedge/env

Use Docker

If you use Docker, you can simply run the WasmEdge application developer Docker images (x86 and arm64). Those images contain all the tooling you need for quick WasmEdge development.

  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 examples

We have several WebAssembly bytecode program examples for you to try out on your newly installed WasmEdge CLI.

Hello world

The hello.wasm WebAssembly program contains a main() function. Checkout its Rust source code project. It prints out hello followed by the command line arguments.

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

Call a function written in Rust

The add.wasm WebAssembly program contains an add() function. Checkout its Rust source code project. We use WasmEdge in reactor mode to call the add() with two integer input parameters.

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

Call a function written in WAT

We created the fibonacci.wat program by hand and used the wat2wasm compiler to build the fibonacci.wasm WebAssembly program. It contains a fib() function which takes a single integer as input parameter. We use wasmedge in reactor mode to call the exported function.

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

With Statistics enabled

The CLI supports --enable-all-statistics flags for the statistics and 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 ======================

With gas-limit enabled

The CLI supports --gas-limit flags for controlling the execution costs.

  1. # cd <path/to/WasmEdge>
  2. $ cd examples/wasm
  3. # With enough 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 ======================
  16. # Without enough gas
  17. $ wasmedge --enable-all-statistics --gas-limit 20 hello.wasm second state
  18. [2021-12-23 15:19:06.690] [error] Cost exceeded limit. Force terminate the execution.
  19. [2021-12-23 15:19:06.690] [error] In instruction: ref.func (0xd2) , Bytecode offset: 0x00000000
  20. [2021-12-23 15:19:06.690] [error] At AST node: expression
  21. [2021-12-23 15:19:06.690] [error] At AST node: element segment
  22. [2021-12-23 15:19:06.690] [error] At AST node: element section
  23. [2021-12-23 15:19:06.690] [error] At AST node: module
  24. [2021-12-23 15:19:06.690] [info] ==================== Statistics ====================
  25. [2021-12-23 15:19:06.690] [info] Total execution time: 0 ns
  26. [2021-12-23 15:19:06.690] [info] Wasm instructions execution time: 0 ns
  27. [2021-12-23 15:19:06.690] [info] Host functions execution time: 0 ns
  28. [2021-12-23 15:19:06.690] [info] Executed wasm instructions count: 21
  29. [2021-12-23 15:19:06.690] [info] Gas costs: 20

JavaScript examples

It is possible to use WasmEdge as a high-performance, secure, extensible, easy to deploy, and Kubernetes-compliant JavaScript runtime.

The qjs.wasm program is a JavaScript interpreter compiled into WebAssembly. The hello.js file is a very simple JavaScript program.

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

The qjs_tf.wasm is a JavaScript interpreter with WasmEdge Tensorflow extension compiled into WebAssembly. To run qjs_tf.wasm, you must use the wasmedge-tensorflow-lite CLI tool, which is a build of WasmEdge with Tensorflow extension built-in. You can download a full Tensorflow-based JavaScript example to classify images.

  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
  6. $ wasmedge-tensorflow-lite --dir .:. qjs_tf.wasm main.js
  7. label: Hot dog
  8. confidence: 0.8941176470588236

Read on and continue your learning of WasmEdge.