2023-05-08 13:21:31 +00:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-08-15 20:10:30 +00:00
|
|
|
"log/slog"
|
2023-05-08 13:21:31 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2023-05-24 20:11:05 +00:00
|
|
|
return w.stateProxy.Get([]byte(key))
|
2023-05-08 13:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w wasiExecutionContext) SetState(_ context.Context, key string, value []byte) error {
|
2023-05-24 20:11:05 +00:00
|
|
|
return w.stateProxy.Set([]byte(key), value)
|
2023-05-08 13:21:31 +00:00
|
|
|
}
|