Skip to content

Commit

Permalink
feat: add a json allocation helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Jul 14, 2024
1 parent 855c34f commit 478640b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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/tiny_reactor.wasm read_file --input "example/reactor/test.txt" --allow-path ./example/reactor --wasi --log-level info
extism call example/tiny_countvowels.wasm count_vowels_roundtrip_json_mem --wasi

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"
extism call example/std_http.wasm _start --wasi --log-level info --allow-host "jsonplaceholder.typicode.com"

27 changes: 27 additions & 0 deletions example/countvowels/std_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
countVowels()
countVowelsTyped()
countVowelsJSONOutput()
countVowelsJSONRoundtripMem()
}

// CountVowelsInput represents the JSON input provided by the host.
Expand Down Expand Up @@ -54,6 +55,32 @@ func countVowelsJSONOutput() int32 {
return 0
}

//export count_vowels_roundtrip_json_mem
func countVowelsJSONRoundtripMem() int32 {
a := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
mem, err := pdk.AllocateJSON(&a)
if err != nil {
pdk.SetError(err)
return -1
}

// find the data in mem and ensure it's the same once decoded
var b CountVowelsOuptut
err = pdk.JSONFrom(mem.Offset(), &b)
if err != nil {
pdk.SetError(err)
return -1
}

if a.Count != b.Count || a.Total != b.Total || a.Vowels != b.Vowels {
pdk.SetErrorString("roundtrip JSON failed")
return -1
}

pdk.OutputString("JSON roundtrip: a === b")
return 0
}

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

Expand Down
26 changes: 26 additions & 0 deletions example/countvowels/tiny_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ func countVowelsJSONOutput() int32 {
return 0
}

//export count_vowels_roundtrip_json_mem
func countVowelsJSONRoundtripMem() int32 {
a := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
mem, err := pdk.AllocateJSON(&a)
if err != nil {
pdk.SetError(err)
return -1
}

// find the data in mem and ensure it's the same once decoded
var b CountVowelsOuptut
err = pdk.JSONFrom(mem.Offset(), &b)
if err != nil {
pdk.SetError(err)
return -1
}

if a.Count != b.Count || a.Total != b.Total || a.Vowels != b.Vowels {
pdk.SetErrorString("roundtrip JSON failed")
return -1
}

pdk.OutputString("JSON roundtrip: a === b")
return 0
}

//export count_vowels
func countVowels() int32 {
input := pdk.Input()
Expand Down
10 changes: 10 additions & 0 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func AllocateString(data string) Memory {
return AllocateBytes([]byte(data))
}

// AllocateJSON allocates and saves the type `any` into Memory on the host.
func AllocateJSON(v any) (Memory, error) {
b, err := json.Marshal(v)
if err != nil {
return Memory{}, err
}

return AllocateBytes(b), nil
}

// InputString returns the input data from the host as a UTF-8 string.
func InputString() string {
return string(Input())
Expand Down

0 comments on commit 478640b

Please sign in to comment.