wasp_vm v0.8.1 WaspVM View Source

Execute WebAssembly code

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Call an exported function by name from the VM. The function must have been loaded in through a module using load_file/2 or load/2 previously

Retrieve a Virtual Memory set from the VM. Memory must have been exported from the WebAssembly module in order to be accessible here

Invoked to handle continue instructions

Load a WebAssembly module directly from a binary into the VM

Load a binary WebAssembly file (.wasm) as a module into the VM

Load a module that was already decoded by load/3 or load_file/3. This is useful for caching modules, as it skips the entire decoding step

Starts the Virtual Machine and returns the PID which is used to interface with the VM

Write to a module's exported memory directly. Memory must have been exported from the WebAssembly module in order to be accessible here

Returns the state for a given VM instance

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

execute(ref, func, args \\ [], opts \\ []) View Source
execute(pid(), String.t(), list(), list()) ::
  :ok | {:ok, any()} | {:error, any()}

Call an exported function by name from the VM. The function must have been loaded in through a module using load_file/2 or load/2 previously

Usage

Most basic usage for a simple module (no imports or host functions):

Wasm File (add.wat)

(module
 (func (export "basic_add") (param i32 i32) (result i32)
  get_local 0
  get_local 1
  i32.add
 )
)

Use an external tool to compile add.wat to add.wasm (compile from text representation to binary representation)

{:ok, pid} = WaspVM.start() # Start the VM
WaspVM.load_file(pid, "path/to/add.wasm") # Load the module that contains our add function

# Call the add function, passing in 3 and 10 as args
{:ok, gas, result} = WaspVM.execute(pid, "basic_add", [3, 10])

Executing modules with host functions:

Wasm file (log.wat)

(module
  (import "env" "consoleLog" (func $consoleLog (param f32)))
  (export "getSqrt" (func $getSqrt))
  (func $getSqrt (param f32) (result f32)
    get_local 0
    f32.sqrt
    tee_local 0
    call $consoleLog

    get_local 0
  )
)

Use an external tool to compile log.wat to log.wasm (compile from text representation to binary representation)

{:ok, pid} = WaspVM.start() # Start the VM

# Define the imports used in this module. Keys in the import map
# must be strings
imports = %{
  "env" => %{
    "consoleLog" => fn x -> IO.puts "its #{x}" end
  }
}

# Load the file, passing in the imports
WaspVM.load_file(pid, "path/to/log.wasm", imports)

# Call getSqrt with an argument of 25
WaspVM.execute(pid, "getSqrt", [25])

Program execution can also be limited by specifying a :gas_limit option:

WaspVM.execute(pid, "some_func", [], gas_limit: 100)

This will stop execution of the program if the accumulated gas exceeds 100

Program execution can also output to a log file by specifying a :trace option:

WaspVM.execute(pid, "some_func", [], trace: true)

This will trace all instructions passed, as well as the gas cost accumulated to a log file
Link to this function

get_memory(ref, mem_name) View Source
get_memory(pid(), String.t()) :: WaspVM.Memory

Retrieve a Virtual Memory set from the VM. Memory must have been exported from the WebAssembly module in order to be accessible here.

Link to this function

handle_continue(arg, vm) View Source

Invoked to handle continue instructions.

It is useful for performing work after initialization or for splitting the work in a callback in multiple steps, updating the process state along the way.

Return values are the same as c:handle_cast/2.

This callback is optional. If one is not implemented, the server will fail if a continue instruction is used.

This callback is only supported on Erlang/OTP 21+.

Callback implementation for GenServer.handle_continue/2.

Link to this function

load(ref, binary, imports \\ %{}) View Source
load(pid(), binary(), map()) :: {:ok, WaspVM.Module}

Load a WebAssembly module directly from a binary into the VM

Link to this function

load_file(ref, filename, imports \\ %{}) View Source
load_file(pid(), String.t(), map()) :: {:ok, WaspVM.Module}

Load a binary WebAssembly file (.wasm) as a module into the VM

Link to this function

load_module(ref, module, imports \\ %{}) View Source
load_module(pid(), WaspVM.Module, map()) :: {:ok, WaspVM.Module}

Load a module that was already decoded by load/3 or load_file/3. This is useful for caching modules, as it skips the entire decoding step.

Link to this function

start() View Source
start() :: {:ok, pid()}

Starts the Virtual Machine and returns the PID which is used to interface with the VM.

Link to this function

update_memory(ref, mem_name, mem) View Source
update_memory(pid(), String.t(), WaspVM.Memory) :: WaspVM

Write to a module's exported memory directly. Memory must have been exported from the WebAssembly module in order to be accessible here.

Link to this function

vm_state(ref) View Source
vm_state(pid()) :: WaspVM

Returns the state for a given VM instance