gchoi_acts/rust-wasmer-2
Public
wasmer run gchoi_acts/rust-wasmer-2

Rust Wasmer

Example code of Rust Wasmer.

References


Creating the Library


touch calculator.wai
// calculator.wai

/// Addition of two numbers
add: func(a: float32, b: float32) -> float32

/// Subtraction of two numbers
subtract: func(a: float32, b: float32) -> float32

/// Multiplication of two numbers
multiply: func(a: float32, b: float32) -> float32

/// Division of two numbers
divide: func(a: float32, b: float32) -> float32
// src/lib.rs

wai_bindgen_rust::export!("calculator.wai");
struct Calculator;

impl crate::calculator::Calculator for Calculator {
    fn add(a: f32, b: f32) -> f32 { a + b }
    fn subtract(a: f32, b: f32) -> f32 { a - b }
    fn multiply(a: f32, b: f32) -> f32 { a * b }
    fn divide(a: f32, b: f32) -> f32 {
        if b != 0.0 {
            a / b
        }
        else {
            0.0
        }
    }
}

Publishing the Library


Install wapm

make install-wapm

Wapm Login

If you don't have an account for https://wasmer.io/, please make one.

wapm login --user <user_id> --password <password>

Or edit wasmer user ID and password in wapm-login in Makefile, and type the following:

make wapm-login

Edit Cargo.toml

[package]
name = "rust-wasmer"
version = "0.1.0"
edition = "2021"
description = "Wasmer Demo"

[package.metadata.wapm]
namespace = "<YOUR_USERNAME>"
abi = "none"
bindings = { wai-version = "0.2.0", exports = "calculator.wai" }

[lib]
crate-type = ["cdylib", "rlib"]

Publish the package

make wapm-publish

After successfully publishing your package, you will see the package in your Wasmer page like,

wasmer-package.jpg

Using the Library


JavaScript

Create JS project:

make js-project

package.json:

{
  "name": "rust-wasmer",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "git@bitbucket.org:vivityai/rust-wasmer.git",
  "author": "Alex Choi <geol.choi@vivity.ai>",
  "license": "MIT",
  "dependencies": {
    "@gchoi_acts/rust-wasmer": "https://cdn.wasmer.io/bindings/generator-0.7.2/npm/gchoi_acts/rust-wasmer/rust-wasmer-0.1.0.tar.gz"
  },
  "type": "module"
}

Create index.js:

// index.js

import {bindings} from "@gchoi_acts/rust-wasmer";

async function main() {
  const calculator = await bindings.calculator();
  console.log("2 + 2 =", calculator.add(2, 2));
  console.log("2 - 2 =", calculator.subtract(2, 2));
  console.log("2 * 2 =", calculator.multiply(2, 2));
  console.log("2 / 2 =", calculator.divide(2, 2));
}

main();

Run the index.js:

make js-run
2 + 2 = 4
2 - 2 = 0
2 * 2 = 4
2 / 2 = 1

Python

Currently, Wasmer Python package supports Python 3.9 (Not tested yet for Python 3.8).

Set up the Python virtual environment:

make python-venv
from rust_wasmer import bindings

if __name__ == "__main__":
    calculator = bindings.calculator()
    print("2 + 2 = ", calculator.add(2.0, 2.0))
    print("2 - 2 = ", calculator.subtract(2.0, 2.0))
    print("2 * 2 = ", calculator.multiply(2.0, 2.0))
    print("2 / 2 = ", calculator.divide(2.0, 2.0))

Run main.py:

make python-run

Wasmer Demo

More packages
wasmer/wasmer-hello website
wasmer/wasmer-edge-nuxt website
The WebAssembly interface to wasmer-pack.
Wasmer Compiler
ptitseb avatar
ptitseb
ptitseb avatar
ptitseb
The Wasmer frontend