Generating Javascript bindings with UniFFI
Firefox supports auto-generating JS bindings for Rust components using UniFFI.
How it works
The uniffi-bindgen-gecko-js
tool, which lives in the Firefox source tree, generates 2 things:
JS bindings for your Rust crate.
C++ code that the JS bindings use to handle the low-level details, like calling into Rust.
Currently, this generated code gets checked in to source control. We are working on a system to avoid this and auto-generate it at build time instead (see bugzilla 1756214).
Before creating new bindings with UniFFI
Keep a few things in mind before you create a new set of bindings:
UniFFI was not written to maximize performance. It’s code is efficient enough to handle many use cases, but at this point should probably be avoided for performance critical components.
uniffi-bindgen-gecko-js bindings run with chrome privileges. Make sure this is acceptable for your project
Only a subset of Rust types can be exposed via the FFI. Check the UniFFI Book to see what types are compatible with UniFFI.
If any of these are blockers for your work, consider discussing it further with the UniFFI devs to see if we can support your project:
Chat with us on
#uniffion Matrix/ElementFile an issue on mozilla/uniffi
Creating new bindings with UniFFI
You can see an example of this feature in use: when application-services swapped the tabs js sync engine with rust
Here’s how you can create a new set of bindings using UniFFI:
UniFFI your crate (if it isn’t already):
Follow the steps from the UniFFI user guide to add support to your crate.
UDL and proc-macros are both supported.
Add your crate as a Firefox dependency (if it isn’t already)
If the code will exist in the Firefox repository:
Create a new directory for the Rust crate
Edit
toolkit/components/uniffi-bindgen-gecko-js/components/Cargo.tomland add a dependency to your library path
If the code exists in an external repo:
Edit
toolkit/components/uniffi-bindgen-gecko-js/components/Cargo.tomland add a dependency to your library URLRun
mach vendor rustto vendor in your Rust code
By default components are only built on desktop: add the dependency to the
cfg(not(any(target_os = "android", target_os = "ios")))section. See Platforms if your crate needs a different set of platforms.
Configure your crate (optional)
Edit
toolkit/components/uniffi-bindgen-gecko-js/config.tomland add an entry for your crate.
Add scaffolding for your crate
Edit
toolkit/components/uniffi-bindgen-gecko-js/components/lib.rsand add an entry for the uniffi scaffolding for your crate.
Generate bindings code for your crate
Run
./mach uniffi generateadd your newly generated
Rust{udl-name}.sys.mjsfile to the desktop-only list intoolkit/components/uniffi-bindgen-gecko-js/components/moz.build
Then simply import your module to the file you want to use it in and start using your APIs!
Example from tabs module:
ChromeUtils.defineESModuleGetters(lazy, { ... TabsStore: "moz-src:///toolkit/components/uniffi-bindgen-gecko-js/components/generated/RustTabs.sys.mjs", }); ... this._rustStore = await lazy.TabsStore.init(path);
Platforms
The platforms a crate’s bindings are compiled into are set by library_group in its
toolkit/components/uniffi-bindgen-gecko-js/config.toml entry. The value is a combination of
DESKTOP (Linux, macOS and Windows) and GECKOVIEW (Android and iOS) joined by |, or
ALL_PLATFORMS:
[my_crate]
library_group = "DESKTOP | GECKOVIEW"
When library_group is missing it defaults to DESKTOP. The generated C++ scaffolding is
guarded accordingly, but the Rust dependency and the .sys.mjs file have to be gated by hand to
match: the cfg section in components/Cargo.toml, the scaffolding re-export in
components/lib.rs and the list in components/moz.build.
GeckoView
On Android and iOS the Rust components are normally consumed by the app through the Kotlin or Swift bindings, so only a few of them are linked into libxul. To make a component available to Gecko on those platforms too:
Add it to the platform-independent
[dependencies]intoolkit/components/uniffi-bindgen-gecko-js/components/Cargo.toml.Re-export its scaffolding in
reexport_appservices_uniffi_scaffoldingincomponents/lib.rs.Add
library_group = "ALL_PLATFORMS"to its entry intoolkit/components/uniffi-bindgen-gecko-js/config.toml, so that its C++ scaffolding is not disabled on GeckoView.List its
Rust{udl-name}.sys.mjsfile outside of the desktop-only block incomponents/moz.build.