-
Notifications
You must be signed in to change notification settings - Fork 9
/
extism_pdk.go
439 lines (350 loc) · 8.35 KB
/
extism_pdk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package pdk
import (
"encoding/binary"
"encoding/json"
)
type Memory struct {
offset extismPointer
length uint64
}
type LogLevel int
const (
LogInfo LogLevel = iota
LogDebug
LogWarn
LogError
LogTrace
)
func load(offset extismPointer, buf []byte) {
length := len(buf)
chunkCount := length >> 3
for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
binary.LittleEndian.PutUint64(buf[i:i+8], extism_load_u64(offset+extismPointer(i)))
}
remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
buf[index] = extism_load_u8(offset + extismPointer(index))
}
}
func loadInput() []byte {
length := int(extism_input_length())
buf := make([]byte, length)
chunkCount := length >> 3
for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
binary.LittleEndian.PutUint64(buf[i:i+8], extism_input_load_u64(extismPointer(i)))
}
remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
buf[index] = extism_input_load_u8(extismPointer(index))
}
return buf
}
func store(offset extismPointer, buf []byte) {
length := len(buf)
chunkCount := length >> 3
for chunkIdx := 0; chunkIdx < chunkCount; chunkIdx++ {
i := chunkIdx << 3
x := binary.LittleEndian.Uint64(buf[i : i+8])
extism_store_u64(offset+extismPointer(i), x)
}
remainder := length & 7
remainderOffset := chunkCount << 3
for index := remainderOffset; index < (remainder + remainderOffset); index++ {
extism_store_u8(offset+extismPointer(index), buf[index])
}
}
func Input() []byte {
return loadInput()
}
func JSONFrom(offset uint64, v any) error {
mem := FindMemory(offset)
return json.Unmarshal(mem.ReadBytes(), v)
}
func InputJSON(v any) error {
return json.Unmarshal(Input(), v)
}
func OutputJSON(v any) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
OutputMemory(AllocateBytes(b))
return nil
}
func Allocate(length int) Memory {
clength := uint64(length)
offset := extism_alloc(clength)
return Memory{
offset: offset,
length: clength,
}
}
func AllocateBytes(data []byte) Memory {
clength := uint64(len(data))
offset := extism_alloc(clength)
store(offset, data)
return Memory{
offset: offset,
length: clength,
}
}
func AllocateString(data string) Memory {
return AllocateBytes([]byte(data))
}
func InputString() string {
return string(Input())
}
func OutputMemory(mem Memory) {
extism_output_set(mem.offset, mem.length)
}
func Output(data []byte) {
clength := uint64(len(data))
offset := extism_alloc(clength)
store(offset, data)
extism_output_set(offset, clength)
}
func OutputString(s string) {
Output([]byte(s))
}
func SetError(err error) {
SetErrorString(err.Error())
}
func SetErrorString(err string) {
mem := AllocateString(err)
extism_error_set(mem.offset)
}
func GetConfig(key string) (string, bool) {
mem := AllocateBytes([]byte(key))
defer mem.Free()
offset := extism_config_get(mem.offset)
clength := extism_length(offset)
if offset == 0 || clength == 0 {
return "", false
}
value := make([]byte, clength)
load(offset, value)
return string(value), true
}
func LogMemory(level LogLevel, memory Memory) {
switch level {
case LogInfo:
extism_log_info(memory.offset)
case LogDebug:
extism_log_debug(memory.offset)
case LogWarn:
extism_log_warn(memory.offset)
case LogError:
extism_log_error(memory.offset)
}
}
func Log(level LogLevel, s string) {
mem := AllocateString(s)
defer mem.Free()
LogMemory(level, mem)
}
func GetVar(key string) []byte {
mem := AllocateBytes([]byte(key))
offset := extism_var_get(mem.offset)
clength := extism_length(offset)
if offset == 0 || clength == 0 {
return nil
}
value := make([]byte, clength)
load(offset, value)
return value
}
func SetVar(key string, value []byte) {
keyMem := AllocateBytes([]byte(key))
defer keyMem.Free()
valMem := AllocateBytes(value)
defer valMem.Free()
extism_var_set(keyMem.offset, valMem.offset)
}
func GetVarInt(key string) int {
mem := AllocateBytes([]byte(key))
offset := extism_var_get(mem.offset)
clength := extism_length(offset)
if offset == 0 || clength == 0 {
return 0
}
value := make([]byte, clength)
load(offset, value)
return int(binary.LittleEndian.Uint64(value))
}
func SetVarInt(key string, value int) {
keyMem := AllocateBytes([]byte(key))
defer keyMem.Free()
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, uint64(value))
valMem := AllocateBytes(bytes)
defer valMem.Free()
extism_var_set(keyMem.offset, valMem.offset)
}
func RemoveVar(key string) {
mem := AllocateBytes([]byte(key))
extism_var_set(mem.offset, 0)
}
type HTTPRequestMeta struct {
Url string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
}
type HTTPRequest struct {
meta HTTPRequestMeta
body []byte
}
type HTTPResponse struct {
memory Memory
status uint16
}
func (r HTTPResponse) Memory() Memory {
return r.memory
}
func (r HTTPResponse) Body() []byte {
buf := make([]byte, r.memory.length)
r.memory.Load(buf)
return buf
}
func (r HTTPResponse) Status() uint16 {
return r.status
}
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,
Headers: nil,
Method: method.String(),
},
body: nil,
}
}
func (r *HTTPRequest) SetHeader(key string, value string) *HTTPRequest {
if r.meta.Headers == nil {
r.meta.Headers = make(map[string]string)
}
r.meta.Headers[key] = value
return r
}
func (r *HTTPRequest) SetBody(body []byte) *HTTPRequest {
r.body = body
return r
}
func (r *HTTPRequest) Send() HTTPResponse {
enc, _ := json.Marshal(r.meta)
req := AllocateBytes(enc)
defer req.Free()
data := AllocateBytes(r.body)
defer data.Free()
offset := extism_http_request(req.offset, data.offset)
length := extism_length_unsafe(offset)
status := uint16(extism_http_status_code())
memory := Memory{offset, length}
return HTTPResponse{
memory,
status,
}
}
func (m *Memory) ReadBytes() []byte {
buff := make([]byte, m.length)
m.Load(buff)
return buff
}
func (m *Memory) Load(buffer []byte) {
load(m.offset, buffer)
}
func (m *Memory) Store(data []byte) {
store(m.offset, data)
}
func (m *Memory) Free() {
extism_free(m.offset)
}
func (m *Memory) Length() uint64 {
return m.length
}
func (m *Memory) Offset() uint64 {
return uint64(m.offset)
}
func FindMemory(offset uint64) Memory {
length := extism_length(extismPointer(offset))
if length == 0 {
return Memory{0, 0}
}
return Memory{extismPointer(offset), length}
}
// Load bytes from Extism memory given an offset
func ParamBytes(offset uint64) []byte {
mem := FindMemory(offset)
return mem.ReadBytes()
}
// Load string from Extism memory given an offset
func ParamString(offset uint64) string {
return string(ParamBytes(offset))
}
// Load uint32 from Extism memory given an offset
func ParamU32(offset uint64) uint32 {
return binary.LittleEndian.Uint32(ParamBytes(offset))
}
// Load uint64 from Extism memory given an offset
func ParamU64(offset uint64) uint64 {
return binary.LittleEndian.Uint64(ParamBytes(offset))
}
// Allocate bytes and return the offset in Extism memory
func ResultBytes(d []byte) uint64 {
mem := AllocateBytes(d)
return mem.Offset()
}
// Allocate a string and return the offset in Extism memory
func ResultString(s string) uint64 {
mem := AllocateString(s)
return mem.Offset()
}
// Allocate a uint32 and return the offset in Extism memory
func ResultU32(d uint32) uint64 {
mem := AllocateBytes(binary.LittleEndian.AppendUint32([]byte{}, d))
return mem.Offset()
}
// Allocate a uint64 and return the offset in Extism memory
func ResultU64(d uint64) uint64 {
mem := AllocateBytes(binary.LittleEndian.AppendUint64([]byte{}, d))
return mem.Offset()
}