-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Question about strings and structs and wasmtime API #891
Comments
Most of questions above can be answered by Rust's type layout and ABI discussion. Bottom line is you need to known both to call Rust functions directly without wasm-bindgen or interface types. You can make your life easier and use
There are examples about embedding the wasmtime into custom project, see e.g. "gcd" and "memory". (You may also find the example at #39 (comment) relatively useful -- it shows how C compiled code can send a string to host function)
You have to call Rust a helper function to allocate a memory for a Person struct, then you populate it based on its
If I'm not mistaken, first two are pointer and length of @eminence does this answer all of your questions? |
This helps at lot, thanks. In retrospect I should have realized that this is how things work. I had gotten myself confused with several things. I think you already answered my follow up question, but just to confirm: The long-term approach to calling rust code (compiled to wasm) from wasmtime is with interface types? (And not with the a C or Rust ABI) |
That's correct. See more discussions on the subject at #677 Closing the issue as answered. |
Thanks! |
I've got the following rust code:
that I'm compiling to the
wasm32-unknown-unknown
target:To my surprise, this compiled happily. Since strings and structs are not types that webassembly knows about, I was surprised to even see a successful compilation. My understanding was that in order to deal with these types you either have to use wasm-bindgen, or interface types.
I'm not using wasm-bindgen, and I'm not using interface types (since when I look at the output, I don't see any of the interface types custom sections).
So my first question is: how is this actually working in the first place?
I'm not fluent in reading wasm, but when I looked at the generated code (attached in full at the bottom, but quoted in brief here), it all looked quite reasonable:
This seems reasonable: The parameter looks to be an offset into wasm linear memory to some structure that expects the 'age' field to be at offset 12 from the start of the structure.
My next question: how do I call this from the wasmtime API? How to I construct an instance of my
Person
struct and put it into the right memory? How do I know how to layout my struct?A similar question applies to
greet
:It looks like string types are transferred using 3 i32s (how do I create those from a string), but where's the return type?
Thanks!
plugin.wat.txt
The text was updated successfully, but these errors were encountered: