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.
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()
We still use OpenBSD libc by @trevyn here.