Created
September 17, 2022 15:21
-
-
Save bhelx/9f4fa39fe04d150106b042ad870a2a9b to your computer and use it in GitHub Desktop.
GenServer wrapper for Extism Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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