Files
extism/elixir
Steve Manuel 581e9cea99 chore: update wasmtime to 6.0, bump extism versions (#247)
Unsure if now is the best time to do the `extism` crate version bumps,
but I figured they'd need to happen at some point in the near future
anyways. Happy to revert if there is any opposition. Also, I added back
the local `path` property to the Elixir NIF crate manifest. I want to
see if this breaks again in CI, or if we can leave it.

---------

Co-authored-by: zach <zach@dylib.so>
2023-03-01 15:24:37 -08:00
..
2022-10-19 14:12:56 -05:00

Extism

Extism Host SDK for Elixir and Erlang

Docs

Read the docs on hexdocs.pm.

Installation

You can find this package on hex.pm.

def deps do
  [
    {:extism, "~> 0.1.0"}
  ]
end

Getting Started

Example

# Create a context for which plugins can be allocated and cleaned
ctx = Extism.Context.new()

# point to some wasm code, this is the count_vowels example that ships with extism
manifest = %{ wasm: [ %{ path: "/Users/ben/code/extism/wasm/code.wasm" } ]}
{:ok, plugin} = Extism.Context.new_plugin(ctx, manifest, false)
# {:ok,
# %Extism.Plugin{
#   resource: 0,
#   reference: #Reference<0.520418104.1263009793.80956>
# }}
{:ok, output} = Extism.Plugin.call(plugin, "count_vowels", "this is a test")
# {:ok, "{\"count\": 4}"}
{:ok, result} = JSON.decode(output)
# {:ok, %{"count" => 4}}

# free up the context and any plugins we allocated
Extism.Context.free(ctx)

Modules

The two primary modules you should learn are:

Context

The Context can be thought of as a session. You need a context to interact with the Extism runtime. The context holds your plugins and when you free the context, it frees your plugins. It's important to free up your context and plugins when you are done with them.

ctx = Extism.Context.new()
# frees all the plugins 
Extism.Context.reset(ctx)
# frees the context and all its plugins
Extism.Context.free(ctx)

Plugin

The Plugin represents an instance of your WASM program from the given manifest. The key method to know here is Extism.Plugin#call which takes a function name to invoke and some input data, and returns the results from the plugin.

{:ok, plugin} = Extism.Context.new_plugin(ctx, manifest, false)
{:ok, output} = Extism.Plugin.call(plugin, "count_vowels", "this is a test")