Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

One thing articles like this always miss, and what I'm most keen on, is how exactly the bridging works. Is there a standard Rust wasm crate that exposes the DOM? Do you pass some struct with function pointers? Is the Rust code exposed as a black box with no connection to the outside world and you just call a few entrypoint functions? Can you call Rust from JS and JS from Rust? If you go JS->Rust->JS->Rust->JS and the innermost JS function throws an exception, is all that properly propagated up the stack?


There are two low-level bridges: js-sys provides bindings to all the ECMAScript stuff, and web-sys provides bindings to all of the rest of a browser environment, including the DOM.

> Can you call Rust from JS and JS from Rust?

Yes.

I personally have written some JS code that returns a promise, wrapped in wasm that turned it into a Rust Future, then converted that Future into a promise that I've returned to Javascript.

> If you go JS->Rust->JS->Rust->JS and the innermost JS function throws an exception, is all that properly propagated up the stack?

Sort of. Rust doesn't use exceptions, but the binding converts a JavaScript exception to a Rust "result" type and vice versa at the boundary, so if you propogate it, it will get properly propagated.


The Rollup plugin linked to in the article produces JS functions that call into WASM code. JS objects are automatically converted to Rust structs via Serde and vice-versa.

I use TypeScript / Svelte for DOM interactions and only call into Rust code for the state updates (treating it like a black box).




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

Search: