Skip to content

Commit 33b1e36

Browse files
authored
feat: remove strings dependency, optimizations (#23)
1 parent 3828de8 commit 33b1e36

File tree

7 files changed

+138
-10
lines changed

7 files changed

+138
-10
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ jobs:
4747
echo $TEST | grep '"a": "this is var a"'
4848
4949
extism call example/tiny_http.wasm --wasi http_get --github-token="$GITHUB_TOKEN" --allow-host "jsonplaceholder.typicode.com" | grep '"userId": 1'
50+
51+
52+
# run all the tests
53+
make test

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ example:
33
tinygo build -o example/tiny_countvowels.wasm -target wasi ./example/countvowels
44
tinygo build -o example/tiny_http.wasm -target wasi ./example/http
55

6-
GOOS=wasip1 GOARCH=wasm go build -o example/std_countvowels.wasm ./example/countvowels
7-
GOOS=wasip1 GOARCH=wasm go build -o example/std_http.wasm ./example/http
6+
GOOS=wasip1 GOARCH=wasm go build -tags std -o example/std_countvowels.wasm ./example/countvowels
7+
GOOS=wasip1 GOARCH=wasm go build -tags std -o example/std_http.wasm ./example/http
88

99
test:
1010
extism call example/tiny_countvowels.wasm count_vowels --wasi --input "this is a test" --set-config '{"thing": "1234"}'
1111
extism call example/tiny_http.wasm http_get --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"
1212

13-
extism call example/std_countvowels.wasm count_vowels --wasi --input "this is a test" --set-config '{"thing": "1234"}'
14-
extism call example/std_http.wasm http_get --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"
13+
extism call example/std_countvowels.wasm _start --wasi --input "this is a test" --set-config '{"thing": "1234"}'
14+
extism call example/std_http.wasm _start --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"

example/countvowels/std_main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//go:build std
2+
// +build std
3+
4+
package main
5+
6+
import (
7+
"strconv"
8+
9+
"github.com/extism/go-pdk"
10+
)
11+
12+
// Currently, the standard Go compiler cannot export custom functions and is limited to exporting
13+
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
14+
// invoke by explicitly calling `_start`.
15+
func main() {
16+
countVowels()
17+
}
18+
19+
func countVowels() int32 {
20+
input := pdk.Input()
21+
22+
count := 0
23+
for _, a := range input {
24+
switch a {
25+
case 'A', 'I', 'E', 'O', 'U', 'a', 'e', 'i', 'o', 'u':
26+
count++
27+
default:
28+
}
29+
}
30+
31+
// test some extra pdk functionality
32+
if pdk.GetVar("a") == nil {
33+
pdk.SetVar("a", []byte("this is var a"))
34+
}
35+
varA := pdk.GetVar("a")
36+
thing, ok := pdk.GetConfig("thing")
37+
38+
if !ok {
39+
thing = "<unset by host>"
40+
}
41+
42+
output := `{"count": ` + strconv.Itoa(count) + `, "config": "` + thing + `", "a": "` + string(varA) + `"}`
43+
mem := pdk.AllocateString(output)
44+
45+
// zero-copy output to host
46+
pdk.OutputMemory(mem)
47+
48+
return 0
49+
}

example/countvowels/main.go renamed to example/countvowels/tiny_main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !std
2+
// +build !std
3+
14
package main
25

36
import (

example/http/std_main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build std
2+
// +build std
3+
4+
package main
5+
6+
import (
7+
"github.com/extism/go-pdk"
8+
)
9+
10+
// Currently, the standard Go compiler cannot export custom functions and is limited to exporting
11+
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
12+
// invoke by explicitly calling `_start`.
13+
func main() {
14+
httpGet()
15+
}
16+
17+
func httpGet() int32 {
18+
// create an HTTP Request (withuot relying on WASI), set headers as needed
19+
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://jsonplaceholder.typicode.com/todos/1")
20+
req.SetHeader("some-name", "some-value")
21+
req.SetHeader("another", "again")
22+
// send the request, get response back (can check status on response via res.Status())
23+
res := req.Send()
24+
25+
// zero-copy output to host
26+
pdk.OutputMemory(res.Memory())
27+
28+
return 0
29+
}

example/http/main.go renamed to example/http/tiny_main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !std
2+
// +build !std
3+
14
package main
25

36
import (
@@ -7,7 +10,7 @@ import (
710
//export http_get
811
func httpGet() int32 {
912
// create an HTTP Request (withuot relying on WASI), set headers as needed
10-
req := pdk.NewHTTPRequest("GET", "https://jsonplaceholder.typicode.com/todos/1")
13+
req := pdk.NewHTTPRequest(pdk.MethodGet, "https://jsonplaceholder.typicode.com/todos/1")
1114
req.SetHeader("some-name", "some-value")
1215
req.SetHeader("another", "again")
1316
// send the request, get response back (can check status on response via res.Status())

extism_pdk.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package pdk
33
import (
44
"encoding/binary"
55
"encoding/json"
6-
"strings"
76
)
87

98
type Memory struct {
@@ -258,13 +257,54 @@ func (r HTTPResponse) Status() uint16 {
258257
return r.status
259258
}
260259

261-
func NewHTTPRequest(method string, url string) *HTTPRequest {
260+
type HTTPMethod int32
261+
262+
const (
263+
MethodGet HTTPMethod = iota
264+
MethodHead
265+
MethodPost
266+
MethodPut
267+
MethodPatch // RFC 5789
268+
MethodDelete
269+
MethodConnect
270+
MethodOptions
271+
MethodTrace
272+
)
273+
274+
func (m HTTPMethod) String() string {
275+
switch m {
276+
case MethodGet:
277+
return "GET"
278+
case MethodHead:
279+
return "HEAD"
280+
case MethodPost:
281+
return "POST"
282+
case MethodPut:
283+
return "PUT"
284+
case MethodPatch:
285+
return "PATCH"
286+
case MethodDelete:
287+
return "DELETE"
288+
case MethodConnect:
289+
return "CONNECT"
290+
case MethodOptions:
291+
return "OPTIONS"
292+
case MethodTrace:
293+
return "TRACE"
294+
default:
295+
return ""
296+
}
297+
}
298+
299+
func NewHTTPRequest(method HTTPMethod, url string) *HTTPRequest {
262300
return &HTTPRequest{
263301
meta: HTTPRequestMeta{
264-
Url: url,
265-
Method: strings.ToUpper(method),
302+
Url: url,
303+
Headers: nil,
304+
Method: method.String(),
266305
},
267-
body: nil}
306+
body: nil,
307+
}
268308
}
269309

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

0 commit comments

Comments
 (0)