Skip to content

Commit 67465e4

Browse files
authored
chore: prep for release (#31)
* chore: prep for release * chore: use correct extism call command in docs
1 parent 42b0b85 commit 67465e4

File tree

2 files changed

+73
-37
lines changed

2 files changed

+73
-37
lines changed

README.md

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Extism Zig PDK
22

3-
4-
This library can be used to write [Extism Plug-ins](https://extism.org/docs/concepts/plug-in) in Zig.
3+
This library can be used to write
4+
[Extism Plug-ins](https://extism.org/docs/concepts/plug-in) in Zig.
55

66
## Install
77

@@ -13,10 +13,11 @@ cd my-plugin
1313
zig init
1414
```
1515

16-
Add the library as a dependency. Git ref should be the hash of the latest commit:
16+
Add the library as a dependency. Git ref should be the hash of the latest
17+
commit:
1718

1819
```sh
19-
zig fetch --save https://github.com/extism/zig-pdk/archive/<git-ref-here>.tar.gz
20+
zig fetch --save https://github.com/extism/zig-pdk/archive/refs/tags/v1.1.0.tar.gz
2021
```
2122

2223
Change your `build.zig` so that it references `extism-pdk`:
@@ -49,7 +50,13 @@ pub fn build(b: *std.Build) void {
4950

5051
## Getting Started
5152

52-
The goal of writing an [Extism plug-in](https://extism.org/docs/concepts/plug-in) is to compile your Zig code to a Wasm module with exported functions that the host application can invoke. The first thing you should understand is creating an export. Let's write a simple program that exports a `greet` function which will take a name as a string and return a greeting string. Zig has excellent support for this through the `export` keyword:
53+
The goal of writing an
54+
[Extism plug-in](https://extism.org/docs/concepts/plug-in) is to compile your
55+
Zig code to a Wasm module with exported functions that the host application can
56+
invoke. The first thing you should understand is creating an export. Let's write
57+
a simple program that exports a `greet` function which will take a name as a
58+
string and return a greeting string. Zig has excellent support for this through
59+
the `export` keyword:
5360

5461
```zig
5562
const std = @import("std");
@@ -69,27 +76,31 @@ export fn greet() i32 {
6976
}
7077
```
7178

72-
> Note: if you started with the generated project files from `zig init`, you should delete `src/root.zig`
73-
and any references to it if they are in your `build.zig` file.
79+
> Note: if you started with the generated project files from `zig init`, you
80+
> should delete `src/root.zig` and any references to it if they are in your
81+
> `build.zig` file.
7482
7583
Then run:
84+
7685
```sh
7786
zig build
7887
```
7988

80-
This will put your compiled wasm in `zig-out/bin`.
81-
We can now test it using the [Extism CLI](https://github.com/extism/cli)'s `run`
82-
command:
89+
This will put your compiled wasm in `zig-out/bin`. We can now test it using the
90+
[Extism CLI](https://github.com/extism/cli)'s `call` command:
8391

8492
```bash
8593
extism call ./zig-out/bin/my-plugin.wasm greet --input "Benjamin"
8694
# => Hello, Benjamin!
8795
```
8896

89-
> **Note**: We also have a web-based, plug-in tester called the [Extism Playground](https://playground.extism.org/)
97+
> **Note**: We also have a web-based, plug-in tester called the
98+
> [Extism Playground](https://playground.extism.org/)
9099
91100
### More Exports: Error Handling
92-
Suppose want to re-write our greeting module to never greet Benjamins. We can use `Plugin.setError`:
101+
102+
Suppose want to re-write our greeting module to never greet Benjamins. We can
103+
use `Plugin.setError`:
93104

94105
```zig
95106
export fn greet() i32 {
@@ -123,7 +134,9 @@ echo $?
123134

124135
### Json
125136

126-
Extism export functions simply take bytes in and bytes out. Those can be whatever you want them to be. A common and simple way to get more complex types to and from the host is with json:
137+
Extism export functions simply take bytes in and bytes out. Those can be
138+
whatever you want them to be. A common and simple way to get more complex types
139+
to and from the host is with json:
127140

128141
```zig
129142
export fn add() i32 {
@@ -149,7 +162,7 @@ export fn add() i32 {
149162
}
150163
```
151164

152-
To use a json helper, you can accomplish the same as the above with:
165+
To use a json helper, you can accomplish the same as the above with:
153166

154167
```zig
155168
export fn add() i32 {
@@ -179,7 +192,9 @@ extism call ./zig-out/bin/my-plugin.wasm add --input='{"a": 20, "b": 21}'
179192
## Configs
180193

181194
Configs are key-value pairs that can be passed in by the host when creating a
182-
plug-in. These can be useful to statically configure the plug-in with some data that exists across every function call. Here is a trivial example using `Plugin.getConfig`:
195+
plug-in. These can be useful to statically configure the plug-in with some data
196+
that exists across every function call. Here is a trivial example using
197+
`Plugin.getConfig`:
183198

184199
```zig
185200
export fn greet() i32 {
@@ -195,7 +210,8 @@ export fn greet() i32 {
195210
}
196211
```
197212

198-
To test it, the [Extism CLI](https://github.com/extism/cli) has a `--config` option that lets you pass in `key=value` pairs:
213+
To test it, the [Extism CLI](https://github.com/extism/cli) has a `--config`
214+
option that lets you pass in `key=value` pairs:
199215

200216
```bash
201217
extism call ./zig-out/bin/my-plugin.wasm greet --config user=Benjamin
@@ -206,7 +222,7 @@ extism call ./zig-out/bin/my-plugin.wasm greet --config user=Benjamin
206222

207223
Variables are another key-value mechanism but it's a mutable data store that
208224
will persist across function calls. These variables will persist as long as the
209-
host has loaded and not freed the plug-in.
225+
host has loaded and not freed the plug-in.
210226

211227
```zig
212228
export fn count() i32 {
@@ -226,7 +242,8 @@ export fn count() i32 {
226242
}
227243
```
228244

229-
To test it, the [Extism CLI](https://github.com/extism/cli) has a `--loop` option that lets you pass call the same function multiple times:
245+
To test it, the [Extism CLI](https://github.com/extism/cli) has a `--loop`
246+
option that lets you pass call the same function multiple times:
230247

231248
```sh
232249
extism call ./zig-out/bin/my-plugin.wasm count --loop 3
@@ -235,12 +252,17 @@ extism call ./zig-out/bin/my-plugin.wasm count --loop 3
235252
3
236253
```
237254

238-
> **Note**: Use the untyped variants `Plugin.setVar(self: Plugin, key: []const u8, value: []const u8)` and `Plugin.getVar(self: Plugin, key: []const u8) !?[]u8` to handle your own types.
255+
> **Note**: Use the untyped variants
256+
> `Plugin.setVar(self: Plugin, key: []const u8, value: []const u8)` and
257+
> `Plugin.getVar(self: Plugin, key: []const u8) !?[]u8` to handle your own
258+
> types.
239259
240260
## Logging
241261

242-
Because Wasm modules by default do not have access to the system, printing to stdout won't work (unless you use WASI).
243-
Extism provides a simple logging function that allows you to use the host application to log without having to give the plug-in permission to make syscalls.
262+
Because Wasm modules by default do not have access to the system, printing to
263+
stdout won't work (unless you use WASI). Extism provides a simple logging
264+
function that allows you to use the host application to log without having to
265+
give the plug-in permission to make syscalls.
244266

245267
```zig
246268
export fn log_stuff() i32 {
@@ -265,11 +287,14 @@ extism call ./zig-out/bin/my-plugin.wasm log_stuff --log-level=debug
265287
2023/11/22 14:00:26 An error log!
266288
```
267289

268-
> *Note*: From the CLI you need to pass a level with `--log-level`. If you are running the plug-in in your own host using one of our SDKs, you need to make sure that you call `set_log_file` to `"stdout"` or some file location.
290+
> _Note_: From the CLI you need to pass a level with `--log-level`. If you are
291+
> running the plug-in in your own host using one of our SDKs, you need to make
292+
> sure that you call `set_log_file` to `"stdout"` or some file location.
269293
270294
## HTTP
271295

272-
Sometimes it is useful to let a plug-in [make HTTP calls]. [see: Extism HTTP library](src/http.zig)
296+
Sometimes it is useful to let a plug-in [make HTTP calls].
297+
[see: Extism HTTP library](src/http.zig)
273298

274299
```zig
275300
const http = extism_pdk.http;
@@ -324,7 +349,9 @@ export fn http_get() i32 {
324349
}
325350
```
326351

327-
By default, Extism modules cannot make HTTP requests unless you specify which hosts it can connect to. You can use `--allow-host` in the Extism CLI to set this:
352+
By default, Extism modules cannot make HTTP requests unless you specify which
353+
hosts it can connect to. You can use `--allow-host` in the Extism CLI to set
354+
this:
328355

329356
```
330357
extism call ./zig-out/bin/my-plugin.wasm http_get --allow-host='*.typicode.com'
@@ -333,23 +360,29 @@ extism call ./zig-out/bin/my-plugin.wasm http_get --allow-host='*.typicode.com'
333360

334361
## Imports (Host Functions)
335362

336-
Like any other code module, Wasm not only let's you export functions to the outside world, you can
337-
import them too. Host Functions allow a plug-in to import functions defined in the host. For example,
338-
if you host application is written in Python, it can pass a Python function down to your Zig plug-in
339-
where you can invoke it.
363+
Like any other code module, Wasm not only let's you export functions to the
364+
outside world, you can import them too. Host Functions allow a plug-in to import
365+
functions defined in the host. For example, if you host application is written
366+
in Python, it can pass a Python function down to your Zig plug-in where you can
367+
invoke it.
340368

341-
This topic can get fairly complicated and we have not yet fully abstracted the Wasm knowledge you need
342-
to do this correctly. So we recommend reading our [concept doc on Host Functions](https://extism.org/docs/concepts/host-functions) before you get started.
369+
This topic can get fairly complicated and we have not yet fully abstracted the
370+
Wasm knowledge you need to do this correctly. So we recommend reading our
371+
[concept doc on Host Functions](https://extism.org/docs/concepts/host-functions)
372+
before you get started.
343373

344374
### A Simple Example
345375

346-
Host functions have a similar interface as exports. You just need to declare them as extern. You only declare the interface as it is the host's responsibility to provide the implementation:
376+
Host functions have a similar interface as exports. You just need to declare
377+
them as extern. You only declare the interface as it is the host's
378+
responsibility to provide the implementation:
347379

348380
```zig
349381
pub extern "extism:host/user" fn a_python_func(u64) u64;
350382
```
351383

352-
We should be able to call this function as a normal Zig function. Note that we need to manually handle the pointer casting:
384+
We should be able to call this function as a normal Zig function. Note that we
385+
need to manually handle the pointer casting:
353386

354387
```zig
355388
export fn hello_from_python() i32 {
@@ -375,8 +408,10 @@ export fn hello_from_python() i32 {
375408

376409
### Testing it out
377410

378-
We can't really test this from the Extism CLI as something must provide the implementation. So let's
379-
write out the Python side here. Check out the [docs for Host SDKs](https://extism.org/docs/concepts/host-sdk) to implement a host function in a language of your choice.
411+
We can't really test this from the Extism CLI as something must provide the
412+
implementation. So let's write out the Python side here. Check out the
413+
[docs for Host SDKs](https://extism.org/docs/concepts/host-sdk) to implement a
414+
host function in a language of your choice.
380415

381416
```python
382417
from extism import host_fn, Plugin
@@ -394,7 +429,7 @@ def a_python_func(input: str) -> str:
394429
```
395430

396431
Now when we load the plug-in we pass the host function:
397-
432+
398433
```python
399434
manifest = {"wasm": [{"path": "/path/to/plugin.wasm"}]}
400435
plugin = Plugin(manifest, functions=[a_python_func], wasi=True)
@@ -410,4 +445,5 @@ python3 app.py
410445

411446
### Reach Out!
412447

413-
Have a question or just want to drop in and say hi? [Hop on the Discord](https://extism.org/discord)!
448+
Have a question or just want to drop in and say hi?
449+
[Hop on the Discord](https://extism.org/discord)!

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.{
22
.name = "extism-pdk",
3-
.version = "0.1.0",
3+
.version = "1.1.0",
44
.paths = .{""},
55
}

0 commit comments

Comments
 (0)