Skip to content

Commit

Permalink
Arithmetic working with WASM! (#188)
Browse files Browse the repository at this point in the history
This is the first cut of a Rust generator, enough to run the
`fixture/arithmetic`.

Unfortunately due to mozilla/uniffi-rs#2329 not
yet released, we have to introduce a procmacro version of the fixture.

In this PR:

1. Introduce a Rust crate generator; this uses 
2. a runtime which uses typealiasing and import renaming to use
`wasm-bindgen` indirectly in the generated code.
3. the generated code needs a CallStatus to be defined in Rust; this
differs from the JSI version where the CallStatus is defined in
typescript. So the `rust-call.ts` module is made into a class with three
methods instead of three functions.
4. the generated Rust is then compiled and put through wasm-bindgen-cli
where it generates a low-level JS file.
5. the high level typescript now uses the flavor to decide whether it
uses the JSI generated typescript or the WASM, and which implementation
of the CallStatus.
6. Uses `tsx` to run the whole thing.
  • Loading branch information
jhugman authored Dec 17, 2024
1 parent a241dfb commit 093bb40
Show file tree
Hide file tree
Showing 40 changed files with 1,320 additions and 132 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ jobs:
- name: Installing
run: cargo xtask bootstrap yarn

- name: Installing WASM
run: rustup target add wasm32-unknown-unknown

- name: Installing wasm-bindgen
run: cargo install wasm-bindgen-cli

- name: Run tests of generated WASM bindings
run: ./scripts/run-tests.sh --flavor wasm

Expand Down
18 changes: 17 additions & 1 deletion crates/ubrn_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = []
wasm = []
wasm = ["quote", "prettyplease", "syn", "proc-macro2"]

[dependencies]
anyhow = { workspace = true }
Expand All @@ -23,3 +23,19 @@ uniffi_meta = { workspace = true }
extend = "1.2.0"
topological-sort = "0.2.2"
toml = "0.5"

[dependencies.quote]
version = "1.0"
optional = true

[dependencies.prettyplease]
version = "0.2"
optional = true

[dependencies.syn]
version = "2.0"
optional = true

[dependencies.proc-macro2]
version = "1.0"
optional = true
20 changes: 20 additions & 0 deletions crates/ubrn_bindgen/src/bindings/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/
use anyhow::Result;

use crate::{AbiFlavor, ModuleMetadata, SwitchArgs};

use super::gen_cpp;
#[cfg(feature = "wasm")]
use super::gen_rust;

pub fn generate_entrypoint(switches: &SwitchArgs, modules: &Vec<ModuleMetadata>) -> Result<String> {
match &switches.flavor {
AbiFlavor::Jsi => gen_cpp::generate_entrypoint(modules),
#[cfg(feature = "wasm")]
AbiFlavor::Wasm => gen_rust::generate_entrypoint(modules),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extern "C" {
) : callInvoker(invoker), props() {
// Map from Javascript names to the cpp names
{%- for func in ci.iter_ffi_functions_js_to_cpp() %}
{%- let name = func.name() %}
{%- let name = func.name()|fmt("ubrn_{}") %}
props["{{ name }}"] = jsi::Function::createFromHostFunction(
rt,
jsi::PropNameID::forAscii(rt, "{{ name }}"),
Expand Down
11 changes: 11 additions & 0 deletions crates/ubrn_bindgen/src/bindings/gen_rust/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct RustConfig {}
Loading

0 comments on commit 093bb40

Please sign in to comment.