From 095ac11ddc4e34055adf140aef24d51f920c3fc5 Mon Sep 17 00:00:00 2001 From: zach Date: Mon, 8 Apr 2024 09:13:39 -0700 Subject: [PATCH] feat: add Param/Result helpers for working with memory in non plugin functions (#31) --- extism_pdk.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/extism_pdk.go b/extism_pdk.go index 7ff8794..3410791 100644 --- a/extism_pdk.go +++ b/extism_pdk.go @@ -392,3 +392,48 @@ func FindMemory(offset uint64) Memory { } 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() +}