Skip to content

Commit

Permalink
fix: readme/tinygo examples use go:wasmexport/wasmimport, -buildmode=…
Browse files Browse the repository at this point in the history
…c-shared

Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Jan 23, 2025
1 parent 1502218 commit f82b09e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
.PHONY: example
example:
tinygo build -o example/tiny_countvowels.wasm -target wasi -buildmode c-shared ./example/countvowels
tinygo build -o example/tiny_http.wasm -target wasi -buildmode c-shared ./example/http
tinygo build -o example/tiny_reactor.wasm -target wasi -buildmode c-shared ./example/reactor
tinygo build -o example/tiny_countvowels.wasm -target wasip1 -buildmode c-shared ./example/countvowels
tinygo build -o example/tiny_http.wasm -target wasip1 -buildmode c-shared ./example/http
tinygo build -o example/tiny_reactor.wasm -target wasip1 -buildmode c-shared ./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
Expand Down
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ platform.
Compile this with the command:

```bash
tinygo build -o plugin.wasm -target wasi main.go
tinygo build -o plugin.wasm -target wasip1 main.go
```

We can now test `plugin.wasm` using the
Expand All @@ -79,7 +79,7 @@ extism call plugin.wasm greet --input "Benjamin" --wasi
# => Hello, Benjamin!
```

> **Note**: Currently `wasi` must be provided for all Go plug-ins even if they
> **Note**: Currently `wasip1` must be provided for all Go plug-ins even if they
> don't need system access, however this will eventually be optional.
> **Note**: We also have a web-based, plug-in tester called the
Expand Down Expand Up @@ -356,11 +356,26 @@ python3 app.py

## 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:
Since TinyGo version 0.34.0, the compiler has native support for
[Reactor modules](https://dylibso.com/blog/wasi-command-reactor/).

Make sure you invoke the compiler with the `-buildmode=c-shared` flag
so that libc and the Go runtime are properly initialized:

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

### Note on TinyGo 0.33.0 and earlier

TinyGo versions below 0.34.0 do not support
[Reactor modules](https://dylibso.com/blog/wasi-command-reactor/).
If you want to use WASI inside your Reactor module functions (exported functions other
than `main`). You can however import the `wasi-reactor` module to ensure that libc
and go runtime are initialized as expected:

```go
package main
Expand Down Expand Up @@ -389,7 +404,7 @@ func main() {}
```

```bash
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
tinygo build -target wasip1 -o reactor.wasm ./tiny_main.go
extism call ./reactor.wasm read_file --input "./test.txt" --allow-path . --wasi --log-level info
# => Hello World!
```
Expand Down
3 changes: 1 addition & 2 deletions example/http/tiny_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/extism/go-pdk"
)

//export http_get
//go:wasmimport http_get
func httpGet() int32 {
// create an HTTP Request (withuot relying on WASI), set headers as needed
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://jsonplaceholder.typicode.com/todos/1")
Expand All @@ -21,4 +21,3 @@ func httpGet() int32 {

return 0
}

12 changes: 7 additions & 5 deletions example/reactor/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
## 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.
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:
This is only required for TinyGo versions below 0.34.0 where native support for reactort-style modules
was missing.To test this example, run:

```bash
tinygo build -target wasi -o reactor.wasm .\tiny_main.go
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
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
```
```

0 comments on commit f82b09e

Please sign in to comment.