Giter Club home page Giter Club logo

learn-wasm-components's Introduction

learn-wasm-components

Goal

Create a WASM component function in any language that can be invoked from a host runtime. This example creates a Golang and Rust component to implement is-odd-or-even.wit. The components are hosted by the runtime/rust crate.

Install dependencies

Bytecode Alliance's wasm-tools and wit-bindgen are required to build a WASM component with TinyGo's.

cargo install wasm-tools
cargo install wit-bindgen-cli

brew tap tinygo-org/tools
brew install tinygo

Build steps

The component/golang and component/rust directories have a README.md files with instructions for building the WASM component.

runtime/rust can be run with cargo run with a COMPONENT_LANGUAGE env var specifying golang or rust. is-odd-or-even will be called with the arg to the program.

COMPONENT_LANGUAGE=golang cargo run -- 42

Inspecting components

wasm-tools validate is-odd-or-even.wasm --features component-model
wasm-tools component wit is-odd-or-even.wasm

Current state

golang rust
debug 107214 1750360
release 107214 1640355

Rust component is 16.3x larger than Golang component.

wasm-tools component wit output for each language's component varies and includes a lot of WASI imports that unused by the implementation.

golang component WIT

package root:component;

world root {
  import wasi:io/error@0.2.0;
  import wasi:io/streams@0.2.0;
  import wasi:cli/stdin@0.2.0;
  import wasi:cli/stdout@0.2.0;
  import wasi:cli/stderr@0.2.0;
  import wasi:clocks/wall-clock@0.2.0;
  import wasi:filesystem/types@0.2.0;
  import wasi:filesystem/preopens@0.2.0;

  export is-odd-or-even: func(number: s32) -> bool;
}

Rust component WIT

package root:component;

world root {
  import wasi:cli/environment@0.2.0;
  import wasi:cli/exit@0.2.0;
  import wasi:io/error@0.2.0;
  import wasi:io/streams@0.2.0;
  import wasi:cli/stdin@0.2.0;
  import wasi:cli/stdout@0.2.0;
  import wasi:cli/stderr@0.2.0;
  import wasi:clocks/wall-clock@0.2.0;
  import wasi:filesystem/types@0.2.0;
  import wasi:filesystem/preopens@0.2.0;

  export is-odd-or-even: func(number: s32) -> bool;
}

learn-wasm-components's People

Watchers

Adam McKee 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.