wasmedge-hs-0.17.1.0: Haskell bindings for the WasmEdge WebAssembly runtime
Copyright(c) 2026 Tushar Adhatrao
LicenseMIT
Stabilityexperimental
Safe HaskellNone
LanguageGHC2021

WasmEdge.Memory.Codec

Description

WebAssembly's type system only has four numeric types and references. Every complex value — Text, ByteString, a list, or any ADT — must be transported through linear memory: the Wasm module's heap.

The canonical pattern (used by the Rust and Go SDKs) is:

  1. The guest (Wasm) module exports alloc(size: i32) -> i32 and dealloc(ptr: i32, len: i32).
  2. The host passes a (ptr: i32, len: i32) pair for every buffer argument.
  3. Both sides serialise complex data to/from a flat byte representation.

WasmMemory standardises step 3 on the Haskell side.

Encoding conventions

  • Primitive numeric types use little-endian binary encoding (matches Wasm's native byte order).
  • Text is encoded as UTF-8 bytes.
  • ByteString is passed as-is (zero-copy where possible).
  • Complex ADTs should implement WasmMemory using jsonWasmPoke / jsonWasmPeek, which serialises via aeson JSON — the same convention used by wit-bindgen and the WASI ecosystem.

Typical usage

-- Host reads a Text value from a Wasm linear memory buffer:
txt <- wasmPeek @Text mem offset len

-- Host writes an ADT back into linear memory (assuming a JSON instance):
Right n <- wasmPoke mem retOffset myRecord

See Alloc for the high-level bracket that combines allocation, marshalling, and function invocation in one call.

Synopsis

Core typeclass

class WasmMemory a where #

A type that can be serialised to and deserialised from a contiguous slice of WebAssembly linear memory.

Minimal complete definition: wasmPeek + wasmPoke.

For fixed-size types (primitives, fixed-size tuples), implement FixedWasmMemory and use fixedPeek / fixedPoke as the default methods.

For JSON-serialisable ADTs use jsonWasmPeek / jsonWasmPoke.

Methods

wasmPeek #

Arguments

:: MemoryInstance

The linear memory instance

-> Word32

Byte offset in linear memory

-> Word32

Byte length of the slice

-> IO (WasmResult a) 

Read a value of type a from len bytes of linear memory at offset. Returns Left on out-of-bounds access or deserialisation failure.

wasmPoke #

Arguments

:: MemoryInstance

The linear memory instance

-> Word32

Byte offset to write at

-> a

The value to serialise

-> IO (WasmResult Word32)

Right bytes written, or Left error

Write a value of type a into linear memory at offset. Returns Right n where n is the number of bytes written, or Left on out-of-bounds access.

Instances

Instances details
WasmMemory ByteString #

ByteString — raw bytes, zero-copy.

The entire slice [offset .. offset+len) is returned/written as-is. Writing an empty ByteString is a no-op (returns Right 0).

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int16 #

Int16 — 2 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int32 #

Int32 — 4 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int64 #

Int64 — 8 bytes, little-endian two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Int8 #

Int8 — 1 byte, two's complement.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word16 #

Word16 — 2 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word32 #

Word32 — 4 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word64 #

Word64 — 8 bytes, little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Word8 #

Word8 — 1 byte, identity encoding.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Text #

Text — UTF-8 encoded bytes.

The codec is strict UTF-8: an invalid byte sequence returns Left with a WasmEdgeError rather than silently replacing bytes with the replacement character. Use 'WasmMemory ByteString' if you need raw bytes. Writing empty Text is a no-op (returns Right 0).

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Bool #

Bool — 1 byte: 0 = False, 1 = True.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Double #

Double — 8 bytes, IEEE 754 double-precision little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory Float #

Float — 4 bytes, IEEE 754 single-precision little-endian.

Instance details

Defined in WasmEdge.Memory.Codec

(ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

WasmMemory [Word8] #

[Word8] — raw bytes (wraps via ByteString).

Instance details

Defined in WasmEdge.Memory.Codec

(FixedWasmMemory a, FixedWasmMemory b) => WasmMemory (a, b) #

A pair of fixed-size values is stored sequentially: a at offset, b at offset + wasmWidth @a.

Instance details

Defined in WasmEdge.Memory.Codec

Fixed-size subclass

class WasmMemory a => FixedWasmMemory a where #

A WasmMemory type with a statically-known byte width. Implement this for all primitive types.

Methods

wasmWidth :: Proxy a -> Word32 #

Byte width of the serialised form.

serialise :: a -> ByteString #

Serialise to exactly wasmWidth bytes.

deserialise :: ByteString -> Either String a #

Deserialise from at least wasmWidth bytes.

Instances

Instances details
FixedWasmMemory Int16 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int32 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int64 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Int8 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word16 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word32 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word64 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Word8 # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Bool # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Double # 
Instance details

Defined in WasmEdge.Memory.Codec

FixedWasmMemory Float # 
Instance details

Defined in WasmEdge.Memory.Codec

JSON codec helpers (for custom ADTs)

jsonWasmPoke :: ToJSON a => MemoryInstance -> Word32 -> a -> IO (WasmResult Word32) #

Write any ToJSON value into linear memory as a UTF-8 JSON blob. Returns the number of bytes written.

jsonWasmPeek :: FromJSON a => MemoryInstance -> Word32 -> Word32 -> IO (WasmResult a) #

Read a UTF-8 JSON blob from linear memory and decode it as a FromJSON value.

newtype WasmJSON a #

Newtype wrapper to derive WasmMemory for any type with ToJSON and FromJSON via DerivingVia.

data Person = Person { name :: Text, age :: Int }
  deriving stock (Generic, Show, Eq)
  deriving anyclass (ToJSON, FromJSON)
  deriving (WasmMemory) via (WasmJSON Person)

Constructors

WasmJSON 

Fields

Instances

Instances details
FromJSON a => FromJSON (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

ToJSON a => ToJSON (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Show a => Show (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Methods

showsPrec :: Int -> WasmJSON a -> ShowS #

show :: WasmJSON a -> String #

showList :: [WasmJSON a] -> ShowS #

Eq a => Eq (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec

Methods

(==) :: WasmJSON a -> WasmJSON a -> Bool #

(/=) :: WasmJSON a -> WasmJSON a -> Bool #

(ToJSON a, FromJSON a) => WasmMemory (WasmJSON a) # 
Instance details

Defined in WasmEdge.Memory.Codec