/uniffi-wasm-playground

A repo that using UniFFI and WASM to generate FFI bindings for Rust and C functions

Primary LanguagePython

UniFFI WASM Playground

NOTE: Further work is not happening in this repo. For more production-ready examples and build steps, see https://github.com/algorandfoundation/Algorand-Rust-FFIs

This is a repo that is being used to play around with UniFFI and WASM. It is based on the UniFFI Arithmetic Example. So far everything has been tested on MacOS.

For more information about UniFFI, see the official documentation and the README.

TL;DR: Write one Rust codebase, generate bindings for the following languages:

  • Python
  • Kotlin
  • Swift
  • React Native*
  • Kotlin Multiplatform*
  • Dart*
  • Go*
  • C#*
  • Ruby**

* = Third-party bindings ** = Legacy support

wasm-pack is also be used in conjunction with UniFFI to target JS (node and web) as well.

Key Files

What's been tested

Feature Python Web
Basic FFI bindings
Async HTTP FFI bindings
Error handling
Calling C lib (c -> rust -> target lang)

Building

Because of rust-lang/cargo#1197 we need to modify the surf features dynamically in Cargo.toml depending on if we're targeting wasm or not. This means instead of using cargo build directly we have a Python script build.py that does this and then calls either cargo build or wasm-pack build respectively.

To build the Python bindings: python build.py py

To build the web bindings: python build.py wasm

Running

To run the Python bindings: python consumers/python/app.py

To run the web bindings: cd consumers/web && python -m http.server

TODO

  • Test on Windows
  • Test on Linux
  • Test other languages supported by UniFFI
  • Further exploration of wasm2js to potentially enable native JS output (vs needing to use wasm), which could be useful for maximal browser compatibility