kwasm.runtime

Kwasm.runtime

package kwasm.runtime

Classes

Name Description
sealed class Address

Addresses for FunctionInstances, Memorys, Tables, and Globals.

From the docs:

Function instances, table instances, memory instances, and global instances in the store are referenced with abstract addresses. These are simply indices into the respective store component.


addr       ::= 0|1|2|...
funcaddr   ::= addr
tableaddr  ::= addr
memaddr    ::= addr
globaladdr ::= addr

An embedder may assign identity to exported store objects corresponding to their addresses, even where this identity is not observable from within WebAssembly code itself (such as for function instances or immutable globals).

sealed class Export

Represents a runtime exported value.

From the docs:

An export instance is the runtime representation of an export. It defines the export’s name and the associated external value.


exportinst ::= {name name, value externval}

sealed class FunctionInstance

Represents either a WasmFunction from a ModuleInstance, or a HostFunction exposed to the ModuleInstance via imports.

From the docs:

A function instance is the runtime representation of a function. It effectively is a closure of the original function over the runtime module instance of its originating module. The module instance is used to resolve references to other definitions during execution of the function.


funcinst   ::= {type functype, module moduleinst, code func}
{type functype, hostcode hostfunc}
hostfunc ::= ...

A host function is a function expressed outside WebAssembly but passed to a module as an import.

sealed class Global

From the docs:

A global instance is the runtime representation of a global variable. It holds an individual value and a flag indicating whether it is mutable.


globalinst ::= {value val, mut mut}

The value of mutable globals can be mutated through variable instructions or by external means provided by the embedder.

data class IdentifierContext

From the docs:

Where I is a IdentifierContext:


I  ::=   { types       (id?),
funcs       (id?),
tables      (id?),
mems        (id?),
globals     (id?),
locals      (id?),
labels      (id?),
typedefs    functype   }

sealed class ImportExtern

Represents an external value imported by a WasmModule. Its address will be used to populate a

ModuleInstance so that lookups happen correctly at runtime.
  1. Parse WasmModules
  2. Build ImportExterns from the modules' declared imports.
  3. Allocate the modules with their import externs.
  4. Using the allocations, update the addresses from the import externs appropriately.
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.

data class ModuleInstance

Represents an instantiated kwasm.ast.module.WasmModule.

From the docs:

A module instance is the runtime representation of a module. It is created by instantiating a module, and collects runtime representations of all entities that are imported, defined, or exported by the module.


moduleinst ::= {
types functype*,
funcaddrs funcaddr*,
tableaddrs tableaddr*,
memaddrs memaddr*,
globaladdrs globaladdr*,
exports exportinst*
}

Each component references runtime instances corresponding to respective declarations from the original module – whether imported or defined – in the order of their static indices. Function instances, table instances, memory instances, and global instances are referenced with an indirection through their respective addresses in the store.

It is an invariant of the semantics that all export instances in a given module instance have different names.

data class ModuleAllocationResult

Result of allocating a module using WasmModule.allocate.

interface StackElement

Defines an element in one of the runtime Stacks.

data class Store

From the docs:

The store represents all global state that can be manipulated by WebAssembly programs. It consists of the runtime representation of all instances of functions, tables, memories, and globals that have been allocated during the life time of the abstract machine.

Syntactically, the store is defined as a record listing the existing instances of each category:


store  ::= {
funcs funcinst*,
tables tableinst*,
mems meminst*,
globals globalinst*
}

data class Table

Runtime representation of a WebAssembly Table.

From the docs:

A table instance is the runtime representation of a table. It holds a vector of function elements and an optional maximum size, if one was specified in the table type at the table’s definition site.

Each function element is either empty, representing an uninitialized table entry, or a function address. Function elements can be mutated through the execution of an element segment or by external means provided by the embedder.


tableinst ::= {elem vec(funcelem), max u32?}
funcelem ::= funcaddr?

It is an invariant of the semantics that the length of the element vector never exceeds the maximum size, if present.

interface Value

Defines a value for use in WebAssembly at runtime.

From the docs:

WebAssembly computations manipulate values of the four basic value types: integers and floating-point data of 32 or 64 bit width each, respectively.

In most places of the semantics, values of different types can occur. In order to avoid ambiguities, values are therefore represented with an abstract syntax that makes their type explicit. It is convenient to reuse the same notation as for the const instructions producing them:


val    ::= i32.const i32
i64.const i64
f32.const f32
f64.const f64

object EmptyValue

Represents an empty value. Should only ever be used as a return-type for a void/Unit kwasm.api.HostFunction.

inline class IntValue

Holds a 32-bit integer Value.

inline class LongValue

Holds a 64-bit integer Value.

inline class FloatValue

Holds a 32-bit floating-point Value.

inline class DoubleValue

Holds a 64-bit floating-point Value.

Subpackages

Fields

Name Description
val zeroValue: Value<*>

Gets the default zero Value for the receiving ValueType.

Methods

allocate

fun <T : Number> Store.allocate(globalType: GlobalType, value: T): Allocation<Global>

From the docs:

  1. Let globaltype be the global type to allocate and val the value to initialize the global with.
  2. Let mut t be the structure of global type globaltype.
  3. Let a be the first free global address in S.
  4. Let globalinst be the global instance {value val, mut mut}.
  5. Append globalinst to the globals of S.
  6. Return a.

Receiver

Name Description
Store

Parameters

Name Description
globalType: GlobalType
value: T

ReturnValue

Name Description
Allocation<Global>

allocate

fun Store.allocate(memoryProvider: MemoryProvider, memoryType: MemoryType): Allocation<Memory>

From the docs:

  1. Let memtype be the memory type to allocate.
  2. Let {min n, max m?} be the structure of memory type memtype.
  3. Let a be the first free memory address in S.
  4. Let meminst be the memory instance {data (0x00)^(n⋅64Ki), max m?} that contains n pages of zeroed bytes.
  5. Append meminst to the mems of S.
  6. Return a.

Receiver

Name Description
Store

Parameters

Name Description
memoryProvider: MemoryProvider
memoryType: MemoryType

ReturnValue

Name Description
Allocation<Memory>

allocate

fun WasmModule.allocate(validationContext: Module, memoryProvider: MemoryProvider, externalValues: List<ImportExtern<Address>>, store: Store): ModuleAllocationResult

Instantiates a WasmModule into a ModuleInstance given a Store.

From the docs:

  1. Let module be the module to allocate and externval*_im the vector of external values providing the module’s imports, and val* the initialization values of the module’s globals.
  2. For each function func_i in module.funcs, do:

    • Let funcaddr_i be the function address resulting from allocating func_i for the module instance moduleinst defined below.
  3. For each table table_i in module.tables, do:

    • Let tableaddr_i be the table address resulting from allocating table_i.type.
  4. For each memory mem_i in module.mems, do:

    • Let memaddr_i be the memory address resulting from allocating mem_i.type.
  5. For each global global_i in module.globals, do:

    • Let globaladdr_i be the global address resulting from allocating global_i.type with initializer value val*[i].
  6. Let funcaddr* be the the concatenation of the function addresses funcaddr_i in index order.
  7. Let tableaddr* be the the concatenation of the table addresses tableaddr_i in index order.
  8. Let memaddr* be the the concatenation of the memory addresses memaddr_i in index order.
  9. Let globaladdr* be the the concatenation of the global addresses globaladdr_i in index order.
  10. Let funcaddr*_mod be the list of function addresses extracted from externval*_im, concatenated with funcaddr*.
  11. Let tableaddr*_mod be the list of table addresses extracted from externval*_im, concatenated with tableaddr*.
  12. Let memaddr*_mod be the list of memory addresses extracted from externval*_im, concatenated with memaddr*.
  13. Let globaladdr*_mod be the list of global addresses extracted from externval*_im, concatenated with globaladdr*.
  14. For each export export_i in module.exports, do:

    • If export_i is a function export for function index x, then let externval_i be the external value func (funcaddr*_mod[x]).
    • Else, if export_i is a table export for table index x, then let externval_i be the external value table (tableaddr*_mod[x]).
    • Else, if export_i is a memory export for memory index x, then let externval_i be the external value mem (memaddr*_mod[x]).
    • Else, if export_i is a global export for global index x, then let externval_i be the external value global (globaladdr*_mod[x]).
    • Let exportinst_i be the export instance {name (export_i.name), value externval_i}.
  15. Let exportinst* be the the concatenation of the export instances exportinst_i in index order.
  16. Let moduleinst be the module instance { types (module.types), funcaddrs funcaddr*_mod, tableaddrs tableaddr*_mod, memaddrs memaddr*_mod, globaladdrs globaladdr*_mod, exports exportinst* }.
  17. Return moduleinst.

Receiver

Name Description
WasmModule

Parameters

Name Description
validationContext: Module
memoryProvider: MemoryProvider
externalValues: List<ImportExtern<Address>>
store: Store

ReturnValue

Name Description
ModuleAllocationResult

allocate

fun Store.allocate(tableType: TableType): Allocation<Table>

From the docs:

  1. Let tabletype be the table type to allocate.
  2. Let ({min n, max m?} elemtype) be the structure of table type tabletype.
  3. Let a be the first free table address in S.
  4. Let tableinst be the table instance {elem(ϵ)^n, max m?} with n empty elements.
  5. Append tableinst to the tables of S.
  6. Return a.

Receiver

Name Description
Store

Parameters

Name Description
tableType: TableType

ReturnValue

Name Description
Allocation<Table>

collectImportExterns

fun WasmModule.collectImportExterns(): List<ImportExtern<Address>>

Transforms a WasmModule's raw list of Imports into ImportExterns.

Receiver

Name Description
WasmModule

ReturnValue

Name Description
List<ImportExtern<Address>>

Table

fun Table(tableType: TableType, builder: (MutableMap<Int, Function?>)->Unit): Table

Builder to create a Table given a TableType and a builder function used to add elements to the Table's elements.

Parameters

Name Description
tableType: TableType
builder: (MutableMap<Int, Function?>)->Unit

ReturnValue

Name Description
Table

toValue

fun UInt.toValue(): IntValue

Wraps a UInt in an IntValue.

Receiver

Name Description
UInt

ReturnValue

Name Description
IntValue

toValue

fun <T : Number> T.toValue(): Value<*>

Wraps the receiving value as a Value instance.

Receiver

Name Description
T

ReturnValue

Name Description
Value<*>

checkType

fun Value<*>.checkType(expected: ValueType)

Checks the receiving Value against an expected ValueType

Receiver

Name Description
Value<*>

Parameters

Name Description
expected: ValueType

ReturnValue

Name Description
Unit

isType

fun Value<*>.isType(expected: ValueType): Boolean

Checks the receiving Value against an expected ValueType

Receiver

Name Description
Value<*>

Parameters

Name Description
expected: ValueType

ReturnValue

Name Description
Boolean

toValue

fun Int.toValue(): IntValue

Wraps an Int in an IntValue.

Receiver

Name Description
Int

ReturnValue

Name Description
IntValue

toValue

fun Long.toValue(): LongValue

Wraps a Long in a LongValue.

Receiver

Name Description
Long

ReturnValue

Name Description
LongValue

toValue

fun Float.toValue(): FloatValue

Wraps a Float in a FloatValue.

Receiver

Name Description
Float

ReturnValue

Name Description
FloatValue

toValue

fun Double.toValue(): DoubleValue

Wraps a Double in a DoubleValue.

Receiver

Name Description
Double

ReturnValue

Name Description
DoubleValue

toValueType

fun KClass<Value<*>>.toValueType(): ValueType?

Converts a KClass for a Value class to a ValueType.

Receiver

Name Description
KClass<Value<*>>

ReturnValue

Name Description
ValueType?