Code for my workshop "Production-ready WebAssembly with Rust" presented at RustLab 2023 in Florence

Overview

Workshop: Production-ready WebAssembly with Rust

A workshop on Rust for WebAssembly by Alberto Schiabel (@jkomyno). 🤓 This workshop was first presented at RustLab 2023 in Florence, Italy.

The goal of this workshop is allowing experienced Rust engineers to develop WebAssembly modules that can be seamlessly integrated with Node.js and TypeScript codebases. No previous knowledge of WebAssembly is required.

We will be coding through a series of tutorials and test-driven exercises covering topics that every WebAssembly developer likely encounters in production settings:

  • understanding wasm_bindgen, its uses, limitations, and alternatives
  • error management and panic handling

Along the way, we'll point out some edge cases that will hopefully help you avoid hours of head-scratching and debugging sessions 😄.

We'll also hint at ways you can overcome WebAssembly's own limitations, proving that you can actually run I/O logic without needing WASI.

Finally, we'll demonstrate how to port a moderately complex Rust library to WebAssembly, applying the concepts learnt during the workshop.

Prerequisites

Before diving into the workshop, let's make sure you have the following tools installed in your system:

Optionally, you can also install the Wasm binary tools from bynarien. They are not strictly required for the workshop, but they can be useful to inspect the generated WebAssembly modules. If you use Docker, you'll find these tools (e.g., wasm2wat and wasm-opt) already installed.

Getting started

Clone the repository and run ./install.sh to get all the necessary dependencies. This workshop is divided into chapters, containing a mixture of lessons and exercises.

The first chapter is 1-wasm-preliminaries,

Each chapter focuses on a specific concept about WebAssembly development, and provides some examples and exercises to familiarize with that concept. For each chapter, you will need to complete some Rust code.

To test the exercises, open two terminal tabs side-by-side:

Run ./watch.sh in each tab, followed by the chapter number. For instance, for the first chapter, if you run ./watch.sh 2 in each tab,

Watch

the Rust code will silently be re-compiled to WebAssembly for you via cargo watch, and tested on Node.js via vitest.

The Rust source code in ./rust uses Cargo workspaces. This enables sharing dependencies across the exercise crates for each chapter via the [workspace.dependencies] attribute in ./rust/Cargo.toml: rather than having to add a specific version of a dependency in an exercise crate, we can refer to the workspace version instead.

# Cargo.toml in an exercise crate
[dependencies]
wasm-bindgen.workspace = true  # <- wasm-bindgen = "0.2.88"

Should run into any issue with the repository and the assumed level of knowledge, please raise your hand and ping us, so we can sort this through together 🪄. Enjoy!

➡️ GET STARTED.

Using Docker

If you prefer to keep your environment clean and use Docker, you have two choices:

  • If you use VSCode, you can re-open this repository in a DevContainer by running the Dev Containers: Reopen in Container command (you can open the command palette via Ctrl+⇧+P / Cmd+⇧+P).
  • Otherwise, you can spin up a workable environment in your terminal via:
docker compose run --build --rm -it workspace

Chapters overview

This is a work in progress, so stay tuned for more chapters about:

  • async / await support
  • online WebAssembly debugging on Chrome Dev Tools with source maps and breakpoints
  • WASI support on Node.js
  • Publish WebAssembly modules on npm
  • and more!

License

Licensed under MIT License. © Alberto Schiabel.

You might also like...
Rust solutions to problems in Advent of Code '22.

Advent of Code '22 This repository contains my solutions for AoC'22 problems, written in the language that will save us from cpp. Running the code The

Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.
Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.

🌼 Zinnia Zinnia is a runtime for Filecoin Station modules. It provides a sandboxed environment to execute untrusted code on consumer-grade computers.

zzhack-cli is a Command Tool to help you quickly generate a WASM WebApp with simple configuration and zero code
zzhack-cli is a Command Tool to help you quickly generate a WASM WebApp with simple configuration and zero code

English | 中文文档 zzhack-cli is a Command Tool that can help you quickly generate a WASM WebApp with simple configuration and zero code. It's worth menti

Simple RISC-V emulator presented at Rust Nation 2023

A tale of binary translation This repo contains the code for the RISC-V emulator that I presented at Rust Nation 2023. It is intended as a teaching ex

GraphQL Summit 2023 Workshop Demo for Federated Subscriptions in GraphOS.

Kahoot Clone Demo App - Subgraphs Welcome to the project for GraphQL Summit 2023 "Federated Subscriptions in GraphOS" workshop! ⚠️ Before the workshop

Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

Source code from Atlas, our 64k demo presented at Revision 2019 with Macau Exports

Atlas source code dump This is a dump of the source code for the engine, graphics tool and player for Atlas, our 64k demo released with Macau Exports

Accompanying code for my talk "No free lunch: Limits of Wasm as a bridge from Rust to JS" presented @ EuroRust2022 in Berlin

No free lunch: Limits of Wasm as a bridge from Rust to JS Accompanying code for the talk I presented at EuroRust 2022 in Berlin, Germany Slides for th

⚙️ Workshop Publishing Utility for Garry's Mod, written in Rust & Svelte and powered by Tauri
⚙️ Workshop Publishing Utility for Garry's Mod, written in Rust & Svelte and powered by Tauri

⚙️ gmpublisher Currently in Beta development. A powerful and feature-packed Workshop publisher for Garry's Mod is finally here! Click for downloads Ar

HackWasm Workshop Starter, Checkpoints and Instructions

Osmosis Swaprouter Worskhop This is a workshop for building an Osmosis Swap Router CosmWasm contract. The original contract repository is located here

STARK 101 Workshop in Rust 🐺🦀

STARK101-rs 🦀 About This repository is based on the STARK 101 workshop, originally written in Python. A Rust tutorial for a basic STARK (Scalable Tra

Example (with solution) for the Rust Fundamentals Workshop

Workshop Repo: Rust Fundamentals NOTE: This is Work-In-Progress! Please check for updates a day before the workshop. This Github repository contains a

STARK 101 Workshop in Rust 🐺🦀

STARK101-rs 🦀 About This repository is based on the STARK 101 workshop, originally written in Python. A Rust tutorial for a basic STARK (Scalable Tra

🦀🔨 DevBcn Workshop - Full Stack Rust - Actix - Postgres - Shuttle - Dioxus
🦀🔨 DevBcn Workshop - Full Stack Rust - Actix - Postgres - Shuttle - Dioxus

Building a Movie Collection Manager - Full Stack Workshop with Rust, Actix, SQLx, Dioxus, and Shuttle Welcome to the this workshop! In this hands-on w

Noir, Circom, and Halo2 exercises for DSL workshop at ZKSummit10

ZKSummit10 - Exploring Noir, Circom, halo2 Workshop Prompt Write Noir, Halo2 and Circom circuits that constrain integer division i.e decimals are trun

🎄Solutions for Advent of Code 2023
🎄Solutions for Advent of Code 2023

🎄 Advent of Code 2023 Solutions for Advent of Code in Rust. 2023 Results Day Part 1 Part 2 Day 1 ⭐ ⭐ Day 2 ⭐ ⭐ Day 3 ⭐ ⭐ Day 4 ⭐ ⭐ Day 5 ⭐ ⭐ Day 6 ⭐

My solutions for the Advent of Code 2023.

🎄 Advent Of Code 2023 Hi! These are my Rust solutions for the Advent of Code 2023. Day Name Source Part 1 Part 2 Time 1 Time 2 1 Trebuchet?! 01.rs ⭐

Shuttle.rs Christmas Code Hunt 2023

🚀 Shuttle.rs Christmas Code Hunt 2023 Submissions 🎄 These are my submissions for the Christmas Code Hunt hosted by shuttle.rs This is using a custom

Solutions of Advent-of-Code 2023 in Rust

Advent of Code 2023 in Rust If you've never heard of Advent of Code, you're missing out. Check it out now at adventofcode.com! I mostly do AoC puzzles

Owner
Alberto Schiabel
Computer scientist and consultant with a keen eye for efficient algorithms and mathematical structures.
Alberto Schiabel
Run Java code from Rust!

Java Native Interface Bindings for Rust This library provides complete FFI bindings to the Java Native Interface, as well as a safe and intuitive wrap

Ben Anderson 66 Nov 28, 2022
Guarding 是一个用于 Java、JavaScript、Rust、Golang 等语言的架构守护工具。借助于易于理解的 DSL,来编写守护规则。Guarding is a guardians for code, architecture, layered.

Guarding Guarding is a guardians for code, architecture, layered. Using git hooks and DSL for design guard rules. Usage install cargo install guarding

Inherd OS Team (硬核开源小组) 47 Dec 5, 2022
An interactive scripting language where you can read and modify code comments as if they were regular strings

An interactive scripting language where you can read and modify code comments as if they were regular strings. Add and view text-based visualizations and debugging information inside your source code file.

Sumeet Agarwal 13 Apr 28, 2022
A Web-App written in Rust with Yew, using the same SyntaxHighlighter from Google Code Archive as planetb.ca

PlanetB SyntaxHighlighter About This is a small app, providing static files to have a frontend to format your code so you can paste it with styles to

Christof Weickhardt 2 Dec 14, 2022
Twiggy🌱 is a code size profiler

Twiggy ?? A code size profiler for Wasm Guide | Contributing | Chat Built with ?? ?? by The Rust and WebAssembly Working Group About Twiggy is a code

Rust and WebAssembly 958 Jan 5, 2023
Rust library for build scripts to compile C/C++ code into a Rust library

A library to compile C/C++/assembly into a Rust library/application.

Alex Crichton 1.3k Dec 21, 2022
This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents.

lua-to-ts This tool converts Lua code to TS automatically, including the conversion of common standards to their TS equivalents. Code that fails to be

Dion 11 Nov 21, 2022
A collection of unsound rust functions using entirly *safe* code

A collection of unsound rust functions using entirly *safe* code

null 2 Sep 6, 2022
A simple code for checking crate 'prost' on WebAssembly (🦀 + 🕸️ = 💖)

rust-wasm-prost This repository is a simple code for checking crate 'prost' on WebAssembly ( ?? + ??️ = ?? ). What is prost? prost is a Protocol Buffe

Chris Ohk 6 Apr 5, 2022
Turns running Rust code into a serializable data structure.

WasmBox WasmBox turns running Rust code into a serializable data structure. It does this by compiling it to WebAssembly and running it in a sandbox. T

drifting in space 12 Dec 7, 2022