Memory

Memory

interface Memory

Defines runtime memory for use by a wasm program, along with facilities for accessing and mutating memory by the host JVM application.

From the docs:

A memory instance is the runtime representation of a linear memory. It holds a vector of bytes and an optional maximum size, if one was specified at the definition site of the memory.


meminst ::= {data vec(byte), max u32?}

The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant 65536 – abbreviated 64Ki. Like in a memory type, the maximum size in a memory instance is given in units of this page size.

The bytes can be mutated through memory instructions, the execution of a data segment, or by external means provided by the embedder.

It is an invariant of the semantics that the length of the byte vector, divided by page size, never exceeds the maximum size, if present.

Fields

Name Description
abstract sizePages: Int

The size of the Memory as a number of pages, where each page is PAGE_SIZE bytes.

open sizeBytes: Int

Returns the size of the Memory as the total number of bytes available.

Note: this is equivalent to sizePages * PAGE_SIZE.

Methods

lock

abstract suspend fun <T> lock(block: suspend Memory.()->T): T

Runs the block within a thread-safe mutual exclusion.

Parameters

Name Description
block: suspend Memory.()->T

ReturnValue

Name Description
T

readInt

abstract fun readInt(offset: Int, byteWidth: Int, alignment: Int): Int

Reads an Int from the Memory at the given offset.

Parameters

Name Description
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Int

readUInt

abstract fun readUInt(offset: Int, byteWidth: Int, alignment: Int): UInt

Reads a UInt from the Memory at the given offset.

Parameters

Name Description
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
UInt

readLong

abstract fun readLong(offset: Int, byteWidth: Int, alignment: Int): Long

Reads a Long from the Memory at the given offset.

Parameters

Name Description
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Long

readULong

abstract fun readULong(offset: Int, byteWidth: Int, alignment: Int): ULong

Reads a ULong from the Memory at the given offset.

Parameters

Name Description
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
ULong

readFloat

abstract fun readFloat(offset: Int, alignment: Int): Float

Reads a Float from the Memory at the given offset.

Parameters

Name Description
offset: Int
alignment: Int

ReturnValue

Name Description
Float

readDouble

abstract fun readDouble(offset: Int, alignment: Int): Double

Reads a Double from the Memory at the given offset.

Parameters

Name Description
offset: Int
alignment: Int

ReturnValue

Name Description
Double

writeInt

abstract fun writeInt(value: Int, offset: Int, byteWidth: Int, alignment: Int)

Writes an Int to the Memory at the given offset.

Parameters

Name Description
value: Int
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Unit

writeUInt

abstract fun writeUInt(value: UInt, offset: Int, byteWidth: Int, alignment: Int)

Writes a UInt to the Memory at the given offset.

Parameters

Name Description
value: UInt
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Unit

writeLong

abstract fun writeLong(value: Long, offset: Int, byteWidth: Int, alignment: Int)

Writes a Long to the Memory at the given offset.

Parameters

Name Description
value: Long
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Unit

writeULong

abstract fun writeULong(value: ULong, offset: Int, byteWidth: Int, alignment: Int)

Writes a ULong to the Memory at the given offset.

Parameters

Name Description
value: ULong
offset: Int
byteWidth: Int
alignment: Int

ReturnValue

Name Description
Unit

writeFloat

abstract fun writeFloat(value: Float, offset: Int, alignment: Int)

Writes a Float to the Memory at the given offset.

Parameters

Name Description
value: Float
offset: Int
alignment: Int

ReturnValue

Name Description
Unit

writeDouble

abstract fun writeDouble(value: Double, offset: Int, alignment: Int)

Writes a Double to the Memory at the given offset.

Parameters

Name Description
value: Double
offset: Int
alignment: Int

ReturnValue

Name Description
Unit

readBytes

abstract fun readBytes(out: ByteArray, memoryOffset: Int, outOffset: Int, length: Int): Int

Reads length bytes starting at the given memoryOffset from the Memory into the supplied ByteArray, starting at outOffset and returns the number of bytes which were able to be read.

Parameters

Name Description
out: ByteArray
memoryOffset: Int
outOffset: Int
length: Int

ReturnValue

Name Description
Int

writeBytes

abstract fun writeBytes(value: ByteArray, offset: Int, valueOffset: Int, valueLength: Int)

Writes a ByteArray into the Memory at the given offset.

Optional arguments valueOffset and valueLength allow the caller to specify a subsequence of bytes from the value to write. Defaults for these arguments result in the entirety of the value being written.

Parameters

Name Description
value: ByteArray
offset: Int
valueOffset: Int
valueLength: Int

ReturnValue

Name Description
Unit

growBy

abstract fun growBy(newPages: Int): Int

Grows the Memory by the specified number of newPages (each page's size = PAGE_SIZE).

Parameters

Name Description
newPages: Int

ReturnValue

Name Description
Int

the size of the Memory before the grow operation if the grow was successful, else GROW_FAILURE.

CompanionObject

Memory

interface Memory

Defines runtime memory for use by a wasm program, along with facilities for accessing and mutating memory by the host JVM application.

From the docs:

A memory instance is the runtime representation of a linear memory. It holds a vector of bytes and an optional maximum size, if one was specified at the definition site of the memory.


meminst ::= {data vec(byte), max u32?}

The length of the vector always is a multiple of the WebAssembly page size, which is defined to be the constant 65536 – abbreviated 64Ki. Like in a memory type, the maximum size in a memory instance is given in units of this page size.

The bytes can be mutated through memory instructions, the execution of a data segment, or by external means provided by the embedder.

It is an invariant of the semantics that the length of the byte vector, divided by page size, never exceeds the maximum size, if present.

Fields

Name Description
const PAGE_SIZE: Int
const GROW_FAILURE: Int

Methods

pagesForBytes

fun pagesForBytes(bytes: Long): Int

Returns the minimum number of pages required to hold the specified number of bytes.

Parameters

Name Description
bytes: Long

ReturnValue

Name Description
Int