#WASM memory reads & writes in Zig

1 messages · Page 1 of 1 (latest)

plain oyster
#

I've written a WASM module in WAT (its text format) and I want to write it in Zig to see how it compares. However I'm struggling to find information on how to read and write to host-provided memory. To illustrate what I mean, say I've got this WASM module that gets and sets values at certain indexes in the imported memory:

(module
  (import "env" "memory" (memory 1))
  (func (export "get_at") (param $index i32) (result i32)
    (return (i32.load8_u (local.get $index)))
  )
  (func (export "set_at") (param $index i32) (param $value i32)
    (i32.store8 (local.get $index) (local.get $value))
  )
)

I can get Zig to add the (import "env" "memory" (memory 1)) declaration with the --import-memory CLI flag in my zig build-exe command, but I don't know how to refer to said memory in Zig, much less read or write to it. I don't think I need allocators, since the host language ensures enough memory for the purpose of my module. In case relevant, here's what I use to compile:

zig build-exe -target wasm32-freestanding -O ReleaseSmall -fno-entry --import-memory --stack 8192 -rdynamic main.zig

How would I get Zig to output the example WASM module?

vale flame