Skip to content

Commit

Permalink
Merge pull request #26 from extism/feat/reactor-module
Browse files Browse the repository at this point in the history
feat: create separate package to make reactor modules easier
  • Loading branch information
mhmd-azeez authored Jan 18, 2024
2 parents 33b1e36 + e7340db commit d48f368
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
extism call example/tiny_http.wasm --wasi http_get --github-token="$GITHUB_TOKEN" --allow-host "jsonplaceholder.typicode.com" | grep '"userId": 1'
extism call example/tiny_reactor.wasm read_file --input "example/reactor/test.txt" --allow-path ./example/reactor --wasi --log-level info | grep 'Hello World!'
# run all the tests
make test
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
example:
tinygo build -o example/tiny_countvowels.wasm -target wasi ./example/countvowels
tinygo build -o example/tiny_http.wasm -target wasi ./example/http
tinygo build -o example/tiny_reactor.wasm -target wasi ./example/reactor

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

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

extism call example/std_countvowels.wasm _start --wasi --input "this is a test" --set-config '{"thing": "1234"}'
extism call example/std_http.wasm _start --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,44 @@ python3 app.py
# => An argument to send to Python!
```

## Reactor modules

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:

```go
package main

import (
"os"

"github.com/extism/go-pdk"
_ "github.com/extism/go-pdk/wasi-reactor"
)

//export read_file
func read_file() {
name := pdk.InputString()

content, err := os.ReadFile(name)
if err != nil {
pdk.Log(pdk.LogError, err.Error())
return
}

pdk.Output(content)
}

func main() {}
```

```bash
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
extism call ./reactor.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
# => Hello World!
```

Note: this is not required if you only have the `main` function.

### Reach Out!

Have a question or just want to drop in and say hi? [Hop on the Discord](https://extism.org/discord)!
16 changes: 16 additions & 0 deletions example/reactor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Reactor module example
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.

To test this example, run:

```bash
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
extism call ./reactor.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
# => Hello World!
```

If you don't include the pacakge, you'll see this output:
```bash
extism call .\c.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
# => 2024/01/18 20:48:48 open ./test.txt: errno 76
```
1 change: 1 addition & 0 deletions example/reactor/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
26 changes: 26 additions & 0 deletions example/reactor/tiny_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !std
// +build !std

package main

import (
"os"

"github.com/extism/go-pdk"
_ "github.com/extism/go-pdk/wasi-reactor"
)

//export read_file
func read_file() {
name := pdk.InputString()

content, err := os.ReadFile(name)
if err != nil {
pdk.Log(pdk.LogError, err.Error())
return
}

pdk.Output(content)
}

func main() {}
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.21.1

use (
.
./wasi-reactor
)
9 changes: 9 additions & 0 deletions wasi-reactor/extism_pdk_reactor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package reactor

//export __wasm_call_ctors
func __wasm_call_ctors()

//export _initialize
func _initialize() {
__wasm_call_ctors()
}
3 changes: 3 additions & 0 deletions wasi-reactor/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/extism/go-pdk/wasi-reactor

go 1.21.1

0 comments on commit d48f368

Please sign in to comment.