Custom aggregate functions.

This commit is contained in:
Nuno Cruces
2023-07-01 15:14:45 +01:00
parent fec1f8d32a
commit 3844e81404
9 changed files with 221 additions and 32 deletions

View File

@@ -14,6 +14,7 @@ import (
type Context struct {
*module
handle uint32
final bool
}
// ResultBool sets the result of the function to a bool.
@@ -154,4 +155,27 @@ func (c *Context) ResultError(err error) {
c.call(c.api.resultErrorCode,
uint64(c.handle), uint64(xcode))
}
}
func AggregateContext[T any](ctx Context) *T {
var size uint64
if !ctx.final {
size = ptrlen
}
pAgg := uint32(ctx.call(ctx.api.aggregateData, uint64(ctx.handle), size))
if pAgg == 0 {
return nil
}
pData := util.ReadUint32(ctx.mod, pAgg)
if data := util.GetHandle(ctx.ctx, pData); data != nil {
return data.(*T)
}
if ctx.final {
return nil
}
data := new(T)
pData = util.AddHandle(ctx.ctx, data)
util.WriteUint32(ctx.mod, pAgg, pData)
return data
}