Class MemoryWriter

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

public final class MemoryWriter extends Object
Writes typed values to WebAssembly linear memory.

Handles endianness conversion (WASM uses little-endian) and complex type marshalling through the provided MemoryAllocator.

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

  • Constructor Summary

    Constructors
    Constructor
    Description
    MemoryWriter(ai.tegmentum.webassembly4j.api.Memory memory, MemoryAllocator allocator)
    Creates a new MemoryWriter.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    writeBool(int offset, boolean value)
    Writes a boolean value at the given offset.
    void
    writeBytes(int offset, byte[] value)
    Writes a byte array at the given offset as a (pointer, byte_length) pair.
    void
    writeF32(int offset, float value)
    Writes a 32-bit float at the given offset (little-endian).
    void
    writeF64(int offset, double value)
    Writes a 64-bit float at the given offset (little-endian).
    void
    writeI32(int offset, int value)
    Writes a 32-bit integer at the given offset (little-endian).
    void
    writeI64(int offset, long value)
    Writes a 64-bit integer at the given offset (little-endian).
    void
    writeString(int offset, String value)
    Writes a string at the given offset as a (pointer, byte_length) pair.

    Methods inherited from class java.lang.Object

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

    • MemoryWriter

      public MemoryWriter(ai.tegmentum.webassembly4j.api.Memory memory, MemoryAllocator allocator)
      Creates a new MemoryWriter.
      Parameters:
      memory - the linear memory to write to
      allocator - the allocator for complex types that need additional memory
  • Method Details

    • writeBool

      public void writeBool(int offset, boolean value)
      Writes a boolean value at the given offset.
      Parameters:
      offset - the byte offset in linear memory
      value - the boolean value
    • writeI32

      public void writeI32(int offset, int value)
      Writes a 32-bit integer at the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      value - the integer value
    • writeI64

      public void writeI64(int offset, long value)
      Writes a 64-bit integer at the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      value - the long value
    • writeF32

      public void writeF32(int offset, float value)
      Writes a 32-bit float at the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      value - the float value
    • writeF64

      public void writeF64(int offset, double value)
      Writes a 64-bit float at the given offset (little-endian).
      Parameters:
      offset - the byte offset in linear memory
      value - the double value
    • writeString

      public void writeString(int offset, String value)
      Writes a string at the given offset as a (pointer, byte_length) pair.

      The string data is UTF-8 encoded and allocated separately in linear memory. The (pointer, byte_length) descriptor is written at the given offset.

      Parameters:
      offset - the byte offset where the (ptr, len) pair is written
      value - the string to write
    • writeBytes

      public void writeBytes(int offset, byte[] value)
      Writes a byte array at the given offset as a (pointer, byte_length) pair.

      The byte data is allocated separately in linear memory. The (pointer, byte_length) descriptor is written at the given offset.

      Parameters:
      offset - the byte offset where the (ptr, len) pair is written
      value - the byte array to write