Skip to content

Commit d48f368

Browse files
authored
Merge pull request #26 from extism/feat/reactor-module
feat: create separate package to make reactor modules easier
2 parents 33b1e36 + e7340db commit d48f368

File tree

9 files changed

+102
-0
lines changed

9 files changed

+102
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ jobs:
4848
4949
extism call example/tiny_http.wasm --wasi http_get --github-token="$GITHUB_TOKEN" --allow-host "jsonplaceholder.typicode.com" | grep '"userId": 1'
5050
51+
extism call example/tiny_reactor.wasm read_file --input "example/reactor/test.txt" --allow-path ./example/reactor --wasi --log-level info | grep 'Hello World!'
5152
5253
# run all the tests
5354
make test

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
example:
33
tinygo build -o example/tiny_countvowels.wasm -target wasi ./example/countvowels
44
tinygo build -o example/tiny_http.wasm -target wasi ./example/http
5+
tinygo build -o example/tiny_reactor.wasm -target wasi ./example/reactor
56

67
GOOS=wasip1 GOARCH=wasm go build -tags std -o example/std_countvowels.wasm ./example/countvowels
78
GOOS=wasip1 GOARCH=wasm go build -tags std -o example/std_http.wasm ./example/http
89

910
test:
1011
extism call example/tiny_countvowels.wasm count_vowels --wasi --input "this is a test" --set-config '{"thing": "1234"}'
1112
extism call example/tiny_http.wasm http_get --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"
13+
extism call example/tiny_reactor.wasm read_file --input "example/reactor/test.txt" --allow-path ./example/reactor --wasi --log-level info
1214

1315
extism call example/std_countvowels.wasm _start --wasi --input "this is a test" --set-config '{"thing": "1234"}'
1416
extism call example/std_http.wasm _start --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,44 @@ python3 app.py
326326
# => An argument to send to Python!
327327
```
328328

329+
## Reactor modules
330+
331+
Since TinyGo doesn't support [Reactor modules](https://dylibso.com/blog/wasi-command-reactor/) yet, If you want to use WASI inside your Reactor module functions (exported functions other than `main`), you'll need to import `wasi-reactor` module which makes sure libc and go runtime are properly initialized:
332+
333+
```go
334+
package main
335+
336+
import (
337+
"os"
338+
339+
"github.com/extism/go-pdk"
340+
_ "github.com/extism/go-pdk/wasi-reactor"
341+
)
342+
343+
//export read_file
344+
func read_file() {
345+
name := pdk.InputString()
346+
347+
content, err := os.ReadFile(name)
348+
if err != nil {
349+
pdk.Log(pdk.LogError, err.Error())
350+
return
351+
}
352+
353+
pdk.Output(content)
354+
}
355+
356+
func main() {}
357+
```
358+
359+
```bash
360+
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
361+
extism call ./reactor.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
362+
# => Hello World!
363+
```
364+
365+
Note: this is not required if you only have the `main` function.
366+
329367
### Reach Out!
330368

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

example/reactor/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Reactor module example
2+
By including this package, you'll turn your plugin into a [Reactor](https://dylibso.com/blog/wasi-command-reactor/) module. This makes sure that you can use WASI (e.g. File Access) in your exported functions.
3+
4+
To test this example, run:
5+
6+
```bash
7+
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
8+
extism call ./reactor.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
9+
# => Hello World!
10+
```
11+
12+
If you don't include the pacakge, you'll see this output:
13+
```bash
14+
extism call .\c.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
15+
# => 2024/01/18 20:48:48 open ./test.txt: errno 76
16+
```

example/reactor/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!

example/reactor/tiny_main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !std
2+
// +build !std
3+
4+
package main
5+
6+
import (
7+
"os"
8+
9+
"github.com/extism/go-pdk"
10+
_ "github.com/extism/go-pdk/wasi-reactor"
11+
)
12+
13+
//export read_file
14+
func read_file() {
15+
name := pdk.InputString()
16+
17+
content, err := os.ReadFile(name)
18+
if err != nil {
19+
pdk.Log(pdk.LogError, err.Error())
20+
return
21+
}
22+
23+
pdk.Output(content)
24+
}
25+
26+
func main() {}

go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.21.1
2+
3+
use (
4+
.
5+
./wasi-reactor
6+
)

wasi-reactor/extism_pdk_reactor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package reactor
2+
3+
//export __wasm_call_ctors
4+
func __wasm_call_ctors()
5+
6+
//export _initialize
7+
func _initialize() {
8+
__wasm_call_ctors()
9+
}

wasi-reactor/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/extism/go-pdk/wasi-reactor
2+
3+
go 1.21.1

0 commit comments

Comments
 (0)