-
|
How can I access WASM exported memory? I want to read and write some values in WASM memory, how can I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
I found it. val export = instance.exports.find { it.name == "memory" }
requireNotNull(export) { "Export 'memory' not found" }
val memory = requireNotNull(export.value as? Memory) { "Export '${export.name}' is not a memory" } |
Beta Was this translation helpful? Give feedback.
-
|
You might be better off with: val memory = instance.exports.firstNotNullOf { it.value as? Memory }Whilst most modules will export a memory with the name "memory", theres nothing in the spec (wasm) that tells a compiler to emit the export with that name. It's worth noting a couple of other edge cases:
What I would recommend doing:
wasm-tools print ./foo.wasm -o ./foo.wat
Exports can be inline like this (module
(memory $exported_memory (export "mem") 2)Or on their own line with a reference (module
(memory $exported_memory 2)
(export "mem" (memory $exported_memory))
)If there is only one export pointing to a memory you can use this val memory = instance.exports.firstNotNullOf { it.value as? Memory }If there is many its worth picking the one you want by name val memory = instance.exports.firstNotNullOf { it.name == "mem" } |
Beta Was this translation helpful? Give feedback.
I found it.