diff --git a/Cargo.toml b/Cargo.toml index 2ce49eb48..7903a6cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,11 +58,9 @@ members = [ "crates/webidl", "crates/webidl-tests", "examples/add", - "examples/asm.js", "examples/canvas", "examples/char", "examples/closures", - "examples/comments", "examples/console_log", "examples/duck-typed-interfaces", "examples/dom", @@ -71,12 +69,11 @@ members = [ "examples/hello_world", "examples/import_js", "examples/julia_set", - "examples/math", "examples/no_modules", "examples/paint", "examples/performance", - "examples/smorgasboard", "examples/wasm-in-wasm", + "examples/wasm2js", "examples/webaudio", "examples/webgl", "tests/no-std", diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index c7501da48..0dd9c5be6 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1753,6 +1753,13 @@ extern "C" { #[wasm_bindgen(constructor)] pub fn new(init: &JsValue) -> Date; + /// Creates a JavaScript Date instance that represents the current moment in + /// time. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) + #[wasm_bindgen(constructor)] + pub fn new_0() -> Date; + /// The `Date.now()` method returns the number of milliseconds /// elapsed since January 1, 1970 00:00:00 UTC. /// diff --git a/crates/macro/ui-tests/Cargo.toml b/crates/macro/ui-tests/Cargo.toml index ce105e8c2..3b51ff0ac 100644 --- a/crates/macro/ui-tests/Cargo.toml +++ b/crates/macro/ui-tests/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "ui-tests" version = "0.1.0" -authors = ["The wasm-bindgen Authors"] +authors = ["The wasm-bindgen Developers"] [lib] path = "test.rs" diff --git a/crates/test/sample/Cargo.toml b/crates/test/sample/Cargo.toml index 591e147ea..de4f27560 100644 --- a/crates/test/sample/Cargo.toml +++ b/crates/test/sample/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sample" version = "0.1.0" -authors = ["The wasm-bindgen Authors"] +authors = ["The wasm-bindgen Developers"] [lib] test = false diff --git a/crates/webidl-tests/Cargo.toml b/crates/webidl-tests/Cargo.toml index 8c5b2ede8..e8c022c83 100644 --- a/crates/webidl-tests/Cargo.toml +++ b/crates/webidl-tests/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "webidl-tests" version = "0.1.0" -authors = ["The wasm-bindgen authors"] +authors = ["The wasm-bindgen Developers"] [lib] test = false diff --git a/examples/README.md b/examples/README.md index f4467e5bd..1f2e50eec 100644 --- a/examples/README.md +++ b/examples/README.md @@ -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 diff --git a/examples/add/README.md b/examples/add/README.md index a1f75d4eb..5bf9743b9 100644 --- a/examples/add/README.md +++ b/examples/add/README.md @@ -1,12 +1,10 @@ -# 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 @@ -14,48 +12,4 @@ $ ./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! diff --git a/examples/asm.js/README.md b/examples/asm.js/README.md deleted file mode 100644 index fa347257a..000000000 --- a/examples/asm.js/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# WebAssembly to asm.js - -This directory is an example of using [binaryen]'s `wasm2js` tool to convert -the wasm output of `wasm-bindgen` to a normal JS file that can be executed like -asm.js. - -You can build the example locally with: - -``` -$ ./build.sh -``` - -When opened in a web browser this should print "Hello, World!" to the console. - -Note that the `wasm2js` tool is still pretty early days so there's likely to be -a number of bugs to run into or work around. If any are encountered though -please feel free to report them upstream! - -[binaryen]: https://github.com/WebAssembly/binaryen diff --git a/examples/asm.js/index.js b/examples/asm.js/index.js deleted file mode 100644 index 647c31e31..000000000 --- a/examples/asm.js/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import { run } from './asmjs'; - -run(); diff --git a/examples/canvas/.gitignore b/examples/canvas/.gitignore index b2450cc7c..a5e6169e7 100644 --- a/examples/canvas/.gitignore +++ b/examples/canvas/.gitignore @@ -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 diff --git a/examples/canvas/Cargo.toml b/examples/canvas/Cargo.toml index 2f3420b02..3966d127e 100644 --- a/examples/canvas/Cargo.toml +++ b/examples/canvas/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "wasm-bindgen-canvas-demo" +name = "canvas" version = "0.1.0" authors = ["The wasm-bindgen Developers"] diff --git a/examples/canvas/README.md b/examples/canvas/README.md index a5cf37735..914f1b697 100644 --- a/examples/canvas/README.md +++ b/examples/canvas/README.md @@ -1,9 +1,10 @@ -# 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 @@ -11,5 +12,4 @@ $ ./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! diff --git a/examples/canvas/build.sh b/examples/canvas/build.sh index f9273ed42..d6313df38 100755 --- a/examples/canvas/build.sh +++ b/examples/canvas/build.sh @@ -9,7 +9,7 @@ cargo +nightly build --target wasm32-unknown-unknown cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \ --bin wasm-bindgen -- \ - ../../target/wasm32-unknown-unknown/debug/wasm_bindgen_canvas_demo.wasm --out-dir . + ../../target/wasm32-unknown-unknown/debug/canvas.wasm --out-dir . npm install npm run serve diff --git a/examples/canvas/index.html b/examples/canvas/index.html index c13b999e4..319ffd0ed 100644 --- a/examples/canvas/index.html +++ b/examples/canvas/index.html @@ -4,6 +4,5 @@
-