You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
api: hide Chicory low-level primitives, expose Extism function type
- Loosely follow the java-sdk, expose ExtismHostFunction and use an internal
ExtismValTypeList that avoids wrapping as much as possible.
- Hide the conversion between longs and high-level, types
- Add test case following the java-sdk
- Update the README
Signed-off-by: Edoardo Vacchi <[email protected]>
The primary concept in Extism is the [plug-in](https://extism.org/docs/concepts/plug-in). You can think of a plug-in as a code module stored in a `.wasm` file.
36
+
Since you may not have a Extism plug-in on hand to test, let's load a demo plug-in from the web:
37
+
38
+
```java
39
+
var url ="https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm";
40
+
var wasm =ManifestWasm.fromUrl(url).build();
41
+
var manifest =Manifest.ofWasms(wasm).build();
42
+
var plugin =Plugin.ofManifest(manifest).build();
43
+
```
44
+
45
+
> **Note**: See [the Manifest docs](https://www.javadoc.io/doc/org.extism.sdk/extism/latest/org/extism/sdk/manifest/Manifest.html) as it has a rich schema and a lot of options.
46
+
47
+
### Calling A Plug-in's Exports
48
+
49
+
This plug-in was written in Rust and it does one thing, it counts vowels in a string. As such, it exposes one "export" function: `count_vowels`.
50
+
We can call exports using [Plugin#call](https://www.javadoc.io/doc/org.extism.sdk/extism/latest/org/extism/sdk/Plugin.html#call(java.lang.String,byte[]))
51
+
52
+
```java
53
+
var output = plugin.call("count_vowels", "Hello, World!".getBytes(StandardCharsets.UTF_8));
// ^ note count changed to 4 as we configured Y as a vowel this time
100
+
```
101
+
102
+
### Host Functions
103
+
104
+
Let's extend our count-vowels example a little bit: Instead of storing the `total` in an ephemeral plug-in var,
105
+
let's store it in a persistent key-value store!
106
+
107
+
Wasm can't use our app's KV store on its own. This is where [Host Functions](https://extism.org/docs/concepts/host-functions) come in.
108
+
109
+
[Host functions](https://extism.org/docs/concepts/host-functions) allow us to grant new capabilities to our plug-ins from our application.
110
+
They are simply some java methods you write which can be passed down and invoked from any language inside the plug-in.
111
+
112
+
Let's load the manifest like usual but load up this `count_vowels_kvstore` plug-in:
113
+
114
+
```java
115
+
var url ="https://github.com/extism/plugins/releases/latest/download/count_vowels_kvstore.wasm";
116
+
var manifest =newManifest(List.of(UrlWasmSource.fromUrl(url)));
117
+
var plugin =newPlugin(manifest, false, null);
118
+
```
119
+
120
+
> *Note*: The source code for this plug-in is [here](https://github.com/extism/plugins/blob/main/count_vowels_kvstore/src/lib.rs)
121
+
> and is written in rust, but it could be written in any of our PDK languages.
122
+
123
+
Unlike our previous plug-in, this plug-in expects you to provide host functions that satisfy its import interface for a KV store.
124
+
We want to expose two functions to our plugin, `kv_write(String key, Bytes value)` which writes a bytes value to a key and `Bytes kv_read(String key)` which reads the bytes at the given `key`.
> *Note*: In order to write host functions you should get familiar with the methods on the [ExtismCurrentPlugin](https://www.javadoc.io/doc/org.extism.sdk/extism/latest/org/extism/sdk/ExtismCurrentPlugin.html) class.
169
+
> The `plugin` parameter is an instance of this class.
170
+
171
+
Now we just need to pass in these function references when creating the plugin:.
172
+
173
+
```java
174
+
var plugin =Plugin.ofManifest(manifest).withHostFunctions(kvReadHostFn, kvWriteHostFn).build();
175
+
var output = plugin.call("count_vowels", "Yellow, World!".getBytes(StandardCharsets.UTF_8));
176
+
var result =newString(output, StandardCharsets.UTF_8);
0 commit comments