Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write to the memory in wasmer-go?

Tags:

go

webassembly

According to this issue on the wasmer-go github, it used to be that you could use instance.Memory but this appears to be missing from newer versions. I can do instance.Exports.GetMemory("mem") but this does not allow me to write to it. My question is: how do I write to this memory?

Thank you for any help!

Just so you know: this is to pass strings to a wasm function

like image 561
8MB_SSD Avatar asked Oct 18 '25 15:10

8MB_SSD


1 Answers

The way I have made this work is with an exported allocate function (__alloc in AssemblyScript or malloc in WASM compiled from Go), use this exported function to allocate the memory and it returns the starting address. Use instance.Exports.GetMemory("mem").Data() to get the byte array of the memory, start at the address returned from the allocate function, and just manually overwrite the bytes in the array with the bytes from the string. I know this is not the best way but it is the way that works without needing changes to wasmer-go. Keep in mind different modules may use different utf standards (Go uses 8, AssemblyScript 16), I handle this with an export that is read on instance creation.

like image 129
alexpitsikoulis Avatar answered Oct 20 '25 07:10

alexpitsikoulis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!