wasm-bindgen/examples/add
Alex Crichton 906cd7adcc Remove usage of wasm_import_module feature
This is now stabilized! Also tweak usage of it to the stable version.
2018-07-21 19:00:40 -07:00
..
src Remove usage of wasm_import_module feature 2018-07-21 19:00:40 -07:00
.gitignore Add an example of a minimal wasm module 2018-04-20 22:38:22 -07:00
build.sh Add an example of a minimal wasm module 2018-04-20 22:38:22 -07:00
Cargo.toml Add an example of a minimal wasm module 2018-04-20 22:38:22 -07:00
index.html Add an example of a minimal wasm module 2018-04-20 22:38:22 -07:00
index.js Applied eslint from main .eslintrc to examples (#418) 2018-07-08 01:02:10 -05:00
package.json feat: bump webpack 2018-06-06 10:07:02 +02:00
README.md Add an example of a minimal wasm module 2018-04-20 22:38:22 -07:00
webpack.config.js Applied eslint from main .eslintrc to examples (#418) 2018-07-08 01:02:10 -05: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'