wasp_vm v0.8.1 WaspVM.HostFunction View Source
Exposes a DSL for defining and importing host functions
Link to this section Summary
Functions
Pass in an Elixir module or list of Elixir modules that implement defhost
calls to generate imports for WebAssembly to be passed in when loading a
WebAssembly module into the VM
Defines a host function that can be passed in to the VM using create_imports/1
Link to this section Functions
create_imports(modules) View Source
Pass in an Elixir module or list of Elixir modules that implement defhost
calls to generate imports for WebAssembly to be passed in when loading a
WebAssembly module into the VM
Usage
When using a single module to define imports:
WaspVM.HostFunction.create_imports(Module1)
Functions will be accessible in the WebAssembly module as:
(import "Module1" "function_name")
When using multiple modules to define imports:
WaspVM.HostFunction.create_imports([Module1, Module2, Module3])
Functions will be accessible in the WebAssembly module as:
(import "Module1" "function_name")
(import "Module2" "function_name")
(import "Module3" "function_name")
defhost(head, list) View Source (macro)
Defines a host function that can be passed in to the VM using create_imports/1
Will use the name of the module that it's defined in as the name of the
corresponding WebAssembly module that this host function can be imported from.
fname
can be a string or an atom. A variable called ctx
is available
within the context of the macro body as a pointer to VM state, to be used
with functions defined in WaspVM.HostFunction.API
.
Usage
Create an Elixir module that will be used to import host functions into WebAssembly:
defmodule Math do
use WaspVM.HostFunction
defhost add(a, b) do
a + b
end
end
Somewhere in your app:
defmodule MyWaspApp do
def start do
{:ok, pid} = WaspVM.start()
imports = WaspVM.HostFunction.create_imports(Math)
WaspVM.load_file(pid, "path/to/wasm/file.wasm", imports)
end
end
In the above "path/to/wasm/file.wasm", the host function can now be imported:
(import "Math" "add" (func (param i32 i32) (result i32)))
Note that the Elixir module name was used to define the WebAssembly module name that's being used for the import.