forked from rustwasm/wasm-bindgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rustwasm#860 from alexcrichton/juggle-examples
Reorganize and rewrite examples
- Loading branch information
Showing
128 changed files
with
933 additions
and
1,298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,7 @@ | ||
# Examples | ||
|
||
This directory contains a number of examples of the `#[wasm_bindgen]` macro and | ||
how to display them in the browser. Each directory contains a README with a link | ||
to https://webassembly.studio so you can also explore the example online | ||
(apologies if they're out of sync!), and each directory also contains a | ||
`build.sh` which assembles all the relevant files locally. If you open up | ||
`index.html` in a web browser you should be able to see everything in action | ||
when using `build.sh`! | ||
This directory contains a number of Cargo projects that are all examples of how | ||
to use `wasm-bindgen` in various contexts. More documentation can be [found | ||
online][dox] | ||
|
||
The examples here are: | ||
|
||
* `add` - an example of generating a tiny wasm binary, one that only adds two | ||
numbers. | ||
* `asm.js` - an example of using the `wasm2js` tool from [binaryen] to convert | ||
the generated WebAssembly to normal JS | ||
* `char` - an example of passing the rust `char` type to and from the js `string` type | ||
* `closures` - an example of how to invoke functions like `setInterval` or use | ||
the `onclick` property in conjunction with closures. | ||
* `comments` - an example of how Rust comments are copied into js bindings | ||
* `console_log` - a showcase of `#[wasm_bindgen]` importing classes and how to | ||
bind `console.log` | ||
* `dom` - an example of accessing the global `document` object and appending | ||
HTML to it | ||
* `fetch` -- how to use the Fetch API to make async http requests | ||
* `hello_world` - the "hello world" of `#[wasm_bindgen]`, aka throwing up a | ||
dialog greeting you | ||
* `import_js` - an example of importing local JS functionality into a crate | ||
* `math` - like `console_log` except showing how to import Math-related | ||
functions instead | ||
* `no_modules` - an example of how to use the `--no-modules` flag to | ||
the `wasm-bindgen` CLI tool | ||
* `performance` - how to import APIs like `performance.now()` and time various | ||
operations in Rust | ||
* `smorgasboard` - a bunch of features all thrown into one, showing off the | ||
various capabilities of the `#[wasm_bindgen]` macro and what you can do with | ||
it from JS | ||
* `wasm-in-wasm` - how to interact with namespaced APIs like | ||
`WebAssembly.Module` and shows off creation of a WebAssembly module from Rust | ||
* `webaudio` - how to use the Web Audio APIs to generate sounds | ||
|
||
[binaryen]: https://github.com/WebAssembly/binaryen | ||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/index.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,15 @@ | ||
# Adding Numbers | ||
# Adding numbers (small wasm files) | ||
|
||
[View this example online](https://webassembly.studio/?f=612vwsrmwft) | ||
[View documentation for this example online][dox] | ||
|
||
This directory is an example of using the `#[wasm_bindgen]` macro to simply add | ||
two numbers. The neat part about this is that it's an example of how to generate | ||
the smallest wasm-bindgen binary. | ||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/add.html | ||
|
||
You can build the example with: | ||
You can build the example locally with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
|
||
(or running the commands on Windows manually) | ||
|
||
Currently this generates a 651 byte wasm binary: | ||
|
||
``` | ||
$ ls -alh add_bg.wasm | ||
-rw-rw-r-- 1 alex alex 651 Apr 20 22:16 add_bg.wasm | ||
``` | ||
|
||
If you run [wasm-opt], a C++ tool for optimize WebAssembly, you can make it even | ||
smaller too! | ||
|
||
``` | ||
$ wasm-opt -Os add_bg.wasm -o add.wasm | ||
$ ls -alh add.wasm | ||
-rw-rw-r-- 1 alex alex 100 Apr 20 22:19 add.wasm | ||
``` | ||
|
||
And sure enough, using the [wasm2wat] tool it's quite small! | ||
|
||
``` | ||
$ wasm2wat add.wasm | ||
(module | ||
(type (;0;) (func (param i32 i32) (result i32))) | ||
(func (;0;) (type 0) (param i32 i32) (result i32) | ||
get_local 1 | ||
get_local 0 | ||
i32.add) | ||
(memory (;0;) 2) | ||
(export "memory" (memory 0)) | ||
(export "add" (func 0)) | ||
(data (i32.const 1545) "invalid malloc request")) | ||
``` | ||
|
||
Note that it's important to point out that the size reductions here are because | ||
the wasm is compiled in release mode by the build script and this crate's | ||
workspace has the following configuration | ||
|
||
```toml | ||
[profile.release] | ||
lto = true | ||
opt-level = 's' | ||
panic = 'abort' | ||
``` | ||
|
||
[wasm2wat]: https://github.com/webassembly/wabt | ||
[wasm-opt]: https://github.com/webassembly/binaryen | ||
and then visiting http://localhost:8080 in a browser should run the example! |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package-lock.json | ||
wasm_bindgen_canvas_demo.js | ||
wasm_bindgen_canvas_demo_bg.js | ||
wasm_bindgen_canvas_demo_bg.wasm | ||
canvas.js | ||
canvas_bg.js | ||
canvas_bg.wasm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# Canvas 2D Example | ||
# 2D Canvas | ||
|
||
This directory is an example of using the `web-sys` crate to draw on a 2D | ||
canvas. | ||
[View documentation for this example online][dox] | ||
|
||
You can build and run the example with: | ||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/2d-canvas.html | ||
|
||
You can build the example locally with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
|
||
(or running the commands on Windows manually) | ||
|
||
and then opening up `http://localhost:8080/` in a web browser should show a | ||
smiley face drawn on canvas by Rust and WebAssembly. | ||
and then visiting http://localhost:8080 in a browser should run the example! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,5 @@ | |
</head> | ||
<body> | ||
<canvas id="canvas" height="150" width="150" /> | ||
<script src='./index.js'></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// For more comments about what's going on here, check out the `hello_world` | ||
// example. | ||
import('./wasm_bindgen_canvas_demo').then(canvas => { | ||
import('./canvas').then(canvas => { | ||
canvas.draw(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
# Char | ||
# Working with the `char` type | ||
|
||
This directory is an example of how the `#[wasm_bindgen]` macro will convert the rust `char` type to a single code-point js `string`. | ||
[View documentation for this example online][dox] | ||
|
||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/char.html | ||
|
||
You can build the example locally with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
|
||
Opening your web browser should display a single counter with a random character for it's `key` and 0 for its `count`. You can click the `+` button to increase a counter's count. By clicking on the "add counter" button you should see a new counter added to the list with a different random character for it's `key`. | ||
(or running the commands on Windows manually) | ||
|
||
Under the hood javascript is choosing a random character from an Array of characters and passing that to the rust Counter struct's constructor so the character you are seeing on the page has made the full round trip from js to rust and back to js. | ||
and then visiting http://localhost:8080 in a browser should run the example! |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
# Closure examples | ||
# web-sys: Closures | ||
|
||
[View this example online](https://webassembly.studio/?f=g3hc1qs6tka) | ||
[View documentation for this example online][dox] | ||
|
||
This directory is an example of using the `#[wasm_bindgen]` macro with closures | ||
to interact with the DOM. | ||
[dox]: https://rustwasm.github.io/wasm-bindgen/examples/closures.html | ||
|
||
You can build the example with: | ||
You can build the example locally with: | ||
|
||
``` | ||
$ ./build.sh | ||
``` | ||
|
||
(or running the commands on Windows manually) | ||
|
||
and then opening up `index.html` in a web browser should show a hello message on | ||
the web page generated by the wasm. | ||
|
||
For more information about this example be sure to check out | ||
[`hello_world`][hello] which also has more comments about caveats and such. | ||
|
||
[hello]: https://github.com/alexcrichton/wasm-bindgen/tree/master/examples/hello_world | ||
and then visiting http://localhost:8080 in a browser should run the example! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,5 @@ | |
times | ||
</p> | ||
</div> | ||
<script src='./index.js'></script> | ||
</body> | ||
</html> |
Oops, something went wrong.