Giter Site home page Giter Site logo

topheman / webassembly-wasi-experiments Goto Github PK

View Code? Open in Web Editor NEW
24.0 2.0 0.0 1.52 MB

Discover WebAssembly System Interface (WASI) with C/Rust use cases

Home Page: https://topheman.github.io/webassembly-wasi-experiments/

Makefile 30.46% HTML 20.63% JavaScript 21.37% C 1.88% Dockerfile 5.04% Python 16.26% Rust 1.90% CSS 2.46%
webassemby wasi wasm wasmtime wasmer

webassembly-wasi-experiments's Introduction

webassembly-wasi-experiments

You may know WebAssembly, it's been around for a few years. Two years ago, I made a side project topheman/rust-wasm-experiments to explain it to web developers with a more advanced example than the usual add/substract use case.

WebAssembly is moving beyond the browser, which means our code needs a way to talk to the system (to access resources like files, memory, network connections). This is WASI : WebAssembly System Interface.

All WASI is Wasm, but not all Wasm is WASI

Goal

At the time I'm writing these lines, WASI is still very early. The resources and examples are scattered on the web. My goal is to give you a set of examples with the following constraints:

You will also find a lot of resources that I gathered while I was putting this project together.

Summary

Prerequisites

You need to have installed:

  • docker
  • npm / node

Install

git clone https://github.com/topheman/webassembly-wasi-experiments.git
cd webassembly-wasi-experiments
npm install
make init-docker

Run

You will find two programs, one written in C (source), an other written in Rust (source), they both do the same thing: it is a command line interface that accepts any number of arguments and writes a tmp.txt file with those arguments, separated by line breaks.

A Makefile is available, at any moment you can run make help to get the help menu.

Outside of the browser

For each program (c or rust), you can:

  1. Build the .wasm file with make docker-wasm-create-{c,rust}-app
  2. Then execute the generated .wasm file through one of the runtimes with:
  • make docker-run-wasmtime-{c,rust}-app
  • make docker-run-python-{c,rust}-app
  • make run-node-{c,rust}-app
  1. Those tasks will generate a tmp.txt file and output its content in the terminal. This file will have been created by the selected runtime which will have compiled the .wasm file compiled from C or Rust.

Inside the browser

Demo accessible online

The build steps are the same as for outside the browser (since we use the same C/Rust program compiled in the same way to WebAssembly):

  1. Build the .wasm file with make docker-wasm-create-{c,rust}-app
  2. Then build the website files and launch the server with npm start

Go to http://localhost:5000 with Chrome or FireFox and try the demo in the browser.

You will find the same C/Rust programs compiled to WebAssembly running inside the browser through the wasmer runtime which emulates file system bindings.

File system access

WASI will never let you access directly to files, directories, network sockets, and other resources that are identified by UNIX-like file descriptors.

You will have to tell your runtime which resources of the host you wish to access by declaring a preopens property - example:

The following config means that, when executed in the WASI runtime, the /tmp folder on the host machine will be available at . (current folder) in the wasm program (much like docker volume mapping).

new WASI({
  preopens: {
    '.': '/tmp',
  },
});

More infos at: Wasi Overview - Capability-Oriented

Resources

Author

Christophe Rosset

webassembly-wasi-experiments's People

Contributors

topheman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.