Hacker News new | past | comments | ask | show | jobs | submit login
Autogenerating Rust-JS Bindings with UniFFI (hacks.mozilla.org)
92 points by naansequitur on Aug 10, 2023 | hide | past | favorite | 16 comments



I'm not a rust dev, but this language is moving so fast into new territories that I'm more and more interested in picking it up.

Could UniFFI be used to generate native modules to use rust with react native?

The web story with wasm already looks like it could eat away a big chunk of JS's share if WebIDL becomes a thing.


Yes, you can do that. UniFFI allows you to generate Kotlin and Swift wrappers, which you can wrap in a RN wrapper. We did this at work for a cross-platform mobile app, it's a bit of a headache to create because that are some subtleties with async or these kind of things, and they are handled differently in the UniFFI wrappers and RN wrapper, but for relatively simple libs I find it pretty convenient.


Wouldn't this negate the effort of having cross platform Rust Module that now you have to manually create RN wrapper to autogenerated Kotlin and Swift wrappers? Also I think all previous RN bindings from JS to Swift, Java are async in nature end comes with performance penalty (serialization back and forth and async queue)?

Would be great if React Native team extend TurboModules to support generating Rust interop instead of just C++ - there are many truely cross platform rust modules (including support for iOS/android) than c++ and cargo makes much better developer experience than cmake.


What would be really awesome would be a tool for generating JSI bindings to Rust code. Then you wouldn't need to go via Kotlin or Swift. I believe there are no technical barriers to such a tool. Someone just needs to implement it.


maybe some ideas about interop can be taken from flutter module:

https://github.com/fzyzcjy/flutter_rust_bridge


Most of the details for doing this are over my head but wow what a nerd-snipe!


If you're looking for somewhere to start, then this repo which does some manual Rust <-> JSI integration might be a good one https://github.com/triniwiz/nativescript-mason


I built a proof of concept with UniFFI recently at work. The futures/async support is quite immature and for me meant declaring part of my functions in the UDL file and partly in proc macros, but it's not clear what is supported where. It's still a little early, but is generally a really cool project.


I'm using https://koffi.dev for running rust-compiled code in Node.js, so if this -in any way- means there's going to be something that just works(tm) as part of the standard JS stack, rather than having to rely on third parties to "mostly" implement FFI, that'd be amazing.


Similar to the PythonMonkey project which provides high level bindings to SpiderMonkey from Python. Although its implemented in C++, I wonder if it or other projects like it could benefit from UniFFi

PythonMonkey lets you arbitrarily execute JS code like:

``` import pythonmonkey pythonmonkey.eval("(() => { console.log('hi from js'); })()") ``` for reference.


Did switching from "everything is synchronous" to "everything is asynchronous" break a lot of other UniFFI users? I keep hearing people recommend UniFFI as the solution for Easy-FFI™, but that sounds like a drastic change that might break folks downstream.


This what I actually want LLMs to do. Generate spec adhering code from one language to another


why is this big news weve had this stuff for decades now ?


We’ve had a library for generating JS bindings for Rust code for decades?


Agreed, I doubt it's been a decade.. though maybe close-ish

wasm-bindgen does something similar though, right?


It does, but wasm-bindgen is currently missing some features that I personally think are necessary for widespread adoption. Things like being able to have a `Vec<T>` without venturing into type unsafe land and JSON.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: