Class MemoryWriter
java.lang.Object
ai.tegmentum.webassembly4j.runtime.marshal.MemoryWriter
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
ConstructorsConstructorDescriptionMemoryWriter(ai.tegmentum.webassembly4j.api.Memory memory, MemoryAllocator allocator) Creates a new MemoryWriter. -
Method Summary
Modifier and TypeMethodDescriptionvoidwriteBool(int offset, boolean value) Writes a boolean value at the given offset.voidwriteBytes(int offset, byte[] value) Writes a byte array at the given offset as a (pointer, byte_length) pair.voidwriteF32(int offset, float value) Writes a 32-bit float at the given offset (little-endian).voidwriteF64(int offset, double value) Writes a 64-bit float at the given offset (little-endian).voidwriteI32(int offset, int value) Writes a 32-bit integer at the given offset (little-endian).voidwriteI64(int offset, long value) Writes a 64-bit integer at the given offset (little-endian).voidwriteString(int offset, String value) Writes a string at the given offset as a (pointer, byte_length) pair.
-
Constructor Details
-
MemoryWriter
Creates a new MemoryWriter.- Parameters:
memory- the linear memory to write toallocator- 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 memoryvalue- 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 memoryvalue- 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 memoryvalue- 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 memoryvalue- 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 memoryvalue- the double value
-
writeString
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 writtenvalue- 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 writtenvalue- the byte array to write
-