-
Notifications
You must be signed in to change notification settings - Fork 925
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
accessing memory within wasm module #1824
Comments
Why do you need to do this? |
I mean accessing the wasm linear memory in particular, for example on the native side I want to write some data to the linear memory and read it on the wasm side, or vice versa. I don't mean raw memory, only the wasm linear memory. Looking at this issue #411, the solution seems to be similar to what I'd like. Would |
alright, I think I almost answered my own question. Compiling this code with
then running this wasmer code:
shows that the buffer starts at offset
Any idea what's going wrong here? What is |
hey! I've made a similar code where I could manipulate the data within wasm module and outside without errors: This is my wasm module, it has 3 functions package main
var buff [1024]byte
func main() {}
//go:export read
func read(start int32, til int32) []byte {
return buff[start : start+til]
}
//go:export starts_at
func starts_at() *byte {
return &buff[0]
}
//go:export mul
func mul() {
buff[2] = byte(buff[0] * buff[1])
} Using the generated wasm from the code above in the code bello: // ... instantiating wasmer engine, store, setup wasi_unstable_imports
startsAt, err := inst.Exports.GetFunction("starts_at")
check(err)
s, err := startsAt()
check(err)
fmt.Println("wasm buff[0]", s)
offset := s.(int32)
mem, err := inst.Exports.GetMemory("memory")
check(err)
memory := mem.Data()
copy(memory[offset:offset+1], []byte{0x02}) // add 2 to wasm buff at position 0
copy(memory[offset+1:offset+2], []byte{0x03}) // add 3 to wasm buff at position 1
fmt.Println(memory[offset:offset+3])
mulfn, err := inst.Exports.GetFunction("mul")
check(err)
// calling wasm mul exported function that will access
_, err = mulfn()
check(err)
fmt.Println(memory[offset:offset+3]) The outuput: wasm buff[0] 65741
[2 3 0] 131072 131072
[2 3 6] 131072 131072 The wasm memory could be manipulated from inside and outside the wasm module. |
@noot I think the error you reported:
is a kind of index out of range error because:
|
@EclesioMeloJunior thanks for the info, revisited my code and got it working now with printing a string via an imported go function that's passed via the memory:
output is as expected:
I think I can go ahead and close this now, it's still unclear how tinygo determines the layout of the module memory but I'll try to figure that out separately :) |
Hey, I was wondering if it's possible to access the wasm memory in go code that'll be compiled to wasm. eg something like:
If this is supported are there any docs on this? If not, is there some workaround you might suggest? Thanks!
The text was updated successfully, but these errors were encountered: