Skip to content

Commit

Permalink
feat: support EXTISM_ENABLE_WASI_OUTPUT (#51)
Browse files Browse the repository at this point in the history
for any program that embeds the Go SDK, 

```sh
EXTISM_ENABLE_WASI_OUTPUT=1 extism call code.wasm foo --wasi 
will print things from e.g. fmt.Println (to stdout or stderr as needed)
```

brings the SDK into parity with the Rust runtime which has the same
behavior.

---------

Co-authored-by: zach <[email protected]>
  • Loading branch information
nilslice and zshipko authored Jan 2, 2024
1 parent 073f096 commit 43f5bda
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ func NewPlugin(
// See: https://github.com/extism/go-sdk/pull/1#issuecomment-1650527495
moduleConfig = moduleConfig.WithStartFunctions().WithFSConfig(fs)

_, wasiOutput := os.LookupEnv("EXTISM_ENABLE_WASI_OUTPUT")
if c.hasWasi && wasiOutput {
moduleConfig = moduleConfig.WithStderr(os.Stderr).WithStdout(os.Stdout)
}

// Try to find the main module:
// - There is always one main module
// - If a Wasm value has the Name field set to "main" then use that module
Expand Down
10 changes: 10 additions & 0 deletions plugins/println/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/extism/extism-sdk-plugins-println

go 1.21.0

toolchain go1.21.1

require (
github.com/extism/go-pdk v1.0.0-rc2 // indirect
github.com/valyala/fastjson v1.6.3 // indirect
)
8 changes: 8 additions & 0 deletions plugins/println/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/extism/go-pdk v0.0.0-20230119214914-65bffbeb3e64 h1:IfR1k741q+yQLvv5sLShCkvt3FgKU4wQVJfp7hhb/iY=
github.com/extism/go-pdk v0.0.0-20230119214914-65bffbeb3e64/go.mod h1:1wdiAoG8306g4WK+6laBrS+75089/0V4XRVTllt8b5U=
github.com/extism/go-pdk v1.0.0-rc1.0.20231019212020-62d54a6e0263 h1:uSo+K1JhsMlamDmaxyAGj0dGExZHLsL+Edyug0dC+uk=
github.com/extism/go-pdk v1.0.0-rc1.0.20231019212020-62d54a6e0263/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
github.com/extism/go-pdk v1.0.0-rc2 h1:EZuo8idErnV8KB8TTSTiO2T1lkX047OQ7K8STjn2WBs=
github.com/extism/go-pdk v1.0.0-rc2/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
github.com/valyala/fastjson v1.6.3 h1:tAKFnnwmeMGPbwJ7IwxcTPCNr3uIzoIj3/Fh90ra4xc=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
16 changes: 16 additions & 0 deletions plugins/println/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"

pdk "github.com/extism/go-pdk"
)

//export run_test
func run_test() int32 {
input := pdk.InputString()
fmt.Println("this was printed from the plugin", input)
return 0
}

func main() {}
Binary file added wasm/println.wasm
Binary file not shown.

0 comments on commit 43f5bda

Please sign in to comment.