Skip to content

Commit

Permalink
use go:wasmexport and c-shared everywhere
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
  • Loading branch information
evacchi committed Jan 23, 2025
1 parent 4f26acc commit dc2e875
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,25 @@ import (
"github.com/extism/go-pdk"
)

//export greet
//go:wasmexport greet
func greet() int32 {
input := pdk.Input()
greeting := `Hello, ` + string(input) + `!`
pdk.OutputString(greeting)
return 0
}

func main() {}
```

Some things to note about this code:

1. The `//export greet` comment is required. This marks the greet function as an
1. The `//go:wasmexport greet` comment is required. This marks the greet function as an
export with the name `greet` that can be called by the host.
2. We need a `main` but it is unused.
3. Exports in the Go PDK are coded to the raw ABI. You get parameters from the
2. Exports in the Go PDK are coded to the raw ABI. You get parameters from the
host by calling
[pdk.Input* functions](https://pkg.go.dev/github.com/extism/go-pdk#Input) and
you send returns back with the
[pdk.Output* functions](https://pkg.go.dev/github.com/extism/go-pdk#Output).
4. An Extism export expects an i32 return code. `0` is success and `1` is a
3. An Extism export expects an i32 return code. `0` is success and `1` is a
failure.

Install the `tinygo` compiler:
Expand All @@ -68,7 +65,7 @@ platform.
Compile this with the command:

```bash
tinygo build -o plugin.wasm -target wasip1 main.go
tinygo build -o plugin.wasm -target wasip1 -buildmode=c-shared main.go
```

We can now test `plugin.wasm` using the
Expand All @@ -92,7 +89,7 @@ use [pdk.SetError](https://pkg.go.dev/github.com/extism/go-pdk#SetError) or
[pdk.SetErrorString](https://pkg.go.dev/github.com/extism/go-pdk#SetErrorString):

```go
//export greet
//go:wasmexport greet
func greet() int32 {
name := string(pdk.Input())
if name == "Benjamin" {
Expand All @@ -110,6 +107,7 @@ Now when we try again:
```bash
extism call plugin.wasm greet --input="Benjamin" --wasi
# => Error: Sorry, we don't greet Benjamins!
# => returned non-zero exit code: 1
echo $? # print last status code
# => 1
extism call plugin.wasm greet --input="Zach" --wasi
Expand All @@ -134,7 +132,7 @@ type Sum struct {
Sum int `json:"sum"`
}

//export add
//go:wasmexport add
func add() int32 {
params := Add{}
// use json input helper, which automatically unmarshals the plugin input into your struct
Expand All @@ -145,7 +143,7 @@ func add() int32 {
}
sum := Sum{Sum: params.A + params.B}
// use json output helper, which automatically marshals your struct to the plugin output
output, err := pdk.OutputJSON(sum)
_, err := pdk.OutputJSON(sum)
if err != nil {
pdk.SetError(err)
return 1
Expand All @@ -167,7 +165,7 @@ that exists across every function call. Here is a trivial example using
[pdk.GetConfig](https://pkg.go.dev/github.com/extism/go-pdk#GetConfig):

```go
//export greet
//go:wasmexport greet
func greet() int32 {
user, ok := pdk.GetConfig("user")
if !ok {
Expand Down Expand Up @@ -195,7 +193,7 @@ will persist across function calls. These variables will persist as long as the
host has loaded and not freed the plug-in.

```go
//export count
//go:wasmexport count
func count() int32 {
count := pdk.GetVarInt("count")
count = count + 1
Expand All @@ -220,7 +218,7 @@ you to use the host application to log without having to give the plug-in
permission to make syscalls.

```go
//export log_stuff
//go:wasmexport log_stuff
func logStuff() int32 {
pdk.Log(pdk.LogInfo, "An info log!")
pdk.Log(pdk.LogDebug, "A debug log!")
Expand Down Expand Up @@ -252,7 +250,7 @@ Sometimes it is useful to let a plug-in
[See this example](example/http/tiny_main.go)

```go
//export http_get
//go:wasmexport 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 Down Expand Up @@ -304,7 +302,7 @@ We should be able to call this function as a normal Go function. Note that we
need to manually handle the pointer casting:

```go
//export hello_from_python
//go:wasmexport hello_from_python
func helloFromPython() int32 {
msg := "An argument to send to Python"
mem := pdk.AllocateString(msg)
Expand Down Expand Up @@ -377,6 +375,9 @@ If you want to use WASI inside your Reactor module functions (exported functions
than `main`). You can however import the `wasi-reactor` module to ensure that libc
and go runtime are initialized as expected:

Moreover, older versions may not provide the special `//go:wasmexport`
directive, and instead use `//export`.

```go
package main

Expand Down

0 comments on commit dc2e875

Please sign in to comment.