Class MemoryReader

java.lang.Object
ai.tegmentum.webassembly4j.runtime.marshal.MemoryReader

public final class MemoryReader extends Object
Reads typed values from WebAssembly linear memory.

Handles endianness conversion (WASM uses little-endian) and complex type unmarshalling.

Uses a reusable scratch buffer to avoid per-call byte array and ByteBuffer allocations in the hot path.

  • Constructor Summary

    Constructors
    Constructor
    Description
    MemoryReader(ai.tegmentum.webassembly4j.api.Memory memory)
    Creates a new MemoryReader.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    readBool(int offset)
    Reads a boolean value from the given offset.
    byte[]
    readBytes(int offset)
    Reads a byte array from the given offset.
    float
    readF32(int offset)
    Reads a 32-bit float from the given offset (little-endian).
    double
    readF64(int offset)
    Reads a 64-bit float from the given offset (little-endian).
    int
    readI32(int offset)
    Reads a 32-bit integer from the given offset (little-endian).
    long
    readI64(int offset)
    Reads a 64-bit integer from the given offset (little-endian).
    readString(int offset)
    Reads a string from the given offset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MemoryReader

      public MemoryReader(ai.tegmentum.webassembly4j.api.Memory memory)
      Creates a new MemoryReader.
      Parameters:
      memory - the linear memory to read from
  • Method Details

    • readBool

      public boolean readBool(int offset)
      Reads a boolean value from the given offset.
      Parameters:
      offset - the byte offset in linear memory
      Returns:
      the boolean value
    • readI32

      public int readI32(int offset)
      Reads a 32-bit integer from the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      Returns:
      the integer value
    • readI64

      public long readI64(int offset)
      Reads a 64-bit integer from the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      Returns:
      the long value
    • readF32

      public float readF32(int offset)
      Reads a 32-bit float from the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      Returns:
      the float value
    • readF64

      public double readF64(int offset)
      Reads a 64-bit float from the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      Returns:
      the double value
    • readString

      public String readString(int offset)
      Reads a string from the given offset.

      Expects a (pointer, byte_length) pair at the offset, then reads UTF-8 data from the pointed-to location.

      Parameters:
      offset - the byte offset of the (ptr, len) pair
      Returns:
      the decoded string
    • readBytes

      public byte[] readBytes(int offset)
      Reads a byte array from the given offset.

      Expects a (pointer, byte_length) pair at the offset, then reads the byte data from the pointed-to location.

      Parameters:
      offset - the byte offset of the (ptr, len) pair
      Returns:
      the byte array