This prepares for exposing operations like Memory.Grow while keeping the ability to trace what did that, by adding a `context.Context` initial parameter. This adds this to all API methods that mutate or return mutated data. Before, we made a change to trace functions and general lifecycle commands, but we missed this part. Ex. We track functions, but can't track what closed the module, changed memory or a mutable constant. Changing to do this now is not only more consistent, but helps us optimize at least the interpreter to help users identify otherwise opaque code that can cause harm. This is critical before we add more functions that can cause harm, such as Memory.Grow. Signed-off-by: Adrian Cole <adrian@tetrate.io>
Rust allocation example
This example shows how to pass strings in and out of a Wasm function defined
in Rust, built with cargo build --release --target wasm32-unknown-unknown
Ex.
$ go run greet.go wazero
Hello, wazero!
Under the covers, lib.rs does a few things of interest:
- Uses a WebAssembly-tuned memory allocator: wee_alloc.
- Exports wrapper functions to allocate and deallocate memory.
- Uses
&strinstead of CString (NUL-terminated strings). - Uses
std::mem::forgetto prevent Rust from eagerly freeing pointers returned.
Note: We chose to not use CString because it keeps the example similar to how you would track memory for arbitrary blobs. We also watched function signatures carefully as Rust compiles different WebAssembly signatures depending on the input type. All of this is Rust-specific, and wazero isn't a Rust project, but we hope this gets you started. For next steps, consider reading the Rust and WebAssembly book.