Skip to content

Instantly share code, notes, and snippets.

@bhelx
Created September 17, 2022 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhelx/9f4fa39fe04d150106b042ad870a2a9b to your computer and use it in GitHub Desktop.
Save bhelx/9f4fa39fe04d150106b042ad870a2a9b to your computer and use it in GitHub Desktop.
GenServer wrapper for Extism Plugin
defmodule ExtismElixirHost.PluginServer do
use GenServer
@impl true
def init() do
{:ok, nil}
end
@impl true
def handle_call({:new, manifest, wasi}, _from, plugin) do
{:ok, plugin} = Extism.Plugin.new(manifest, wasi)
{:reply, plugin, plugin}
end
@impl true
def handle_call(call_details, _from, plugin) do
[func_name | args] = Tuple.to_list(call_details)
response = apply(Extism.Plugin, func_name, [plugin | args])
{:reply, response, plugin}
end
end
iex(1)> {:ok, pid} = GenServer.start_link(ExtismElixirHost.PluginServer, nil)
{:ok, #PID<0.244.0>}
iex(2)> GenServer.call(pid, {:new, %{wasm: [%{path: "/Users/ben/Code/extism/wasm/code.wasm"}]}, false})
%Extism.Plugin{
resource: 0,
reference: #Reference<0.3842371659.2740977668.17272>
}
iex(3)> GenServer.call(pid, {:call, "count_vowels", "this is a test"})
{:ok, "{\"count\": 4}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment