Custom collating sequences.

This commit is contained in:
Nuno Cruces
2023-06-30 02:49:21 +01:00
parent c7904d30de
commit 6a982559cd
5 changed files with 89 additions and 2 deletions

22
func.go
View File

@@ -8,6 +8,21 @@ import (
"github.com/tetratelabs/wazero/api"
)
// CreateCollation defines a new collating sequence.
//
// https://www.sqlite.org/c3ref/create_collation.html
func (c *Conn) CreateCollation(name string, fn func(a, b []byte) int) error {
namePtr := c.arena.string(name)
funcPtr := util.AddHandle(c.ctx, fn)
r := c.call(c.api.createCollation,
uint64(c.handle), uint64(namePtr), uint64(funcPtr))
if err := c.error(r); err != nil {
util.DelHandle(c.ctx, funcPtr)
return err
}
return nil
}
func exportHostFunctions(env wazero.HostModuleBuilder) wazero.HostModuleBuilder {
util.ExportFuncVI(env, "go_destroy", cbDestroy)
util.ExportFuncIIIIII(env, "go_compare", cbCompare)
@@ -19,10 +34,13 @@ func exportHostFunctions(env wazero.HostModuleBuilder) wazero.HostModuleBuilder
return env
}
func cbDestroy(ctx context.Context, mod api.Module, pArg uint32) {}
func cbDestroy(ctx context.Context, mod api.Module, pArg uint32) {
util.DelHandle(ctx, pArg)
}
func cbCompare(ctx context.Context, mod api.Module, pArg, nKey1, pKey1, nKey2, pKey2 uint32) uint32 {
return 0
fn := util.GetHandle(ctx, pArg).(func(a, b []byte) int)
return uint32(fn(util.View(mod, pKey1, uint64(nKey1)), util.View(mod, pKey2, uint64(nKey2))))
}
func cbFunc(ctx context.Context, mod api.Module, pCtx, nArg, pArg uint32) {}