Class StringCodec

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

public final class StringCodec extends Object
Encodes and decodes strings to/from WebAssembly linear memory using UTF-8.

Follows the Component Model Canonical ABI convention where strings are represented as a (pointer, byte_length) pair in linear memory.

  • Method Details

    • encode

      public static int[] encode(String value, ai.tegmentum.webassembly4j.api.Memory memory, MemoryAllocator allocator)
      Encodes a string into WebAssembly linear memory.
      Parameters:
      value - the string to encode
      memory - the linear memory to write to
      allocator - the allocator to use for the string data
      Returns:
      a two-element array containing [pointer, byte_length]
    • decode

      public static String decode(ai.tegmentum.webassembly4j.api.Memory memory, int ptr, int byteLen)
      Decodes a string from WebAssembly linear memory.
      Parameters:
      memory - the linear memory to read from
      ptr - the pointer to the start of the string data
      byteLen - the length of the string data in bytes
      Returns:
      the decoded string