Skip to content

Commit de6b7b8

Browse files
authored
Add godocs and free memory in more places (#35)
* Add more detailed documentation * Add godocs * Address review feedback * Free allocated memory for output functions * Revert change * Remove backticks from function name godocs --------- Signed-off-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com>
1 parent e565f18 commit de6b7b8

File tree

6 files changed

+215
-87
lines changed

6 files changed

+215
-87
lines changed

env.go

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,144 @@ package pdk
22

33
type extismPointer uint64
44

5+
// extismInputLength returns the number of bytes provided by the host via its input methods.
6+
//
57
//go:wasmimport extism:host/env input_length
6-
func extism_input_length() uint64
8+
func extismInputLength() uint64
79

10+
// extismLength returns the number of bytes associated with the block of host memory
11+
// located at `offset`.
12+
//
813
//go:wasmimport extism:host/env length
9-
func extism_length(extismPointer) uint64
14+
func extismLength(offset extismPointer) uint64
1015

1116
//go:wasmimport extism:host/env length_unsafe
12-
func extism_length_unsafe(extismPointer) uint64
17+
func extismLengthUnsafe(extismPointer) uint64
1318

19+
// extismAlloc allocates `length` bytes of data with host memory for use by the plugin
20+
// and returns its offset within the host memory block.
21+
//
1422
//go:wasmimport extism:host/env alloc
15-
func extism_alloc(uint64) extismPointer
23+
func extismAlloc(length uint64) extismPointer
1624

25+
// extismFree releases the bytes previously allocated with `extism_alloc` at the given `offset`.
26+
//
1727
//go:wasmimport extism:host/env free
18-
func extism_free(extismPointer)
28+
func extismFree(offset extismPointer)
1929

30+
// extismInputLoadU8 returns the byte at location `offset` of the "input" data from the host.
31+
//
2032
//go:wasmimport extism:host/env input_load_u8
21-
func extism_input_load_u8_(extismPointer) uint32
22-
23-
func extism_input_load_u8(p extismPointer) uint8 {
24-
return uint8(extism_input_load_u8_(p))
33+
func extismInputLoadU8_(offset extismPointer) uint32
34+
func extismInputLoadU8(offset extismPointer) uint8 {
35+
return uint8(extismInputLoadU8_(offset))
2536
}
2637

38+
// extismInputLoadU64 returns the 64-bit unsigned integer of the "input" data from the host.
39+
// Note that `offset` must lie on an 8-byte boundary.
40+
//
2741
//go:wasmimport extism:host/env input_load_u64
28-
func extism_input_load_u64(extismPointer) uint64
42+
func extismInputLoadU64(offset extismPointer) uint64
2943

44+
// extismOutputSet sets the "output" data from the plugin to the host to be the memory that
45+
// has been written at `offset` with the given `length`.
46+
// The memory can be immediately freed because the host makes a copy for its use.
47+
//
3048
//go:wasmimport extism:host/env output_set
31-
func extism_output_set(extismPointer, uint64)
49+
func extismOutputSet(offset extismPointer, length uint64)
3250

51+
// extismErrorSet sets the "error" data from the plugin to the host to be the memory that
52+
// has been written at `offset`.
53+
// The memory can be immediately freed because the host makes a copy for its use.
54+
//
3355
//go:wasmimport extism:host/env error_set
34-
func extism_error_set(extismPointer)
56+
func extismErrorSet(offset extismPointer)
3557

58+
// extismConfigGet returns the host memory block offset for the "config" data associated with
59+
// the key which is represented by the UTF-8 string which as been previously written at `offset`.
60+
// The memory for the key can be immediately freed because the host has its own copy.
61+
//
3662
//go:wasmimport extism:host/env config_get
37-
func extism_config_get(extismPointer) extismPointer
63+
func extismConfigGet(offset extismPointer) extismPointer
3864

65+
// extismVarGet returns the host memory block offset for the "var" data associated with
66+
// the key which is represented by the UTF-8 string which as been previously written at `offset`.
67+
// The memory for the key can be immediately freed because the host has its own copy.
68+
//
3969
//go:wasmimport extism:host/env var_get
40-
func extism_var_get(extismPointer) extismPointer
41-
70+
func extismVarGet(offset extismPointer) extismPointer
71+
72+
// extismVarSet sets the host "var" memory keyed by the UTF-8 string located at `offset`
73+
// to be the value which has been previously written at `valueOffset`.
74+
//
75+
// A `valueOffset` of 0 causes the old value associated with this key to be freed on the host
76+
// and the association to be completely removed.
77+
//
78+
// The memory for the key can be immediately freed because the host has its own copy.
79+
// The memory for the value, however, should not be freed, as that erases the value from the host.
80+
//
4281
//go:wasmimport extism:host/env var_set
43-
func extism_var_set(extismPointer, extismPointer)
82+
func extismVarSet(offset, valueOffset extismPointer)
4483

84+
// extismStoreU8 stores the byte `v` at location `offset` in the host memory block.
85+
//
4586
//go:wasmimport extism:host/env store_u8
46-
func extism_store_u8_(extismPointer, uint32)
47-
func extism_store_u8(p extismPointer, v uint8) {
48-
extism_store_u8_(p, uint32(v))
87+
func extismStoreU8_(extismPointer, uint32)
88+
func extismStoreU8(offset extismPointer, v uint8) {
89+
extismStoreU8_(offset, uint32(v))
4990
}
5091

92+
// extismLoadU8 returns the byte located at `offset` in the host memory block.
93+
//
5194
//go:wasmimport extism:host/env load_u8
52-
func extism_load_u8_(extismPointer) uint32
53-
func extism_load_u8(p extismPointer) uint8 {
54-
return uint8(extism_load_u8_(p))
95+
func extismLoadU8_(offset extismPointer) uint32
96+
func extismLoadU8(offset extismPointer) uint8 {
97+
return uint8(extismLoadU8_(offset))
5598
}
5699

100+
// extismStoreU64 stores the 64-bit unsigned integer value `v` at location `offset` in the host memory block.
101+
// Note that `offset` must lie on an 8-byte boundary.
102+
//
57103
//go:wasmimport extism:host/env store_u64
58-
func extism_store_u64(extismPointer, uint64)
104+
func extismStoreU64(offset extismPointer, v uint64)
59105

106+
// extismLoadU64 returns the 64-bit unsigned integer at location `offset` in the host memory block.
107+
// Note that `offset` must lie on an 8-byte boundary.
108+
//
60109
//go:wasmimport extism:host/env load_u64
61-
func extism_load_u64(extismPointer) uint64
110+
func extismLoadU64(offset extismPointer) uint64
62111

112+
// extismHTTPRequest sends the HTTP `request` to the Extism host with the provided `body` (0 means no body)
113+
// and returns back the memory offset to the response body.
114+
//
63115
//go:wasmimport extism:host/env http_request
64-
func extism_http_request(extismPointer, extismPointer) extismPointer
116+
func extismHTTPRequest(request, body extismPointer) extismPointer
65117

118+
// extismHTTPStatusCode returns the status code for the last-sent `extism_http_request` call.
119+
//
66120
//go:wasmimport extism:host/env http_status_code
67-
func extism_http_status_code() int32
121+
func extismHTTPStatusCode() int32
68122

123+
// extismLogInfo logs an "info" string to the host from the previously-written UTF-8 string written to `offset`.
124+
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
125+
//
69126
//go:wasmimport extism:host/env log_info
70-
func extism_log_info(extismPointer)
127+
func extismLogInfo(offset extismPointer)
71128

129+
// extismLogDebug logs a "debug" string to the host from the previously-written UTF-8 string written to `offset`.
130+
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
131+
//
72132
//go:wasmimport extism:host/env log_debug
73-
func extism_log_debug(extismPointer)
133+
func extismLogDebug(offset extismPointer)
74134

135+
// extismLogWarn logs a "warning" string to the host from the previously-written UTF-8 string written to `offset`.
136+
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
137+
//
75138
//go:wasmimport extism:host/env log_warn
76-
func extism_log_warn(extismPointer)
139+
func extismLogWarn(offset extismPointer)
77140

141+
// extismLogError logs an "error" string to the host from the previously-written UTF-8 string written to `offset`.
142+
// Note that the memory at `offset` can be immediately freed because it is immediately logged.
143+
//
78144
//go:wasmimport extism:host/env log_error
79-
func extism_log_error(extismPointer)
145+
func extismLogError(offset extismPointer)

example/countvowels/std_main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ import (
1414
// `_start` via WASI. So, `main` functions should contain the plugin behavior, that the host will
1515
// invoke by explicitly calling `_start`.
1616
func main() {
17-
count_vowels()
18-
count_vowels_typed()
19-
count_vowels_json_output()
17+
countVowels()
18+
countVowelsTyped()
19+
countVowelsJSONOutput()
2020
}
2121

22+
// CountVowelsInput represents the JSON input provided by the host.
2223
type CountVowelsInput struct {
2324
Input string `json:"input"`
2425
}
2526

27+
// CountVowelsOutput represents the JSON output sent to the host.
2628
type CountVowelsOuptut struct {
2729
Count int `json:"count"`
2830
Total int `json:"total"`
2931
Vowels string `json:"vowels"`
3032
}
3133

3234
//export count_vowels_typed
33-
func count_vowels_typed() int32 {
35+
func countVowelsTyped() int32 {
3436
var input CountVowelsInput
3537
if err := pdk.InputJSON(&input); err != nil {
3638
pdk.SetError(err)
@@ -42,7 +44,7 @@ func count_vowels_typed() int32 {
4244
}
4345

4446
//export count_vowels_json_output
45-
func count_vowels_json_output() int32 {
47+
func countVowelsJSONOutput() int32 {
4648
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
4749
err := pdk.OutputJSON(output)
4850
if err != nil {
@@ -52,7 +54,7 @@ func count_vowels_json_output() int32 {
5254
return 0
5355
}
5456

55-
func count_vowels() int32 {
57+
func countVowels() int32 {
5658
input := pdk.Input()
5759

5860
count := 0

example/countvowels/tiny_main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ import (
99
"github.com/extism/go-pdk"
1010
)
1111

12+
// CountVowelsInput represents the JSON input provided by the host.
1213
type CountVowelsInput struct {
1314
Input string `json:"input"`
1415
}
1516

17+
// CountVowelsOutput represents the JSON output sent to the host.
1618
type CountVowelsOuptut struct {
1719
Count int `json:"count"`
1820
Total int `json:"total"`
1921
Vowels string `json:"vowels"`
2022
}
2123

2224
//export count_vowels_typed
23-
func count_vowels_typed() int32 {
25+
func countVowelsTyped() int32 {
2426
var input CountVowelsInput
2527
if err := pdk.InputJSON(&input); err != nil {
2628
pdk.SetError(err)
@@ -32,7 +34,7 @@ func count_vowels_typed() int32 {
3234
}
3335

3436
//export count_vowels_json_output
35-
func count_vowels_json_output() int32 {
37+
func countVowelsJSONOutput() int32 {
3638
output := CountVowelsOuptut{Count: 42, Total: 2.1e7, Vowels: "aAeEiIoOuUyY"}
3739
err := pdk.OutputJSON(output)
3840
if err != nil {
@@ -43,7 +45,7 @@ func count_vowels_json_output() int32 {
4345
}
4446

4547
//export count_vowels
46-
func count_vowels() int32 {
48+
func countVowels() int32 {
4749
input := pdk.Input()
4850

4951
count := 0

example/reactor/tiny_main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
//export read_file
14-
func read_file() {
14+
func readFile() {
1515
name := pdk.InputString()
1616

1717
content, err := os.ReadFile(name)

0 commit comments

Comments
 (0)