Skip to content

Commit

Permalink
feat: add Param/Result helpers for working with memory in non plugin …
Browse files Browse the repository at this point in the history
…functions
  • Loading branch information
zshipko committed Feb 8, 2024
1 parent fae85da commit 2c4c567
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions extism_pdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 2c4c567

Please sign in to comment.