Skip to content

Commit

Permalink
feat: remove strings dependency, optimizations (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice authored Jan 17, 2024
1 parent 3828de8 commit 33b1e36
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 10 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ jobs:
echo $TEST | grep '"a": "this is var a"'
extism call example/tiny_http.wasm --wasi http_get --github-token="$GITHUB_TOKEN" --allow-host "jsonplaceholder.typicode.com" | grep '"userId": 1'
# run all the tests
make test
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ example:
tinygo build -o example/tiny_countvowels.wasm -target wasi ./example/countvowels
tinygo build -o example/tiny_http.wasm -target wasi ./example/http

GOOS=wasip1 GOARCH=wasm go build -o example/std_countvowels.wasm ./example/countvowels
GOOS=wasip1 GOARCH=wasm go build -o example/std_http.wasm ./example/http
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/std_countvowels.wasm count_vowels --wasi --input "this is a test" --set-config '{"thing": "1234"}'
extism call example/std_http.wasm http_get --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"
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"
49 changes: 49 additions & 0 deletions example/countvowels/std_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:build std
// +build std

package main

import (
"strconv"

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

// Currently, the standard Go compiler cannot export custom functions and is limited to exporting
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
// invoke by explicitly calling `_start`.
func main() {
countVowels()
}

func countVowels() int32 {
input := pdk.Input()

count := 0
for _, a := range input {
switch a {
case 'A', 'I', 'E', 'O', 'U', 'a', 'e', 'i', 'o', 'u':
count++
default:
}
}

// test some extra pdk functionality
if pdk.GetVar("a") == nil {
pdk.SetVar("a", []byte("this is var a"))
}
varA := pdk.GetVar("a")
thing, ok := pdk.GetConfig("thing")

if !ok {
thing = "<unset by host>"
}

output := `{"count": ` + strconv.Itoa(count) + `, "config": "` + thing + `", "a": "` + string(varA) + `"}`
mem := pdk.AllocateString(output)

// zero-copy output to host
pdk.OutputMemory(mem)

return 0
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !std
// +build !std

package main

import (
Expand Down
29 changes: 29 additions & 0 deletions example/http/std_main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build std
// +build std

package main

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

// Currently, the standard Go compiler cannot export custom functions and is limited to exporting
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
// invoke by explicitly calling `_start`.
func main() {
httpGet()
}

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")
req.SetHeader("some-name", "some-value")
req.SetHeader("another", "again")
// send the request, get response back (can check status on response via res.Status())
res := req.Send()

// zero-copy output to host
pdk.OutputMemory(res.Memory())

return 0
}
5 changes: 4 additions & 1 deletion example/http/main.go → example/http/tiny_main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !std
// +build !std

package main

import (
Expand All @@ -7,7 +10,7 @@ import (
//export http_get
func httpGet() int32 {
// create an HTTP Request (withuot relying on WASI), set headers as needed
req := pdk.NewHTTPRequest("GET", "https://jsonplaceholder.typicode.com/todos/1")
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://jsonplaceholder.typicode.com/todos/1")
req.SetHeader("some-name", "some-value")
req.SetHeader("another", "again")
// send the request, get response back (can check status on response via res.Status())
Expand Down
50 changes: 45 additions & 5 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package pdk
import (
"encoding/binary"
"encoding/json"
"strings"
)

type Memory struct {
Expand Down Expand Up @@ -258,13 +257,54 @@ func (r HTTPResponse) Status() uint16 {
return r.status
}

func NewHTTPRequest(method string, url string) *HTTPRequest {
type HTTPMethod int32

const (
MethodGet HTTPMethod = iota
MethodHead
MethodPost
MethodPut
MethodPatch // RFC 5789
MethodDelete
MethodConnect
MethodOptions
MethodTrace
)

func (m HTTPMethod) String() string {
switch m {
case MethodGet:
return "GET"
case MethodHead:
return "HEAD"
case MethodPost:
return "POST"
case MethodPut:
return "PUT"
case MethodPatch:
return "PATCH"
case MethodDelete:
return "DELETE"
case MethodConnect:
return "CONNECT"
case MethodOptions:
return "OPTIONS"
case MethodTrace:
return "TRACE"
default:
return ""
}
}

func NewHTTPRequest(method HTTPMethod, url string) *HTTPRequest {
return &HTTPRequest{
meta: HTTPRequestMeta{
Url: url,
Method: strings.ToUpper(method),
Url: url,
Headers: nil,
Method: method.String(),
},
body: nil}
body: nil,
}
}

func (r *HTTPRequest) SetHeader(key string, value string) *HTTPRequest {
Expand Down

0 comments on commit 33b1e36

Please sign in to comment.