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
child_spec(arg) View Source
Returns a specification to start this module under a supervisor.
See Supervisor
.
execute(ref, func, args \\ [], opts \\ []) View Source
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
get_memory(ref, mem_name) View Source
Retrieve a Virtual Memory set from the VM. Memory must have been exported from the WebAssembly module in order to be accessible here.
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
.
load(ref, binary, imports \\ %{}) View Source
Load a WebAssembly module directly from a binary into the VM
load_file(ref, filename, imports \\ %{}) View Source
Load a binary WebAssembly file (.wasm) as a module into the VM
load_module(ref, module, imports \\ %{}) View Source
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.
start()
View Source
start() :: {:ok, pid()}
start() :: {:ok, pid()}
Starts the Virtual Machine and returns the PID which is used to interface with the VM.
update_memory(ref, mem_name, mem) View Source
Write to a module's exported memory directly. Memory must have been exported from the WebAssembly module in order to be accessible here.
vm_state(ref)
View Source
vm_state(pid()) :: WaspVM
vm_state(pid()) :: WaspVM
Returns the state for a given VM instance