Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Param/Result helpers for working with memory in non plugin functions #31

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading