2023-04-13 01:38:33 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.defalsify.org/festive/persist"
|
|
|
|
"git.defalsify.org/festive/resource"
|
|
|
|
)
|
|
|
|
|
2023-04-13 10:38:35 +02:00
|
|
|
// RunPersisted performs a single vm execution from client input using a persisted state.
|
|
|
|
//
|
|
|
|
// State is first loaded from storage. The vm is initialized with the state and executed. The new state is then saved to storage.
|
|
|
|
//
|
|
|
|
// The resulting output of the execution will be written to the provided writer.
|
|
|
|
//
|
|
|
|
// The state is identified by the SessionId member of the Config. Before first execution, the caller must ensure that an
|
|
|
|
// initialized state actually is available for the identifier, otherwise the method will fail.
|
|
|
|
//
|
|
|
|
// It will also fail if execution by the underlying Engine fails.
|
2023-04-13 01:38:33 +02:00
|
|
|
func RunPersisted(cfg Config, rs resource.Resource, pr persist.Persister, input []byte, w io.Writer, ctx context.Context) error {
|
|
|
|
err := pr.Load(cfg.SessionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
st := pr.GetState()
|
2023-04-13 11:33:24 +02:00
|
|
|
location, idx := st.Where()
|
|
|
|
if location != "" {
|
|
|
|
cfg.Root = location
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("run persisted with state %v %x input %s", st, st.Code, input)
|
2023-04-13 01:38:33 +02:00
|
|
|
en := NewEngine(cfg, pr.GetState(), rs, pr.GetMemory(), ctx)
|
|
|
|
|
2023-04-13 11:33:24 +02:00
|
|
|
log.Printf("location %s", location)
|
|
|
|
|
|
|
|
// if len(input) == 0 {
|
|
|
|
// log.Printf("init")
|
|
|
|
// err = en.Init(location, ctx)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
c, err := en.WriteResult(w, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = pr.Save(cfg.SessionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("engine init write %v flags %v", c, st.Flags)
|
|
|
|
if c > 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = idx
|
|
|
|
|
|
|
|
_, err = en.Exec(input, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = en.WriteResult(w, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-04-13 01:38:33 +02:00
|
|
|
}
|
2023-04-13 11:33:24 +02:00
|
|
|
return pr.Save(cfg.SessionId)
|
2023-04-13 01:38:33 +02:00
|
|
|
}
|