ExtismSourceExtism bindings for OCaml
Extism is a framework for executing WebAssembly plugins. The OCaml bindings require libextism, installation information is available on the Extism website
Plugin and Manifest modules are the main modules to look at.Type provides different types that can be encoded as input/output to Plugin.callFunction is used to define host functions and Host_function is used from inside host functions to access the plugin memoryThe 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 resUsing 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 resThe 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 ->
boolSet 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_logsSet 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