wasi-module-sdk-go/context.go

69 lines
1.3 KiB
Go

package sdk
import (
"context"
"io"
"os"
"golang.org/x/exp/slog"
)
var _ ExecutionContext = (*wasiExecutionContext)(nil)
func newWasiExecutionContext(
ctx context.Context,
logger *slog.Logger,
mod Module,
repoRoot, binDir, outDir string,
) *wasiExecutionContext {
return &wasiExecutionContext{
Context: ctx,
logger: logger,
mod: mod,
repoRoot: repoRoot,
outDir: outDir,
binDir: binDir,
}
}
type wasiExecutionContext struct {
context.Context
stateProxy StateProxy
logger *slog.Logger
mod Module
repoRoot string
outDir string
binDir string
}
func (w wasiExecutionContext) WorkingDir() string {
return w.repoRoot
}
func (w wasiExecutionContext) OutDir() string {
return w.outDir
}
func (w wasiExecutionContext) BinariesDir() string {
return w.binDir
}
func (w wasiExecutionContext) StdOut() io.Writer {
return os.Stdout
}
func (w wasiExecutionContext) StdErr() io.Writer {
return os.Stderr
}
func (w wasiExecutionContext) Logger() *slog.Logger {
return w.logger
}
func (w wasiExecutionContext) GetState(_ context.Context, key string) ([]byte, StateMetadata, error) {
return w.stateProxy.Get([]byte(key))
}
func (w wasiExecutionContext) SetState(_ context.Context, key string, value []byte) error {
return w.stateProxy.Set([]byte(key), value)
}