wasi-module-sdk-go/entrypoint.go

54 lines
1.3 KiB
Go
Raw Normal View History

2023-05-08 13:21:31 +00:00
package sdk
import (
"code.icb4dc0.de/buildr/wasi-module-sdk/internal/mem"
rpcv1 "code.icb4dc0.de/buildr/wasi-module-sdk/protocol/generated/rpc/v1"
"context"
"github.com/mailru/easyjson"
_ "github.com/tetratelabs/tinymem"
)
var defaultRegistry = NewTypeRegistry()
func Register(cat Category, moduleName string, factory Factory) {
defaultRegistry.Add(cat, moduleName, factory)
}
2023-05-13 15:46:09 +00:00
//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))
}
2023-05-08 13:21:31 +00:00
//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, "", "")
2023-05-13 15:46:09 +00:00
reference := startTask.GetReference().GetModule()
2023-05-08 13:21:31 +00:00
module := defaultRegistry.Get(Category(reference.GetModuleCategory()), reference.GetModuleType())
if err := easyjson.Unmarshal(startTask.RawTask, module); err != nil {
panic(err)
}
2023-05-13 15:46:09 +00:00
executor.Run(context.Background(), startTask.GetReference().GetName(), module)
2023-05-08 13:21:31 +00:00
}