Skip to content
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

Add an example to show how to read bytes from wasm memory #34

Open
AntsyLich opened this issue Nov 13, 2024 · 4 comments
Open

Add an example to show how to read bytes from wasm memory #34

AntsyLich opened this issue Nov 13, 2024 · 4 comments

Comments

@AntsyLich
Copy link

No description provided.

@CharlieTap
Copy link
Owner

Hey, I'll be adding docs as part of the 1.0 coming up in the couple of months but maybe I can answer your question in interim. You'll need to get a reference to the memory itself first, this is done by calling:

val exports = exports(instance)

Provided your module is in fact exporting the memory, the you can call:

val memory = exports.filterIsInstance<Memory>().first()
val buffer = ByteArray()
readBytes(store, memory, buffer, pointer, bytesToRead)

Theres also a way to create a memory and import it into your module at instantiation time, but its probably easier to just export what you already have.

@AntsyLich
Copy link
Author

Unrelated but does K/Wasm support exporting memory? There's seems to be a very recent issue which mentions importing/exporting memory (https://youtrack.jetbrains.com/issue/KT-73234)

@CharlieTap
Copy link
Owner

Yes, it exports the memory (theres only one at present) with the name "memory". You can check for yourself by turning your .wasm file into a .wat file using a cli tool like wasm-tools:

wasm-tools parse ./module.wasm

You should see an export entry like this:

(export "memory" (memory 0))

@mipastgt
Copy link

I'd very much appreciate it if you could provide a complete but simple example of how to call a wasm function which takes a ByteArray as input and returns another ByteArray as output. That would open the door for testing chasm with non-trivial code examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants