package extism
Install
Dune Dependency
Authors
Maintainers
Sources
md5=f5dc2392fda67085bbba8af63796edf3
sha512=0ff1d70c2979ebf51c965ba0fbc3dd10ee3b20fc0e58abdab7ff2175a40f33bebe5542ca6fd4d5f81a941bac80c29c3873c2ac95d3f6b540e229a54d1e395e26
doc/extism/Extism/index.html
Module Extism
Source
Extism bindings for OCaml
Introduction
Extism is a framework for executing WebAssembly plugins. The OCaml bindings require libextism, installation information is available on the Extism website
- The
Plugin
andManifest
modules are the main modules to look at. Type
provides different types that can be encoded as input/output toPlugin.call
Function
is used to define host functions andHost_function
is used from inside host functions to access the plugin memory
Basic example
The following loads a Plugin
from a file on disk using Manifest
then calls a function with a string and prints the string output:
open Extism
let () =
let plugin =
Plugin.of_manifest_exn
@@ Manifest.create [ Manifest.Wasm.file "test/code.wasm" ]
in
let res =
Plugin.call_string_exn plugin ~name:"count_vowels" "input data"
in
print_endline res
Using the typed plugin interface you can pre-define plug-in functions:
open Extism
module Example = struct
include Plugin.Typed.Init ()
let count_vowels = exn @@ fn "count_vowels" Type.string Type.string
end
let () =
let plugin =
Example.of_plugin_exn
@@ Plugin.of_manifest_exn
@@ Manifest.create [ Manifest.Wasm.file "test/code.wasm" ]
in
let res =
Example.count_vowels plugin "input data"
in
print_endline res
API
The Manifest
module is a reference to the Extism_manifest
package, it allows you to programatically construct Extism manifests.
Val_type
enumerates the available Wasm types, this should only be used when implementing host functions
Type
defines conversions from OCaml values in and out of Extism memory
Host_function
represents the plugin that is currently running from inside a host function definition
Function
is used to create new a new function, which can be called from a WebAssembly plugin
val set_log_file :
?level:[ `Error | `Warn | `Info | `Debug | `Trace | `Filter of string ] ->
string ->
bool
Set the log file and level for all Extism plugins, the names stdout
or stderr
can be used to write to the terminal
val set_log_custom :
?level:[ `Error | `Warn | `Info | `Debug | `Trace | `Filter of string ] ->
(string -> unit) ->
drain_logs
Set the log level and enable buffered logging. Returns a function that can be used to drain the logs
with_plugin f plugin
uses Fun.protect
to ensure that a plugin is freed when f
finished executing
Returns the libextism version, not the version of the OCaml library