// SPDX-FileCopyrightText: 2025 Romain Maneschi // // SPDX-License-Identifier: EUPL-1.2 package plugin import ( "context" "fmt" "path/filepath" "sync" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/api" "github.com/tetratelabs/wazero/experimental" pluginLib "gitroot.dev/libs/golang/plugin/model" "gitroot.dev/server/logger" ) type runtimes struct { lockRuntime sync.Mutex runtime *runtime log *logger.Logger start chan runtimeInputs } func newRuntimes(manager *Manager, log *logger.Logger) *runtimes { chanStart := make(chan runtimeInputs) compileCache, _ := wazero.NewCompilationCacheWithDir(filepath.Join(manager.conf.PathCache(), "wazero")) runtimeConfig := wazero.NewRuntimeConfig().WithCompilationCache(compileCache).WithCoreFeatures(api.CoreFeaturesV2 | experimental.CoreFeaturesThreads) r, err := newRuntime(context.Background(), manager, log, runtimeConfig) if err != nil { log.Error("can't create runtime", err) } go func() { r.listen(chanStart) }() return &runtimes{ runtime: r, log: log, start: chanStart, } } func (r *runtimes) Compile(ctx context.Context, binary []byte) (wazero.CompiledModule, error) { r.lockRuntime.Lock() defer r.lockRuntime.Unlock() return r.runtime.wazRun.CompileModule(ctx, binary) } func (r *runtimes) Start(ctx context.Context, repoName string, plugins []Plugin, kind runtimeInputsKind, commands []CommandForDiff) { r.lockRuntime.Lock() r.start <- runtimeInputs{ ctx: ctx, repoName: repoName, kind: kind, plugins: plugins, commands: commands, close: func() { r.lockRuntime.Unlock() }, } } func (r *runtimes) Conf(ctx context.Context, plugin Plugin) (*pluginLib.ConfPlugin, error) { timerStop := r.log.Time(fmt.Sprintf("Timer conf plugin %s", plugin.Name)) defer timerStop() r.lockRuntime.Lock() defer r.lockRuntime.Unlock() return r.runtime.conf(ctx, plugin) }