wasp_vm v0.8.1 WaspVM.HostFunction.API View Source

Provides an API for interacting with the VM from within a host function

Link to this section Summary

Functions

Returns x number of bytes from a given exported memory, starting at the specified address

Updates a given exported memory with the given bytes, at the specified address

Link to this section Functions

Link to this function

get_memory(pid, mem_name, address, bytes \\ 1) View Source
get_memory(pid(), String.t(), integer(), integer()) ::
  binary() | {:error, :no_exported_mem, String.t()}

Returns x number of bytes from a given exported memory, starting at the specified address

Usage

When within a host function body defined by defhost, if called from a WebAssembly module that has an exported memory called "memory1", and the memory is laid out as such: {0, 0, 0, 0, 0, 0, 0, 0, 243, 80, 45, 92, ...}, it can be accessed by doing:

defhost get_from_memory do
  <<243, 80, 45, 92>> = WaspVM.HostFunction.API.get_memory(ctx, "memory1", 8, 4)
end

Note that ctx here is a variable defined by the defhost macro in order to serve as a pointer to VM state.

Link to this function

update_memory(pid, mem_name, address, bytes) View Source
update_memory(pid(), String.t(), integer(), binary()) ::
  :ok | {:error, :no_exported_mem, String.t()}

Updates a given exported memory with the given bytes, at the specified address

Usage

When within a host function body defined by defhost, if called from a WebAssembly module that has an exported memory called "memory1", it can be updated by doing:

defhost update_memory do
  WaspVM.HostFunction.API.update_memory(ctx, "memory1", 0, <<"hello world">>)
end

This will set the value of "memory1" to {"h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", ...}

Note that ctx here is a variable defined by the defhost macro in order to serve as a pointer to VM state.