2023-04-01 11:58:02 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-01 15:47:03 +02:00
|
|
|
"fmt"
|
2023-04-01 11:58:02 +02:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.defalsify.org/festive/resource"
|
|
|
|
"git.defalsify.org/festive/state"
|
|
|
|
"git.defalsify.org/festive/vm"
|
|
|
|
)
|
|
|
|
//
|
|
|
|
//type Config struct {
|
|
|
|
// FlagCount uint32
|
|
|
|
// CacheSize uint32
|
|
|
|
//}
|
|
|
|
|
2023-04-01 16:04:38 +02:00
|
|
|
// Engine is an execution engine that handles top-level errors when running user inputs against currently exposed bytecode.
|
2023-04-01 11:58:02 +02:00
|
|
|
type Engine struct {
|
2023-04-01 15:47:03 +02:00
|
|
|
st *state.State
|
2023-04-01 11:58:02 +02:00
|
|
|
rs resource.Resource
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:04:38 +02:00
|
|
|
// NewEngine creates a new Engine
|
2023-04-01 15:47:03 +02:00
|
|
|
func NewEngine(st *state.State, rs resource.Resource) Engine {
|
2023-04-01 11:58:02 +02:00
|
|
|
engine := Engine{st, rs}
|
|
|
|
return engine
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:04:38 +02:00
|
|
|
// Init must be explicitly called before using the Engine instance.
|
|
|
|
//
|
|
|
|
// It makes sure bootstrapping code has been executed, and that the exposed bytecode is ready for user input.
|
2023-04-02 10:07:53 +02:00
|
|
|
func(en *Engine) Init(sym string, ctx context.Context) error {
|
2023-04-06 10:14:53 +02:00
|
|
|
err := en.st.SetInput([]byte{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
b := vm.NewLine(nil, vm.MOVE, []string{sym}, nil, nil)
|
|
|
|
b, err = vm.Run(b, en.st, en.rs, ctx)
|
2023-04-01 15:47:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
en.st.SetCode(b)
|
|
|
|
return nil
|
2023-04-01 15:47:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 16:04:38 +02:00
|
|
|
// Exec processes user input against the current state of the virtual machine environment.
|
|
|
|
//
|
|
|
|
// If successfully executed:
|
|
|
|
// - output of the last execution is available using the WriteResult(...) call
|
|
|
|
// - Exec(...) may be called again with new input
|
|
|
|
//
|
|
|
|
// This implementation is in alpha state. That means that any error emitted may have left the system in an undefined state.
|
|
|
|
//
|
|
|
|
// TODO: Disambiguate errors as critical and resumable errors.
|
|
|
|
//
|
|
|
|
// Fails if:
|
|
|
|
// - input is objectively invalid (too long etc)
|
|
|
|
// - no current bytecode is available
|
|
|
|
// - input processing against bytcode failed
|
2023-04-01 15:47:03 +02:00
|
|
|
func (en *Engine) Exec(input []byte, ctx context.Context) error {
|
2023-04-01 23:19:12 +02:00
|
|
|
err := en.st.SetInput(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-04-01 15:47:03 +02:00
|
|
|
}
|
2023-04-06 10:14:53 +02:00
|
|
|
log.Printf("new execution with input '%s' (0x%x)", input, input)
|
2023-04-01 15:47:03 +02:00
|
|
|
code, err := en.st.GetCode()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(code) == 0 {
|
|
|
|
return fmt.Errorf("no code to execute")
|
|
|
|
}
|
2023-04-01 23:19:12 +02:00
|
|
|
code, err = vm.Run(code, en.st, en.rs, ctx)
|
2023-04-01 15:47:03 +02:00
|
|
|
en.st.SetCode(code)
|
2023-04-01 11:58:02 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-01 16:04:38 +02:00
|
|
|
// WriteResult writes the output of the last vm execution to the given writer.
|
|
|
|
//
|
|
|
|
// Fails if
|
|
|
|
// - required data inputs to the template are not available.
|
|
|
|
// - the template for the given node point is note available for retrieval using the resource.Resource implementer.
|
|
|
|
// - the supplied writer fails to process the writes.
|
2023-04-01 11:58:02 +02:00
|
|
|
func(en *Engine) WriteResult(w io.Writer) error {
|
|
|
|
location := en.st.Where()
|
|
|
|
v, err := en.st.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r, err := en.rs.RenderTemplate(location, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-03 00:53:21 +02:00
|
|
|
m, err := en.rs.RenderMenu()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(m) > 0 {
|
|
|
|
r += "\n" + m
|
|
|
|
}
|
2023-04-01 11:58:02 +02:00
|
|
|
c, err := io.WriteString(w, r)
|
|
|
|
log.Printf("%v bytes written as result for %v", c, location)
|
|
|
|
return err
|
|
|
|
}
|