Skip to content

Latest commit

 

History

History

4_wasm_bindgen

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Rust+C in the same WASM binary

Project 4: Wasm-Bindgen

Building on top of the previous example, what if we want a more idiomatic interface in our Javascript?

The canonical way of doing that in the Rust world is by using Wasm Bindgen. While one could wrap the exported methods manually with a custom JS class, Wasm Bindgen and Wasm Pack make this process extremely convenient and safe.

For spicing things up a little bit, we'll move the whole Calculator definition entirely to C.

The Calculator class

This project has the same functions as the last one, but wrapped in a Javascript class generated by Wasm Bindgen:

  • Calculator.add()
  • Calculator.subtract()
  • Calculator.multiply()
  • Calculator.divide()
  • Calculator.store()
  • Calculator.retrieve()
  • Calculator.clear()

References

We still use OpenBSD libc by @trevyn here.