• Stars
    star
    7,225
  • Rank 5,100 (Top 0.2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 26 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Facilitating high-level interactions between Wasm modules and JavaScript

wasm-bindgen

Facilitating high-level interactions between Wasm modules and JavaScript.

Build Status Crates.io version Download docs.rs docs

Built with πŸ¦€πŸ•Έ by The Rust and WebAssembly Working Group

Install wasm-bindgen-cli

You can install it using cargo install:

cargo install wasm-bindgen-cli

Or, you can download it from the release page.

If you have cargo-binstall installed, then you can install the pre-built artifacts by running:

cargo binstall wasm-bindgen-cli

Example

Import JavaScript things into Rust and export Rust things to JavaScript.

use wasm_bindgen::prelude::*;

// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
    fn alert(s: &str);
}

// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
    alert(&format!("Hello, {}!", name));
}

Use exported Rust things from JavaScript with ECMAScript modules!

import { greet } from "./hello_world";

greet("World!");

Features

  • Lightweight. Only pay for what you use. wasm-bindgen only generates bindings and glue for the JavaScript imports you actually use and Rust functionality that you export. For example, importing and using the document.querySelector method doesn't cause Node.prototype.appendChild or window.alert to be included in the bindings as well.

  • ECMAScript modules. Just import WebAssembly modules the same way you would import JavaScript modules. Future compatible with WebAssembly modules and ECMAScript modules integration.

  • Designed with the "Web IDL bindings" proposal in mind. Eventually, there won't be any JavaScript shims between Rust-generated wasm functions and native DOM methods. Because the wasm functions are statically type checked, some of those native methods' dynamic type checks should become unnecessary, promising to unlock even-faster-than-JavaScript DOM access.

Guide

πŸ“š Read the wasm-bindgen guide here! πŸ“š

You can find general documentation about using Rust and WebAssembly together here.

API Docs

License

This project is licensed under either of

at your option.

Contribution

See the "Contributing" section of the guide for information on hacking on wasm-bindgen!

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

wasm-pack

πŸ“¦βœ¨ your favorite rust -> wasm workflow tool!
Rust
5,843
star
2

book

The Rust and WebAssembly Book
Handlebars
1,686
star
3

gloo

A modular toolkit for building fast, reliable Web applications and libraries with Rust and WASM
Rust
1,661
star
4

team

A point of coordination for all things Rust and WebAssembly
1,408
star
5

twiggy

Twiggy🌱 is a code size profiler
Rust
1,217
star
6

wee_alloc

The Wasm-Enabled, Elfin Allocator
Rust
644
star
7

awesome-rust-and-webassembly

Awesome Rust and WebAssembly projects, libraries, tools, and resources
598
star
8

wasm-pack-template

a template for starting a rust-wasm project to be used with wasm-pack
Rust
580
star
9

rust-webpack-template

Kickstart your Rust, WebAssembly, and Webpack project!
JavaScript
553
star
10

create-wasm-app

npm init template for consuming rustwasm pkgs
JavaScript
476
star
11

walrus

Walrus is a WebAssembly transformation library 🌊🐘
Rust
356
star
12

console_error_panic_hook

A panic hook for wasm32-unknown-unknown that logs panics with console.error
Rust
300
star
13

wasm_game_of_life

A Rust and WebAssembly tutorial implementing the Game of Life
Rust
266
star
14

wasm-snip

`wasm-snip` replaces a WebAssembly function's body with an `unreachable`
Rust
209
star
15

rust-parcel-template

Kickstart your Rust, WebAssembly, and Parcel project!
Rust
156
star
16

deprecated_rust_wasm_template

Deprecated in favor of rustwasm/wasm-pack-template or rustwasm/rust-webpack-template
Shell
102
star
17

wasm-tracing-allocator

A global allocator for Wasm that traces allocations and deallocations for debugging purposes.
Rust
100
star
18

rustwasm.github.io

Rust and WebAssembly website! πŸ¦€ + πŸ•Έ
HTML
65
star
19

weedle

A WebIDL Parser
WebIDL
42
star
20

rfcs

The Rust and WebAssembly RFCs
Shell
39
star
21

wasm-webidl-bindings

Read, write, and manipulate the Wasm WebIDL bindings custom section
Rust
37
star
22

binary-install

Rust
10
star
23

wasm-weight-tracker

Tracking Rust and WebAssembly sizes over time
Rust
10
star
24

hello-wasm-bindgen

a presentation introducing wasm-bindgen
JavaScript
8
star
25

sfhtml5-rust-and-wasm

JavaScript
4
star
26

.github

Default issue templates, CONTRIBUTING.md, etc...
2
star
27

wasm-weight-tracker-data

WIP
1
star