wasm-bindgen/examples/guide-supported-types-examples
2018-08-14 17:15:01 -07:00
..
src guide: Add examples of boxed number slices 2018-08-14 17:15:01 -07:00
.gitignore guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
bool.js guide: Add bool example to supported types section 2018-08-13 16:55:05 -07:00
bootstrap.js guide: Add examples of boxed number slices 2018-08-14 17:15:01 -07:00
boxed_js_value_slice.js guide: Add Box<[JsValue]> example to supported types section 2018-08-13 17:08:18 -07:00
boxed_number_slices.js guide: Add examples of boxed number slices 2018-08-14 17:15:01 -07:00
build.sh guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
Cargo.toml guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
char.js guide: Add a char example to the supported types section 2018-08-13 16:24:39 -07:00
exported_types.js guide: Add exported rust type examples to reference 2018-08-13 16:03:02 -07:00
imported_types.js guide: Add exported rust type examples to reference 2018-08-13 16:03:02 -07:00
index.html guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
index.js guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
js_value.js guide: Add JsValue example to supported types section 2018-08-13 16:57:29 -07:00
numbers.js guide: Add examples for working with numbers to types section 2018-08-14 15:45:25 -07:00
package.json guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
pointers.js guide: Add working with pointers example to types section 2018-08-14 15:24:43 -07:00
README.md guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00
str.js guide: Add str examples to supported types section 2018-08-13 16:12:58 -07:00
string.js guide: add String example usage to supported types 2018-08-13 16:20:25 -07:00
webpack.config.js guide: Start adding example usage to "supported types" section 2018-08-10 16:56:40 -07:00

Adding Numbers

View this example online

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.

You can build the example 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

[profile.release]
lto = true
opt-level = 's'
panic = 'abort'