Skip to content

[Question] Convenient way of accessing complex structs from memory in wasm #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andizimmerer opened this issue Jul 2, 2018 · 6 comments

Comments

@andizimmerer
Copy link

andizimmerer commented Jul 2, 2018

Hi all,

I am currently learning Rust+WebAssembly and I came across a common problem. When accessing the memory of my wasm application from JavaScript I use Uint8Arrays and so on which, works fine most of the time. But this becomes quite complex when I have a struct or even an Vec of structs in my memory. For now, I am extracting the fields of the structs manually by utilizing e.g. Uint8Arrays but I was wondering if there is a more convenient way of doing this? Did I miss something there or is there an easier approach?

What I have in my mind is something like an automatically generated JavaScript class based on the struct in Rust that you can pass a pointer to. This class might be just a wrapper around accessing the memory parts directly, but it would be nice to have.

Is there something similar to this already there?

Oh and btw: If this is anyhow helpful for you, I assembled a Docker image with all the tools installed for doing the "game of life" tutorial including the new wasm32-unknown-unknown toolchain. You can find it here https://hub.docker.com/r/jibbow/rust-wasm32-unknown-unknown/

@andizimmerer andizimmerer changed the title Convenient way of accessing complex structs from memory in wasm [Question] Convenient way of accessing complex structs from memory in wasm Jul 2, 2018
@fitzgen
Copy link
Member

fitzgen commented Jul 2, 2018

So your struct is still basically #[repr(C)], correct? And you'd like to have glue for accessing each member of it, given a pointer into wasm memory (that is perhaps within a vec's elements)?

The general idea sounds appealing, but I think we need some design work done to hash out all the implementation details.

+cc @alexcrichton

@alexcrichton
Copy link
Contributor

Indeed it'd be great to support this! This is similar in spirit to #111 but perhaps not the exact same.

Currently as Rust objects that live in JS are actually just wrappers around an integer pointer. The underlying Rust object, however, is typically RefCell<T> rather than just a bare T for safety. The bindings would have to be tweaked for an array view to only yield read-only access but it could possibly be done!

It's safe to say, regardless, that this isn't implemented today so you're not missing any features!

@andizimmerer
Copy link
Author

Yes, that's exactly what I had in my mind @fitzgen !

I guess a good starting point would be an enhanced and automatically generated JavaScript class for the structs exported from Rust that accepts a location in memory and wraps access to the struct's fields in a nice way by hiding calls to array views.

I am not sure if I can grasp the whole implications of such a feature, but as far as I understand making it read-only would certainly be nice, but also would not change that much? Please correct me if I'am wrong as I have just started with wasm, but as of now you can easily edit the memory of wasm applications directly via JavaScript without caring about the ownership system of Rust (I never tried to do it, though)? So adding a nice wrapper in JS wouldn't change anything here?

Nevertheless, having it read-only is of course a better solution @alexcrichton :)

@alexcrichton
Copy link
Contributor

Ah so the readonly piece is sort of about reentrancy across the language boundary (a fancy way of saying JS calls Rust which calls JS which calls back into Rust). That's a case where you can relatively easily violate Rust's guarantees about &mut pointers and cause weird crashes. Rust is centered around catching as many of these crashes ahead of time!

In any case though the read-only is mostly just a detail, this'd still need to be fleshed out regardless!

@fitzgen
Copy link
Member

fitzgen commented Jul 3, 2018

This is perhaps another candidate for an RFC, once we agree on an RFC process

@alexcrichton
Copy link
Contributor

I'm gonna close this for now as I think the question was answered, but we can of course file follow-up issues if necessary!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants