Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create separate package to make reactor modules easier #26

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading