mirror of
https://github.com/extism/extism.git
synced 2026-04-23 03:00:11 -04:00
5c9aa4c90a
This should work but it doesn't. Error message when including extism
0.2.0 locally:
```
Compiling 4 files (.ex)
error: failed to get `extism` as a dependency of package `extism_nif v0.1.0 (/private/tmp/extism_test/deps/extism/native/extism_nif)`
Caused by:
failed to load source for dependency `extism`
Caused by:
Unable to update /private/tmp/extism_test/deps/rust
Caused by:
failed to read `/private/tmp/extism_test/deps/rust/Cargo.toml`
Caused by:
No such file or directory (os error 2)
== Compilation error in file lib/extism/native.ex ==
** (RuntimeError) calling `cargo metadata` failed.
(rustler 0.26.0) lib/rustler/compiler/config.ex:87: Rustler.Compiler.Config.metadata!/1
(rustler 0.26.0) lib/rustler/compiler/config.ex:69: Rustler.Compiler.Config.build/1
(rustler 0.26.0) lib/rustler/compiler.ex:9: Rustler.Compiler.compile_crate/2
lib/extism/native.ex:6: (module)
could not compile dependency :extism, "mix compile" failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile extism", update it with "mix deps.update extism" or clean it with "mix deps.clean extism"
```
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")