Code Monkey home page Code Monkey logo

component-model-calculator's Introduction

Building a Calculator of Wasm Components

This tutorial walks through how to compose a component to build a Wasm calculator. The WIT package for the calculator consists of a world for each mathematical operator add an op enum that delineates each operator. The following example interface only has an add operation:

package docs:calculator@0.1.0;

interface calculate {
    enum op {
        add,
    }
    eval-expression: func(op: op, x: u32, y: u32) -> u32;
}

interface add {
    add: func(a: u32, b: u32) -> u32;
}

world adder {
    export add;
}

world calculator {
    export calculate;
    import add;
}

To expand the exercise to add more components, add another operator world, expand the enum, and modify the command component to call it.

Prerequisites

  • cargo 1.77
  • wasm-tools 1.206.0
  • wasmtime 20.0.0 (if running with wasmtime)

Building and running the example

To compose a calculator component with an add operator, run the following:

(cd calculator && cargo component build --release)
(cd adder && cargo component build --release)
(cd subtractor && cargo component build --release)
(cd multiplier && cargo component build --release)
(cd divisor && cargo component build --release)
(cd command && cargo component build --release)
cd exp
wit-bindgen c --autodrop-borrows yes --world exponential  ../wit/calculator.wit

For the C component

/opt/wasi-sdk/bin/clang exp.c exponential.c exponential_component_type.o -o exponential-core.wasm -mexec-model=reactor
wasm-tools component new ./exponential-core.wasm -o exponential-component.wasm
wasm-tools component wit exponential-component.wasm

Create the composed and the command

wasm-tools compose calculator/target/wasm32-wasi/release/calculator.wasm -d adder/target/wasm32-wasi/release/adder.wasm -d subtractor/target/wasm32-wasi/release/subtractor.wasm -d multiplier/target/wasm32-wasi/release/multiplier.wasm -d divisor/target/wasm32-wasi/release/divisor.wasm -d exp/exponential-component.wasm -o composed.wasm

wasm-tools compose command/target/wasm32-wasi/release/command.wasm -d composed.wasm -o command.wasm

Running with wasmtime

You must have wasmtime 20.0.0 for this to work. Make sure to follow the build step above first.

wasmtime run --wasm component-model command.wasm 1 2 add
1 + 2 = 3
wasmtime run --wasm component-model command.wasm 6 2 sub
6 - 2 = 4
wasmtime run --wasm component-model command.wasm 6 2 add
6 + 2 = 8
wasmtime run --wasm component-model command.wasm 6 2 mul
6 * 2 = 12
wasmtime run --wasm component-model command.wasm 6 2 div
6 / 2 = 3
wasmtime run --wasm component-model command.wasm 6 2 exp
6 ^ 2 = 36

component-model-calculator's People

Contributors

gazzu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.