wasi-module-sdk-go/entrypoint.go

55 lines
1.3 KiB
Go

package sdk
import (
"context"
"github.com/mailru/easyjson"
_ "github.com/tetratelabs/tinymem"
"code.icb4dc0.de/buildr/wasi-module-sdk/mem"
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk/protocol/generated/rpc/v1"
)
var defaultRegistry = NewTypeRegistry()
func Register(cat Category, moduleName string, factory Factory) {
defaultRegistry.Add(cat, moduleName, factory)
}
//export inventory
func Inventory() uint64 {
var inventory rpcv1.PluginInventory
for _, t := range defaultRegistry.List() {
inventory.Modules = append(inventory.Modules, &rpcv1.ModuleReference{
ModuleCategory: t.Category.String(),
ModuleType: t.Type,
})
}
data, err := inventory.MarshalVT()
if err != nil {
panic(err)
}
return mem.UnifyPtrSize(mem.DataToUnmanagedPtr(data))
}
//export run
func Run(specPtr, specSize uint32) {
var startTask rpcv1.StartTaskRequest
if err := startTask.UnmarshalVT(mem.DataFromPtr(specPtr, specSize)); err != nil {
panic(err)
}
executor := NewExecutor(startTask.Buildr.Repo.Root, "", "")
reference := startTask.GetReference().GetModule()
module := defaultRegistry.Get(Category(reference.GetModuleCategory()), reference.GetModuleType())
if err := easyjson.Unmarshal(startTask.RawTask, module); err != nil {
panic(err)
}
executor.Run(context.Background(), startTask.GetReference().GetName(), module)
}