From f82506a1d0b5e4d5d4495a649480e8f2cf1c9296 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Thu, 18 Jan 2024 20:50:07 +0300 Subject: [PATCH 1/4] feat: create separate package to make reactor modules easier --- example/reactor/README.md | 16 ++++++++++++++++ example/reactor/test.txt | 1 + example/reactor/tiny_main.go | 26 ++++++++++++++++++++++++++ go.work | 6 ++++++ pdk_reactor/extism_pdk_reactor.go | 9 +++++++++ pdk_reactor/go.mod | 3 +++ 6 files changed, 61 insertions(+) create mode 100644 example/reactor/README.md create mode 100644 example/reactor/test.txt create mode 100644 example/reactor/tiny_main.go create mode 100644 go.work create mode 100644 pdk_reactor/extism_pdk_reactor.go create mode 100644 pdk_reactor/go.mod diff --git a/example/reactor/README.md b/example/reactor/README.md new file mode 100644 index 0000000..bfa528c --- /dev/null +++ b/example/reactor/README.md @@ -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: + +``` +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: +``` +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 +``` \ No newline at end of file diff --git a/example/reactor/test.txt b/example/reactor/test.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/example/reactor/test.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/example/reactor/tiny_main.go b/example/reactor/tiny_main.go new file mode 100644 index 0000000..5636643 --- /dev/null +++ b/example/reactor/tiny_main.go @@ -0,0 +1,26 @@ +//go:build !std +// +build !std + +package main + +import ( + "os" + + "github.com/extism/go-pdk" + _ "github.com/extism/go-pdk/pdk_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() {} diff --git a/go.work b/go.work new file mode 100644 index 0000000..3a75647 --- /dev/null +++ b/go.work @@ -0,0 +1,6 @@ +go 1.21.1 + +use ( + . + ./pdk_reactor +) diff --git a/pdk_reactor/extism_pdk_reactor.go b/pdk_reactor/extism_pdk_reactor.go new file mode 100644 index 0000000..9765100 --- /dev/null +++ b/pdk_reactor/extism_pdk_reactor.go @@ -0,0 +1,9 @@ +package pdk_reactor + +//export __wasm_call_ctors +func __wasm_call_ctors() + +//export _initialize +func _initialize() { + __wasm_call_ctors() +} diff --git a/pdk_reactor/go.mod b/pdk_reactor/go.mod new file mode 100644 index 0000000..3b9ebb6 --- /dev/null +++ b/pdk_reactor/go.mod @@ -0,0 +1,3 @@ +module github.com/extism/go-pdk/pdk_reactor + +go 1.21.1 From 54d6521fe98c9ce17369c52d2c697f2da38ae06b Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Thu, 18 Jan 2024 21:03:48 +0300 Subject: [PATCH 2/4] rename package --- example/reactor/tiny_main.go | 2 +- go.work | 2 +- pdk_reactor/go.mod | 3 --- {pdk_reactor => wasi-reactor}/extism_pdk_reactor.go | 2 +- wasi-reactor/go.mod | 3 +++ 5 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 pdk_reactor/go.mod rename {pdk_reactor => wasi-reactor}/extism_pdk_reactor.go (85%) create mode 100644 wasi-reactor/go.mod diff --git a/example/reactor/tiny_main.go b/example/reactor/tiny_main.go index 5636643..b4d5371 100644 --- a/example/reactor/tiny_main.go +++ b/example/reactor/tiny_main.go @@ -7,7 +7,7 @@ import ( "os" "github.com/extism/go-pdk" - _ "github.com/extism/go-pdk/pdk_reactor" + _ "github.com/extism/go-pdk/wasi-reactor" ) //export read_file diff --git a/go.work b/go.work index 3a75647..99b21af 100644 --- a/go.work +++ b/go.work @@ -2,5 +2,5 @@ go 1.21.1 use ( . - ./pdk_reactor + ./wasi-reactor ) diff --git a/pdk_reactor/go.mod b/pdk_reactor/go.mod deleted file mode 100644 index 3b9ebb6..0000000 --- a/pdk_reactor/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/extism/go-pdk/pdk_reactor - -go 1.21.1 diff --git a/pdk_reactor/extism_pdk_reactor.go b/wasi-reactor/extism_pdk_reactor.go similarity index 85% rename from pdk_reactor/extism_pdk_reactor.go rename to wasi-reactor/extism_pdk_reactor.go index 9765100..5a5bd4a 100644 --- a/pdk_reactor/extism_pdk_reactor.go +++ b/wasi-reactor/extism_pdk_reactor.go @@ -1,4 +1,4 @@ -package pdk_reactor +package reactor //export __wasm_call_ctors func __wasm_call_ctors() diff --git a/wasi-reactor/go.mod b/wasi-reactor/go.mod new file mode 100644 index 0000000..797bced --- /dev/null +++ b/wasi-reactor/go.mod @@ -0,0 +1,3 @@ +module github.com/extism/go-pdk/wasi-reactor + +go 1.21.1 From 1369b9cd950376efd653161e4ffb0c58dc07a266 Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Thu, 18 Jan 2024 21:13:25 +0300 Subject: [PATCH 3/4] add test for reactor example --- .github/workflows/ci.yml | 1 + Makefile | 2 ++ 2 files changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ce755d9..1c798bd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/Makefile b/Makefile index 31fcaeb..36f5d16 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ 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 @@ -9,6 +10,7 @@ example: 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" \ No newline at end of file From e7340dba5cd11ce2832d9271800c495c4711aa3e Mon Sep 17 00:00:00 2001 From: Muhammad Azeez Date: Thu, 18 Jan 2024 21:23:56 +0300 Subject: [PATCH 4/4] mention the package in the readme --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ example/reactor/README.md | 8 ++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 72104d2..bdd513f 100644 --- a/README.md +++ b/README.md @@ -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)! diff --git a/example/reactor/README.md b/example/reactor/README.md index bfa528c..d6a67aa 100644 --- a/example/reactor/README.md +++ b/example/reactor/README.md @@ -3,14 +3,14 @@ By including this package, you'll turn your plugin into a [Reactor](https://dyli 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! +# => 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 +# => 2024/01/18 20:48:48 open ./test.txt: errno 76 ``` \ No newline at end of file