Skip to content

Commit 095ac11

Browse files
authored
feat: add Param/Result helpers for working with memory in non plugin functions (#31)
1 parent 8d2782c commit 095ac11

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

extism_pdk.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,48 @@ func FindMemory(offset uint64) Memory {
392392
}
393393
return Memory{extismPointer(offset), length}
394394
}
395+
396+
// Load bytes from Extism memory given an offset
397+
func ParamBytes(offset uint64) []byte {
398+
mem := FindMemory(offset)
399+
return mem.ReadBytes()
400+
}
401+
402+
// Load string from Extism memory given an offset
403+
func ParamString(offset uint64) string {
404+
return string(ParamBytes(offset))
405+
}
406+
407+
// Load uint32 from Extism memory given an offset
408+
func ParamU32(offset uint64) uint32 {
409+
return binary.LittleEndian.Uint32(ParamBytes(offset))
410+
}
411+
412+
// Load uint64 from Extism memory given an offset
413+
func ParamU64(offset uint64) uint64 {
414+
return binary.LittleEndian.Uint64(ParamBytes(offset))
415+
}
416+
417+
// Allocate bytes and return the offset in Extism memory
418+
func ResultBytes(d []byte) uint64 {
419+
mem := AllocateBytes(d)
420+
return mem.Offset()
421+
}
422+
423+
// Allocate a string and return the offset in Extism memory
424+
func ResultString(s string) uint64 {
425+
mem := AllocateString(s)
426+
return mem.Offset()
427+
}
428+
429+
// Allocate a uint32 and return the offset in Extism memory
430+
func ResultU32(d uint32) uint64 {
431+
mem := AllocateBytes(binary.LittleEndian.AppendUint32([]byte{}, d))
432+
return mem.Offset()
433+
}
434+
435+
// Allocate a uint64 and return the offset in Extism memory
436+
func ResultU64(d uint64) uint64 {
437+
mem := AllocateBytes(binary.LittleEndian.AppendUint64([]byte{}, d))
438+
return mem.Offset()
439+
}

0 commit comments

Comments
 (0)