2023-04-02 13:12:06 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2023-04-14 10:59:37 +02:00
|
|
|
"git.defalsify.org/vise/cache"
|
|
|
|
"git.defalsify.org/vise/render"
|
|
|
|
"git.defalsify.org/vise/resource"
|
|
|
|
"git.defalsify.org/vise/state"
|
2023-04-02 13:12:06 +02:00
|
|
|
)
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Vm holds sub-components mutated by the vm execution.
|
2023-04-09 10:44:32 +02:00
|
|
|
type Vm struct {
|
2023-04-12 19:04:36 +02:00
|
|
|
st *state.State // Navigation and error states.
|
|
|
|
rs resource.Resource // Retrieves content, code, and templates for symbols.
|
|
|
|
ca cache.Memory // Loaded content.
|
|
|
|
mn *render.Menu // Menu component of page.
|
|
|
|
sizer *render.Sizer // Apply size constraints to output.
|
|
|
|
pg *render.Page // Render outputs with menues to size constraints.
|
2023-04-09 10:44:32 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// NewVm creates a new Vm.
|
2023-04-09 16:35:26 +02:00
|
|
|
func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render.Sizer) *Vm {
|
|
|
|
vmi := &Vm{
|
2023-04-09 10:44:32 +02:00
|
|
|
st: st,
|
|
|
|
rs: rs,
|
|
|
|
ca: ca,
|
2023-04-10 16:26:18 +02:00
|
|
|
pg: render.NewPage(ca, rs),
|
2023-04-09 16:35:26 +02:00
|
|
|
sizer: sizer,
|
|
|
|
}
|
|
|
|
vmi.Reset()
|
|
|
|
return vmi
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Reset re-initializes sub-components for output rendering.
|
2023-04-09 16:35:26 +02:00
|
|
|
func(vmi *Vm) Reset() {
|
|
|
|
vmi.mn = render.NewMenu()
|
2023-04-10 16:26:18 +02:00
|
|
|
vmi.pg.Reset()
|
2023-04-12 19:04:36 +02:00
|
|
|
vmi.pg = vmi.pg.WithMenu(vmi.mn)
|
2023-04-09 16:35:26 +02:00
|
|
|
if vmi.sizer != nil {
|
|
|
|
vmi.pg = vmi.pg.WithSizer(vmi.sizer)
|
2023-04-09 10:44:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 13:12:06 +02:00
|
|
|
// Run extracts individual op codes and arguments and executes them.
|
|
|
|
//
|
|
|
|
// Each step may update the state.
|
|
|
|
//
|
|
|
|
// On error, the remaining instructions will be returned. State will not be rolled back.
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) Run(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
running := true
|
|
|
|
for running {
|
2023-04-10 10:05:48 +02:00
|
|
|
r, err := vm.st.MatchFlag(state.FLAG_TERMINATE, false)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("terminate set! bailing!")
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
2023-04-16 09:13:09 +02:00
|
|
|
//vm.st.ResetBaseFlags()
|
|
|
|
_, err = vm.st.ResetFlag(state.FLAG_TERMINATE)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-04-10 10:05:48 +02:00
|
|
|
_, err = vm.st.SetFlag(state.FLAG_DIRTY)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
op, bb, err := opSplit(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
b = bb
|
2023-04-06 10:14:53 +02:00
|
|
|
log.Printf("execute code %x (%s) %x", op, OpcodeString[op], b)
|
2023-04-10 10:05:48 +02:00
|
|
|
log.Printf("state: %v", vm.st)
|
2023-04-02 13:12:06 +02:00
|
|
|
switch op {
|
|
|
|
case CATCH:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunCatch(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case CROAK:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunCroak(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case LOAD:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunLoad(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case RELOAD:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunReload(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case MAP:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMap(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case MOVE:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMove(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case INCMP:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunInCmp(b, ctx)
|
2023-04-03 00:53:21 +02:00
|
|
|
case MSIZE:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMSize(b, ctx)
|
2023-04-03 00:53:21 +02:00
|
|
|
case MOUT:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMOut(b, ctx)
|
2023-04-08 11:01:28 +02:00
|
|
|
case MNEXT:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMNext(b, ctx)
|
2023-04-08 11:01:28 +02:00
|
|
|
case MPREV:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunMPrev(b, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
case HALT:
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunHalt(b, ctx)
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unhandled state: %v", op)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
|
|
|
}
|
2023-04-06 10:54:51 +02:00
|
|
|
if len(b) == 0 {
|
2023-04-09 10:44:32 +02:00
|
|
|
b, err = vm.RunDeadCheck(b, ctx)
|
2023-04-06 10:54:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
if len(b) == 0 {
|
|
|
|
return []byte{}, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:51 +02:00
|
|
|
// RunDeadCheck determines whether a state of empty bytecode should result in termination.
|
|
|
|
//
|
|
|
|
// If there is remaining bytecode, this method is a noop.
|
|
|
|
//
|
|
|
|
// If input has not been matched, a default invalid input page should be generated aswell as a possiblity of return to last screen (or exit).
|
|
|
|
//
|
|
|
|
// If the termination flag has been set but not yet handled, execution is allowed to terminate.
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-06 10:54:51 +02:00
|
|
|
if len(b) > 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
2023-04-10 10:19:18 +02:00
|
|
|
r, err := vm.st.MatchFlag(state.FLAG_READIN, true)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("Not processing input. Setting terminate")
|
|
|
|
_, err := vm.st.SetFlag(state.FLAG_TERMINATE)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
r, err = vm.st.MatchFlag(state.FLAG_TERMINATE, false)
|
2023-04-06 10:54:51 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if r {
|
2023-04-10 10:05:48 +02:00
|
|
|
log.Printf("Terminate found!!")
|
2023-04-06 10:54:51 +02:00
|
|
|
return b, nil
|
|
|
|
}
|
2023-04-10 10:19:18 +02:00
|
|
|
|
|
|
|
|
2023-04-10 10:05:48 +02:00
|
|
|
log.Printf("no code remaining but not terminating")
|
2023-04-09 10:44:32 +02:00
|
|
|
location, _ := vm.st.Where()
|
2023-04-06 10:54:51 +02:00
|
|
|
if location == "" {
|
|
|
|
return b, fmt.Errorf("dead runner with no current location")
|
|
|
|
}
|
|
|
|
b = NewLine(nil, MOVE, []string{"_catch"}, nil, nil)
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 13:12:06 +02:00
|
|
|
// RunMap executes the MAP opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunMap(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, b, err := ParseMap(b)
|
2023-04-09 10:44:32 +02:00
|
|
|
err = vm.pg.Map(sym)
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMap executes the CATCH opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunCatch(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, sig, mode, b, err := ParseCatch(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
r, err := vm.st.MatchFlag(sig, mode)
|
2023-04-02 14:59:40 +02:00
|
|
|
if err != nil {
|
2023-04-16 09:51:16 +02:00
|
|
|
_, err = vm.st.SetFlag(state.FLAG_TERMINATE)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("terminate set")
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("catch at flag %v, moving to %v", sig, sym) //bitField, d)
|
2023-04-13 11:10:54 +02:00
|
|
|
bh, err := vm.rs.GetCode(sym)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-16 10:13:14 +02:00
|
|
|
//b = append(bh, b...)
|
|
|
|
b = bh
|
2023-04-09 10:44:32 +02:00
|
|
|
vm.st.Down(sym)
|
2023-04-13 11:10:54 +02:00
|
|
|
vm.ca.Push()
|
2023-04-10 18:38:23 +02:00
|
|
|
vm.Reset()
|
2023-04-16 09:51:16 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMap executes the CROAK opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunCroak(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sig, mode, b, err := ParseCroak(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
r, err := vm.st.MatchFlag(sig, mode)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
log.Printf("croak at flag %v, purging and moving to top", sig)
|
2023-04-10 18:38:23 +02:00
|
|
|
vm.Reset()
|
2023-04-09 10:44:32 +02:00
|
|
|
vm.st.Reset()
|
|
|
|
vm.pg.Reset()
|
|
|
|
vm.ca.Reset()
|
2023-04-02 14:59:40 +02:00
|
|
|
b = []byte{}
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the LOAD opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, sz, b, err := ParseLoad(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-13 08:56:35 +02:00
|
|
|
r, err := vm.refresh(sym, vm.rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
err = vm.ca.Add(sym, r, uint16(sz))
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the RELOAD opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, b, err := ParseReload(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
|
2023-04-13 08:56:35 +02:00
|
|
|
r, err := vm.refresh(sym, vm.rs, ctx)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
vm.ca.Update(sym, r)
|
|
|
|
if vm.pg != nil {
|
|
|
|
err := vm.pg.Map(sym)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
}
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunLoad executes the MOVE opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, b, err := ParseMove(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-06 11:55:11 +02:00
|
|
|
if sym == "_" {
|
2023-04-09 10:44:32 +02:00
|
|
|
vm.st.Up()
|
|
|
|
vm.ca.Pop()
|
|
|
|
sym, _ = vm.st.Where()
|
2023-04-06 11:55:11 +02:00
|
|
|
} else {
|
2023-04-09 10:44:32 +02:00
|
|
|
vm.st.Down(sym)
|
|
|
|
vm.ca.Push()
|
2023-04-06 11:55:11 +02:00
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
code, err := vm.rs.GetCode(sym)
|
2023-04-03 00:53:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-15 08:06:03 +02:00
|
|
|
log.Printf("loaded code for %s: %x", sym, code)
|
2023-04-03 00:53:21 +02:00
|
|
|
b = append(b, code...)
|
2023-04-10 18:38:23 +02:00
|
|
|
vm.Reset()
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, nil
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunIncmp executes the INCMP opcode
|
2023-04-16 09:13:09 +02:00
|
|
|
// TODO: create state transition table and simplify flow
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
sym, target, b, err := ParseInCmp(b)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-10 10:19:18 +02:00
|
|
|
|
2023-04-16 09:13:09 +02:00
|
|
|
reading, err := vm.st.GetFlag(state.FLAG_READIN)
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-10 10:19:18 +02:00
|
|
|
panic(err)
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-10 10:19:18 +02:00
|
|
|
have, err := vm.st.GetFlag(state.FLAG_INMATCH)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if have {
|
2023-04-16 09:13:09 +02:00
|
|
|
if !reading {
|
2023-04-10 10:19:18 +02:00
|
|
|
_, err = vm.st.ResetFlag(state.FLAG_INMATCH)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-04-13 11:33:24 +02:00
|
|
|
log.Printf("ignoring input %s, already have match", sym)
|
2023-04-10 10:19:18 +02:00
|
|
|
return b, nil
|
|
|
|
}
|
2023-04-16 09:13:09 +02:00
|
|
|
} else {
|
|
|
|
_, err = vm.st.SetFlag(state.FLAG_READIN)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
input, err := vm.st.GetInput()
|
2023-04-02 13:12:06 +02:00
|
|
|
if err != nil {
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-16 09:13:09 +02:00
|
|
|
log.Printf("testing sym %s input %s", sym, input)
|
|
|
|
|
|
|
|
if !have && sym == "*" {
|
2023-04-10 10:05:48 +02:00
|
|
|
log.Printf("input wildcard match ('%s'), target '%s'", input, target)
|
|
|
|
} else {
|
|
|
|
if sym != string(input) {
|
|
|
|
return b, nil
|
2023-04-16 09:13:09 +02:00
|
|
|
}
|
2023-04-10 10:05:48 +02:00
|
|
|
log.Printf("input match for '%s', target '%s'", input, target)
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
2023-04-06 10:14:53 +02:00
|
|
|
|
2023-04-09 10:44:32 +02:00
|
|
|
_, err = vm.st.SetFlag(state.FLAG_INMATCH)
|
2023-04-10 10:05:48 +02:00
|
|
|
if err != nil {
|
2023-04-10 10:19:18 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = vm.st.ResetFlag(state.FLAG_READIN)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2023-04-10 10:05:48 +02:00
|
|
|
}
|
2023-04-08 09:54:55 +02:00
|
|
|
|
2023-04-10 10:05:48 +02:00
|
|
|
target, _, err = applyTarget([]byte(target), vm.st, vm.ca, ctx)
|
2023-04-12 09:42:37 +02:00
|
|
|
_, ok := err.(*state.IndexError)
|
|
|
|
if ok {
|
|
|
|
_, err = vm.st.ResetFlag(state.FLAG_INMATCH)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = vm.st.SetFlag(state.FLAG_READIN)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
} else if err != nil {
|
2023-04-10 08:54:52 +02:00
|
|
|
return b, err
|
|
|
|
}
|
2023-04-10 18:38:23 +02:00
|
|
|
vm.Reset()
|
2023-04-08 09:54:55 +02:00
|
|
|
|
2023-04-09 10:44:32 +02:00
|
|
|
code, err := vm.rs.GetCode(target)
|
2023-04-03 00:53:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-10 08:54:52 +02:00
|
|
|
log.Printf("loaded additional code for target '%s': %x", target, code)
|
2023-04-03 00:53:21 +02:00
|
|
|
b = append(b, code...)
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunHalt executes the HALT opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunHalt(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-02 14:59:40 +02:00
|
|
|
var err error
|
|
|
|
b, err = ParseHalt(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-02 13:12:06 +02:00
|
|
|
log.Printf("found HALT, stopping")
|
2023-04-02 14:59:40 +02:00
|
|
|
return b, err
|
2023-04-02 13:12:06 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 10:44:32 +02:00
|
|
|
// RunMSize executes the MSIZE opcode
|
|
|
|
func(vm *Vm) RunMSize(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-03 10:11:44 +02:00
|
|
|
log.Printf("WARNING MSIZE not yet implemented")
|
2023-04-03 10:52:58 +02:00
|
|
|
_, _, b, err := ParseMSize(b)
|
|
|
|
return b, err
|
2023-04-03 00:53:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 08:42:15 +02:00
|
|
|
// RunMOut executes the MOUT opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunMOut(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-03 00:53:21 +02:00
|
|
|
choice, title, b, err := ParseMOut(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
err = vm.mn.Put(choice, title)
|
2023-04-03 00:53:21 +02:00
|
|
|
return b, err
|
|
|
|
}
|
2023-04-02 13:12:06 +02:00
|
|
|
|
2023-04-08 11:01:28 +02:00
|
|
|
// RunMNext executes the MNEXT opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunMNext(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-08 11:01:28 +02:00
|
|
|
selector, display, b, err := ParseMNext(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
cfg := vm.mn.GetBrowseConfig()
|
|
|
|
cfg.NextSelector = selector
|
|
|
|
cfg.NextTitle = display
|
|
|
|
cfg.NextAvailable = true
|
|
|
|
vm.mn = vm.mn.WithBrowseConfig(cfg)
|
|
|
|
return b, nil
|
2023-04-08 11:01:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunMPrev executes the MPREV opcode
|
2023-04-09 10:44:32 +02:00
|
|
|
func(vm *Vm) RunMPrev(b []byte, ctx context.Context) ([]byte, error) {
|
2023-04-08 11:01:28 +02:00
|
|
|
selector, display, b, err := ParseMPrev(b)
|
|
|
|
if err != nil {
|
|
|
|
return b, err
|
|
|
|
}
|
2023-04-09 10:44:32 +02:00
|
|
|
cfg := vm.mn.GetBrowseConfig()
|
|
|
|
cfg.PreviousSelector = selector
|
|
|
|
cfg.PreviousTitle = display
|
|
|
|
cfg.PreviousAvailable = true
|
|
|
|
vm.mn = vm.mn.WithBrowseConfig(cfg)
|
|
|
|
return b, nil
|
2023-04-08 11:01:28 +02:00
|
|
|
}
|
2023-04-08 10:31:32 +02:00
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Render wraps output rendering, and handles error when attempting to browse beyond the rendered page count.
|
2023-04-12 09:30:35 +02:00
|
|
|
func(vm *Vm) Render(ctx context.Context) (string, error) {
|
2023-04-10 10:05:48 +02:00
|
|
|
changed, err := vm.st.ResetFlag(state.FLAG_DIRTY)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if !changed {
|
2023-04-13 11:33:24 +02:00
|
|
|
return "", nil
|
2023-04-10 10:05:48 +02:00
|
|
|
}
|
2023-04-09 16:35:26 +02:00
|
|
|
sym, idx := vm.st.Where()
|
|
|
|
r, err := vm.pg.Render(sym, idx)
|
2023-04-12 09:30:35 +02:00
|
|
|
var ok bool
|
|
|
|
_, ok = err.(*render.BrowseError)
|
|
|
|
if ok {
|
|
|
|
vm.Reset()
|
|
|
|
b := NewLine(nil, MOVE, []string{"_catch"}, nil, nil)
|
|
|
|
vm.Run(b, ctx)
|
|
|
|
sym, idx := vm.st.Where()
|
|
|
|
r, err = vm.pg.Render(sym, idx)
|
|
|
|
}
|
2023-04-09 16:35:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2023-04-02 13:12:06 +02:00
|
|
|
// retrieve data for key
|
2023-04-13 08:56:35 +02:00
|
|
|
func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
|
2023-04-02 13:12:06 +02:00
|
|
|
fn, err := rs.FuncFor(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if fn == nil {
|
|
|
|
return "", fmt.Errorf("no retrieve function for external symbol %v", key)
|
|
|
|
}
|
2023-04-13 08:56:35 +02:00
|
|
|
input, _ := vm.st.GetInput()
|
2023-04-13 10:16:17 +02:00
|
|
|
r, err := fn(key, input, ctx)
|
|
|
|
if err != nil {
|
|
|
|
var perr error
|
|
|
|
_, perr = vm.st.SetFlag(state.FLAG_LOADFAIL)
|
|
|
|
if perr != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, flag := range r.FlagSet {
|
|
|
|
if !state.IsWriteableFlag(flag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = vm.st.SetFlag(flag)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, flag := range r.FlagReset {
|
|
|
|
if !state.IsWriteableFlag(flag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = vm.st.ResetFlag(flag)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 16:35:26 +02:00
|
|
|
|
2023-04-13 10:16:17 +02:00
|
|
|
return r.Content, err
|
|
|
|
}
|