namespace: change -> git.grassecon.net/kamikazechaser/vise

This commit is contained in:
Mohamed Sohail 2023-04-17 12:56:56 +00:00
parent 194522fd95
commit d8ef336919
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
31 changed files with 274 additions and 288 deletions

View File

@ -156,7 +156,7 @@ Multipage outputs, like listings, are handled using the _sink_ output constraint
### Languages support
**Not yet implemeennted**
**Not yet implemented**
Language for rendering is determined at the top-level state.

View File

@ -13,10 +13,9 @@ import (
"github.com/alecthomas/participle/v2"
"github.com/alecthomas/participle/v2/lexer"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
// Asm assembles bytecode from the vise assembly mini-language.
type Asm struct {
Instructions []*Instruction `@@*`
@ -24,11 +23,11 @@ type Asm struct {
// Arg holds all parsed argument elements of a single line of assembly code.
type Arg struct {
Sym *string `(@Sym Whitespace?)?`
Size *uint32 `(@Size Whitespace?)?`
Flag *uint8 `(@Size Whitespace?)?`
Sym *string `(@Sym Whitespace?)?`
Size *uint32 `(@Size Whitespace?)?`
Flag *uint8 `(@Size Whitespace?)?`
Selector *string `(@Sym Whitespace?)?`
Desc *string `(Quote ((@Sym | @Size) @Whitespace?)+ Quote Whitespace?)?`
Desc *string `(Quote ((@Sym | @Size) @Whitespace?)+ Quote Whitespace?)?`
}
func flush(b *bytes.Buffer, w io.Writer) (int, error) {
@ -163,12 +162,12 @@ func parseOne(op vm.Opcode, instruction *Instruction, w io.Writer) (int, error)
a := instruction.OpArg
var n_buf int
var n_out int
b := bytes.NewBuffer(nil)
n, err := writeOpcode(b, op)
n_buf += n
if err != nil {
if err != nil {
return n_out, err
}
@ -245,8 +244,8 @@ func (a Arg) String() string {
// Instruction represents one full line of assembly code.
type Instruction struct {
OpCode string `@Ident`
OpArg Arg `(Whitespace @@)?`
OpCode string `@Ident`
OpArg Arg `(Whitespace @@)?`
Comment string `Comment? EOL`
}
@ -273,7 +272,7 @@ var (
func numSize(n uint32) int {
v := math.Log2(float64(n))
return int((v / 8) + 1)
return int((v / 8) + 1)
}
func writeOpcode(w *bytes.Buffer, op vm.Opcode) (int, error) {
@ -312,14 +311,14 @@ func writeSize(w *bytes.Buffer, n uint32) (int, error) {
}
w.Write([]byte{byte(sz)})
binary.BigEndian.PutUint32(bn[:], n)
c := 4-sz
c := 4 - sz
return w.Write(bn[c:])
}
// Batcher handles assembly commands that generates multiple instructions, such as menu navigation commands.
type Batcher struct {
menuProcessor MenuProcessor
inMenu bool
inMenu bool
}
// NewBatcher creates a new Batcher objcet.
@ -330,7 +329,7 @@ func NewBatcher(mp MenuProcessor) Batcher {
}
// MenuExit generates the instructions for the batch and writes them to the given io.Writer.
func(bt *Batcher) MenuExit(w io.Writer) (int, error) {
func (bt *Batcher) MenuExit(w io.Writer) (int, error) {
if !bt.inMenu {
return 0, nil
}
@ -340,7 +339,7 @@ func(bt *Batcher) MenuExit(w io.Writer) (int, error) {
}
// MenuAdd adds a new menu instruction to the batcher.
func(bt *Batcher) MenuAdd(w io.Writer, code string, arg Arg) (int, error) {
func (bt *Batcher) MenuAdd(w io.Writer, code string, arg Arg) (int, error) {
bt.inMenu = true
var selector string
var sym string
@ -361,7 +360,7 @@ func(bt *Batcher) MenuAdd(w io.Writer, code string, arg Arg) (int, error) {
}
// Exit is a synonym for MenuExit
func(bt *Batcher) Exit(w io.Writer) (int, error) {
func (bt *Batcher) Exit(w io.Writer) (int, error) {
return bt.MenuExit(w)
}
@ -373,9 +372,7 @@ func Parse(s string, w io.Writer) (int, error) {
return 0, err
}
batch := Batcher{
}
batch := Batcher{}
var rn int
for _, v := range ast.Instructions {

View File

@ -7,10 +7,9 @@ import (
"log"
"testing"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
func TestParserInit(t *testing.T) {
var b []byte
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
@ -236,7 +235,7 @@ func TestParserWriteMultiple(t *testing.T) {
if n != n_expect {
t.Fatalf("expected total %v bytes output, got %v", n_expect, n)
}
rb := r.Bytes()
if !bytes.Equal(rb, r_expect) {
t.Fatalf("expected result:\n\t%v, got:\n\t%x", r_expect_hex, rb)

View File

@ -3,42 +3,42 @@ package asm
import (
"fmt"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
// BatchCode defines quasi-opcodes that expand to mulitple individual vm instructions.
type BatchCode uint16
const (
_MENU_OFFSET = 256
MENU_DOWN = _MENU_OFFSET
MENU_UP = _MENU_OFFSET + 1
MENU_NEXT = _MENU_OFFSET + 2
_MENU_OFFSET = 256
MENU_DOWN = _MENU_OFFSET
MENU_UP = _MENU_OFFSET + 1
MENU_NEXT = _MENU_OFFSET + 2
MENU_PREVIOUS = _MENU_OFFSET + 3
)
var (
batchCode = map[string]BatchCode{
"DOWN": MENU_DOWN,
"UP": MENU_UP,
"NEXT": MENU_NEXT,
"DOWN": MENU_DOWN,
"UP": MENU_UP,
"NEXT": MENU_NEXT,
"PREVIOUS": MENU_PREVIOUS,
}
)
type menuItem struct {
code BatchCode
choice string
code BatchCode
choice string
display string
target string
target string
}
// MenuProcessor handles code lines with BatchCode quasi-opcodes that control menu generation.
//
//
// It creates vm instructions for display of menu and handling of input on either size of a vm.HALT instruction.
type MenuProcessor struct {
items []menuItem
size uint32
size uint32
}
// NewMenuProcessor creates a new MenuProcessor object.
@ -49,7 +49,7 @@ func NewMenuProcessor() MenuProcessor {
// Add a menu batch instruction to be processed.
//
// Instructions will be rendered in the order in which they have been added.
func(mp *MenuProcessor) Add(bop string, choice string, display string, target string) error {
func (mp *MenuProcessor) Add(bop string, choice string, display string, target string) error {
bopCode := batchCode[bop]
if bopCode == 0 {
return fmt.Errorf("unknown menu instruction: %v", bop)
@ -58,10 +58,10 @@ func(mp *MenuProcessor) Add(bop string, choice string, display string, target st
return fmt.Errorf("target is only valid for DOWN")
}
m := menuItem{
code: bopCode,
choice: choice,
code: bopCode,
choice: choice,
display: display,
target: target,
target: target,
}
mp.items = append(mp.items, m)
return nil

View File

@ -3,10 +3,9 @@ package asm
import (
"testing"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
func TestMenuInterpreter(t *testing.T) {
m := NewMenuProcessor()
err := m.Add("DOWN", "0", "inky", "foo")

View File

@ -6,11 +6,11 @@ import (
"log"
"os"
"git.defalsify.org/vise/asm"
"git.grassecon.net/kamikazechaser/vise/asm"
)
func main() {
if (len(os.Args) < 2) {
if len(os.Args) < 2 {
os.Exit(1)
}
fp := os.Args[1]

View File

@ -2,14 +2,14 @@ package main
import (
"fmt"
"os"
"io/ioutil"
"os"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
func main() {
if (len(os.Args) < 2) {
if len(os.Args) < 2 {
os.Exit(1)
}
fp := os.Args[1]

View File

@ -4,7 +4,7 @@ import (
"fmt"
"os"
"git.defalsify.org/vise/testdata"
"git.grassecon.net/kamikazechaser/vise/testdata"
)
func main() {

View File

@ -6,7 +6,7 @@ import (
"fmt"
"os"
"git.defalsify.org/vise/engine"
"git.grassecon.net/kamikazechaser/vise/engine"
)
func main() {

View File

@ -3,9 +3,9 @@ package engine
import (
"context"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
// NewDefaultEngine is a convenience function to instantiate a filesystem-backed engine with no output constraints.
@ -27,7 +27,7 @@ func NewSizedEngine(dir string, size uint32) Engine {
ca := cache.NewCache()
cfg := Config{
OutputSize: size,
Root: "root",
Root: "root",
}
ctx := context.TODO()
return NewEngine(cfg, &st, &rs, ca, ctx)

View File

@ -6,29 +6,29 @@ import (
"io"
"log"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/render"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/render"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
"git.grassecon.net/kamikazechaser/vise/vm"
)
// Config globally defines behavior of all components driven by the engine.
type Config struct {
OutputSize uint32 // Maximum size of output from a single rendered page
SessionId string
Root string
FlagCount uint32
CacheSize uint32
SessionId string
Root string
FlagCount uint32
CacheSize uint32
}
// Engine is an execution engine that handles top-level errors when running client inputs against code in the bytecode buffer.
type Engine struct {
st *state.State
rs resource.Resource
ca cache.Memory
vm *vm.Vm
root string
st *state.State
rs resource.Resource
ca cache.Memory
vm *vm.Vm
root string
initd bool
}
@ -45,7 +45,7 @@ func NewEngine(cfg Config, st *state.State, rs resource.Resource, ca cache.Memor
ca: ca,
vm: vm.NewVm(st, rs, ca, szr),
}
engine.root = cfg.Root
engine.root = cfg.Root
return engine
}
@ -53,7 +53,7 @@ func NewEngine(cfg Config, st *state.State, rs resource.Resource, ca cache.Memor
// Init must be explicitly called before using the Engine instance.
//
// It loads and executes code for the start node.
func(en *Engine) Init(ctx context.Context) (bool, error) {
func (en *Engine) Init(ctx context.Context) (bool, error) {
if en.initd {
log.Printf("already initialized")
return true, nil
@ -73,7 +73,7 @@ func(en *Engine) Init(ctx context.Context) (bool, error) {
if err != nil {
return false, err
}
log.Printf("ended init VM run with code %x", b)
en.st.SetCode(b)
err = en.st.SetInput(inSave)
@ -86,7 +86,7 @@ func(en *Engine) Init(ctx context.Context) (bool, error) {
// 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.
//
//
// A bool return valus of false indicates that execution should be terminated. Calling Exec again has undefined effects.
//
// Fails if:
@ -154,7 +154,7 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
// - 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.
func(en *Engine) WriteResult(w io.Writer, ctx context.Context) (int, error) {
func (en *Engine) WriteResult(w io.Writer, ctx context.Context) (int, error) {
r, err := en.vm.Render(ctx)
if err != nil {
return 0, err
@ -163,7 +163,7 @@ func(en *Engine) WriteResult(w io.Writer, ctx context.Context) (int, error) {
}
// start execution over at top node while keeping current state of client error flags.
func(en *Engine) reset(ctx context.Context) (bool, error) {
func (en *Engine) reset(ctx context.Context) (bool, error) {
var err error
var isTop bool
for !isTop {

View File

@ -8,15 +8,15 @@ import (
"path"
"testing"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.defalsify.org/vise/testdata"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
"git.grassecon.net/kamikazechaser/vise/testdata"
)
var (
dataGenerated bool = false
dataDir string = testdata.DataDir
dataGenerated bool = false
dataDir string = testdata.DataDir
)
type FsWrapper struct {
@ -26,8 +26,8 @@ type FsWrapper struct {
func NewFsWrapper(path string, st *state.State) FsWrapper {
rs := resource.NewFsResource(path)
wr := FsWrapper {
&rs,
wr := FsWrapper{
&rs,
st,
}
wr.AddLocalFunc("one", wr.one)
@ -36,26 +36,26 @@ func NewFsWrapper(path string, st *state.State) FsWrapper {
return wr
}
func(fs FsWrapper) one(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (fs FsWrapper) one(sym string, input []byte, ctx context.Context) (resource.Result, error) {
return resource.Result{
Content: "one",
}, nil
}
func(fs FsWrapper) inky(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (fs FsWrapper) inky(sym string, input []byte, ctx context.Context) (resource.Result, error) {
return resource.Result{
Content: "tinkywinky",
}, nil
}
func(fs FsWrapper) pinky(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (fs FsWrapper) pinky(sym string, input []byte, ctx context.Context) (resource.Result, error) {
r := fmt.Sprintf("xyzzy: %x", input)
return resource.Result{
Content: r,
}, nil
}
func(fs FsWrapper) GetCode(sym string) ([]byte, error) {
func (fs FsWrapper) GetCode(sym string) ([]byte, error) {
sym += ".bin"
fp := path.Join(fs.Path, sym)
r, err := ioutil.ReadFile(fp)
@ -80,7 +80,7 @@ func TestEngineInit(t *testing.T) {
st := state.NewState(17)
rs := NewFsWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(Config{
Root: "root",
}, &st, &rs, ca, ctx)
@ -137,7 +137,6 @@ func TestEngineExecInvalidInput(t *testing.T) {
rs := NewFsWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(Config{
Root: "root",
}, &st, &rs, ca, ctx)

View File

@ -8,9 +8,9 @@ import (
"strings"
"testing"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
func TestLoopTop(t *testing.T) {
@ -34,7 +34,7 @@ func TestLoopTop(t *testing.T) {
"2",
"j",
"1",
}
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)
@ -56,7 +56,7 @@ func TestLoopBackForth(t *testing.T) {
st := state.NewState(0)
rs := resource.NewFsResource(dataDir)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
@ -72,7 +72,7 @@ func TestLoopBackForth(t *testing.T) {
"0",
"1",
"0",
}
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)
@ -93,7 +93,7 @@ func TestLoopBrowse(t *testing.T) {
cfg := Config{
OutputSize: 68,
Root: "root",
Root: "root",
}
en := NewEngine(cfg, &st, &rs, ca, ctx)
var err error
@ -108,7 +108,7 @@ func TestLoopBrowse(t *testing.T) {
"00",
"11",
"00",
}
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)

View File

@ -4,8 +4,8 @@ import (
"context"
"io"
"git.defalsify.org/vise/persist"
"git.defalsify.org/vise/resource"
"git.grassecon.net/kamikazechaser/vise/persist"
"git.grassecon.net/kamikazechaser/vise/resource"
)
// RunPersisted performs a single vm execution from client input using a persisted state.

View File

@ -6,17 +6,17 @@ import (
"os"
"testing"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/persist"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/persist"
"git.grassecon.net/kamikazechaser/vise/state"
)
func TestPersist(t *testing.T) {
generateTestData(t)
cfg := Config{
OutputSize: 83,
SessionId: "xyzzy",
Root: "root",
SessionId: "xyzzy",
Root: "root",
}
rs := NewFsWrapper(dataDir, nil)
@ -40,14 +40,14 @@ func TestPersist(t *testing.T) {
if err != nil {
t.Fatal(err)
}
pr = persist.NewFsPersister(persistDir)
inputs := []string{
"", // trigger init, will not exec
"1",
"2",
"00",
}
}
for _, v := range inputs {
err = RunPersisted(cfg, rs, pr, []byte(v), w, ctx)
if err != nil {

View File

@ -11,27 +11,27 @@ import (
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/engine"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/engine"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
const (
USERFLAG_IDENTIFIED = iota + state.FLAG_USERSTART
USERFLAG_HAVENAME
USERFLAG_HAVENAME
USERFLAG_HAVEEMAIL
)
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "profile")
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "profile")
emptyResult = resource.Result{}
)
type profileResource struct {
*resource.FsResource
st *state.State
st *state.State
haveEntered bool
}
@ -43,7 +43,7 @@ func newProfileResource(st *state.State, rs *resource.FsResource) *profileResour
}
}
func(pr *profileResource) checkEntry() error {
func (pr *profileResource) checkEntry() error {
log.Printf("%v %v", USERFLAG_IDENTIFIED, USERFLAG_HAVENAME)
if pr.haveEntered {
return nil
@ -66,7 +66,7 @@ func(pr *profileResource) checkEntry() error {
return nil
}
func(pr profileResource) nameSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (pr profileResource) nameSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
log.Printf("writing name to file")
fp := path.Join(scriptDir, "myname.txt")
err := ioutil.WriteFile(fp, input, 0600)
@ -83,7 +83,7 @@ func(pr profileResource) nameSave(sym string, input []byte, ctx context.Context)
return emptyResult, err
}
func(pr profileResource) emailSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (pr profileResource) emailSave(sym string, input []byte, ctx context.Context) (resource.Result, error) {
log.Printf("writing email to file")
fp := path.Join(scriptDir, "myemail.txt")
err := ioutil.WriteFile(fp, input, 0600)
@ -118,8 +118,8 @@ func main() {
rs.AddLocalFunc("do_email_save", rs.emailSave)
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
Root: "root",
SessionId: sessionId,
OutputSize: uint32(size),
}
ctx := context.Background()

View File

@ -11,15 +11,15 @@ import (
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/engine"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/engine"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "session")
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "session")
emptyResult = resource.Result{}
)
@ -46,7 +46,7 @@ func save(sym string, input []byte, ctx context.Context) (resource.Result, error
}
}
return resource.Result{
Content: string(r),
Content: string(r),
}, nil
}
@ -65,8 +65,8 @@ func main() {
rs.AddLocalFunc("do_save", save)
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
Root: "root",
SessionId: sessionId,
OutputSize: uint32(size),
}
ctx := context.Background()

View File

@ -9,15 +9,15 @@ import (
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/engine"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/engine"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
var (
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "validate")
baseDir = testdataloader.GetBasePath()
scriptDir = path.Join(baseDir, "examples", "validate")
emptyResult = resource.Result{}
)
@ -30,7 +30,7 @@ type verifyResource struct {
st *state.State
}
func(vr *verifyResource) verify(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (vr *verifyResource) verify(sym string, input []byte, ctx context.Context) (resource.Result, error) {
var err error
if string(input) == "something" {
_, err = vr.st.SetFlag(USERFLAG_HAVESOMETHING)
@ -40,7 +40,7 @@ func(vr *verifyResource) verify(sym string, input []byte, ctx context.Context) (
}, err
}
func(vr *verifyResource) again(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (vr *verifyResource) again(sym string, input []byte, ctx context.Context) (resource.Result, error) {
var err error
_, err = vr.st.ResetFlag(USERFLAG_HAVESOMETHING)
return resource.Result{}, err
@ -63,8 +63,8 @@ func main() {
rs.AddLocalFunc("again", rs.again)
ca := cache.NewCache()
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
Root: "root",
SessionId: sessionId,
OutputSize: uint32(size),
}
ctx := context.Background()

2
go.mod
View File

@ -1,4 +1,4 @@
module git.defalsify.org/vise
module git.grassecon.net/kamikazechaser/vise
go 1.20

View File

@ -5,17 +5,18 @@ import (
"log"
"path"
"path/filepath"
"github.com/fxamacker/cbor/v2"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/state"
)
// FsPersister is an implementation of Persister that saves state to the file system.
type FsPersister struct {
State *state.State
State *state.State
Memory *cache.Cache
dir string
dir string
}
// NewFsPersister creates a new FsPersister.
@ -34,35 +35,35 @@ func NewFsPersister(dir string) *FsPersister {
// WithContent sets a current State and Cache object.
//
// This method is normally called before Serialize / Save.
func(p *FsPersister) WithContent(st *state.State, ca *cache.Cache) *FsPersister {
func (p *FsPersister) WithContent(st *state.State, ca *cache.Cache) *FsPersister {
p.State = st
p.Memory = ca
return p
}
// GetState implements the Persister interface.
func(p *FsPersister) GetState() *state.State {
func (p *FsPersister) GetState() *state.State {
return p.State
}
// GetState implements the Persister interface.
func(p *FsPersister) GetMemory() cache.Memory {
func (p *FsPersister) GetMemory() cache.Memory {
return p.Memory
}
// GetState implements the Persister interface.
func(p *FsPersister) Serialize() ([]byte, error) {
func (p *FsPersister) Serialize() ([]byte, error) {
return cbor.Marshal(p)
}
// GetState implements the Persister interface.
func(p *FsPersister) Deserialize(b []byte) error {
func (p *FsPersister) Deserialize(b []byte) error {
err := cbor.Unmarshal(b, p)
return err
}
// GetState implements the Persister interface.
func(p *FsPersister) Save(key string) error {
func (p *FsPersister) Save(key string) error {
b, err := p.Serialize()
if err != nil {
return err
@ -73,7 +74,7 @@ func(p *FsPersister) Save(key string) error {
}
// GetState implements the Persister interface.
func(p *FsPersister) Load(key string) error {
func (p *FsPersister) Load(key string) error {
fp := path.Join(p.dir, key)
b, err := ioutil.ReadFile(fp)
if err != nil {

View File

@ -7,9 +7,9 @@ import (
"reflect"
"testing"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/state"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/state"
"git.grassecon.net/kamikazechaser/vise/vm"
)
func TestSerializeState(t *testing.T) {
@ -39,7 +39,7 @@ func TestSerializeState(t *testing.T) {
err = prnew.Deserialize(v)
if err != nil {
t.Fatal(err)
}
}
if !reflect.DeepEqual(prnew.State.ExecPath, pr.State.ExecPath) {
t.Fatalf("expected %s, got %s", prnew.State.ExecPath, pr.State.ExecPath)
}

View File

@ -1,17 +1,16 @@
package persist
import (
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/state"
)
// Persister interface defines the methods needed for a component that can store the execution state to a storage location.
type Persister interface {
Serialize() ([]byte, error) // Output serializes representation of the state.
Deserialize(b []byte) error // Restore state from a serialized state.
Save(key string) error // Serialize and commit the state representation to persisted storage.
Load(key string) error // Load the state representation from persisted storage and Deserialize.
GetState() *state.State // Get the currently loaded State object.
GetMemory() cache.Memory // Get the currently loaded Cache object.
Save(key string) error // Serialize and commit the state representation to persisted storage.
Load(key string) error // Load the state representation from persisted storage and Deserialize.
GetState() *state.State // Get the currently loaded State object.
GetMemory() cache.Memory // Get the currently loaded Cache object.
}

View File

@ -7,31 +7,31 @@ import (
"strings"
"text/template"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/resource"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/resource"
)
// Page exectues output rendering into pages constrained by size.
type Page struct {
cacheMap map[string]string // Mapped content symbols
cache cache.Memory // Content store.
cache cache.Memory // Content store.
resource resource.Resource // Symbol resolver.
menu *Menu // Menu rendererer.
sink *string // Content symbol rendered by dynamic size.
sizer *Sizer // Process size constraints.
menu *Menu // Menu rendererer.
sink *string // Content symbol rendered by dynamic size.
sizer *Sizer // Process size constraints.
}
// NewPage creates a new Page object.
func NewPage(cache cache.Memory, rs resource.Resource) *Page {
return &Page{
cache: cache,
cache: cache,
cacheMap: make(map[string]string),
resource: rs,
}
}
// WithMenu sets a menu renderer for the page.
func(pg *Page) WithMenu(menu *Menu) *Page {
func (pg *Page) WithMenu(menu *Menu) *Page {
pg.menu = menu
if pg.sizer != nil {
pg.sizer = pg.sizer.WithMenuSize(pg.menu.ReservedSize())
@ -40,7 +40,7 @@ func(pg *Page) WithMenu(menu *Menu) *Page {
}
// WithSizer sets a size constraints definition for the page.
func(pg *Page) WithSizer(sizer *Sizer) *Page {
func (pg *Page) WithSizer(sizer *Sizer) *Page {
pg.sizer = sizer
if pg.menu != nil {
pg.sizer = pg.sizer.WithMenuSize(pg.menu.ReservedSize())
@ -49,7 +49,7 @@ func(pg *Page) WithSizer(sizer *Sizer) *Page {
}
// Usage returns size used by values and menu, and remaining size available
func(pg *Page) Usage() (uint32, uint32, error) {
func (pg *Page) Usage() (uint32, uint32, error) {
var l int
var c uint16
for k, v := range pg.cacheMap {
@ -62,7 +62,7 @@ func(pg *Page) Usage() (uint32, uint32, error) {
log.Printf("v %x %v %v %v %v", []byte(v), len(v), l, sz, c)
}
r := uint32(l)
rsv := uint32(c)-r
rsv := uint32(c) - r
if pg.menu != nil {
r += uint32(pg.menu.ReservedSize())
}
@ -74,7 +74,7 @@ func(pg *Page) Usage() (uint32, uint32, error) {
// After this, Val() will return the value for the key, and Size() will include the value size and limitations in its calculations.
//
// Only one symbol with no size limitation may be mapped at the current level.
func(pg *Page) Map(key string) error {
func (pg *Page) Map(key string) error {
v, err := pg.cache.Get(key)
if err != nil {
return err
@ -103,7 +103,7 @@ func(pg *Page) Map(key string) error {
// Val gets the mapped content for the given symbol.
//
// Fails if key is not mapped.
func(pg *Page) Val(key string) (string, error) {
func (pg *Page) Val(key string) (string, error) {
r := pg.cacheMap[key]
if len(r) == 0 {
return "", fmt.Errorf("key %v not mapped", key)
@ -112,7 +112,7 @@ func(pg *Page) Val(key string) (string, error) {
}
// Sizes returned the actual used bytes by each mapped symbol.
func(pg *Page) Sizes() (map[string]uint16, error) {
func (pg *Page) Sizes() (map[string]uint16, error) {
sizes := make(map[string]uint16)
var haveSink bool
for k, _ := range pg.cacheMap {
@ -131,7 +131,7 @@ func(pg *Page) Sizes() (map[string]uint16, error) {
}
// RenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate.
func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) (string, error) {
func (pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) (string, error) {
tpl, err := pg.resource.GetTemplate(sym)
if err != nil {
return "", err
@ -145,13 +145,12 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16)
return "", fmt.Errorf("sizer needed for indexed render")
}
log.Printf("render for index: %v", idx)
tp, err := template.New("tester").Option("missingkey=error").Parse(tpl)
if err != nil {
return "", err
}
b := bytes.NewBuffer([]byte{})
err = tp.Execute(b, values)
if err != nil {
@ -161,7 +160,7 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16)
}
// Render renders the current mapped content and menu state against the template associated with the symbol.
func(pg *Page) Render(sym string, idx uint16) (string, error) {
func (pg *Page) Render(sym string, idx uint16) (string, error) {
var err error
values, err := pg.prepare(sym, pg.cacheMap, idx)
@ -175,14 +174,14 @@ func(pg *Page) Render(sym string, idx uint16) (string, error) {
// Reset prepared the Page object for re-use.
//
// It clears mappings and removes the sink definition.
func(pg *Page) Reset() {
func (pg *Page) Reset() {
pg.sink = nil
pg.cacheMap = make(map[string]string)
}
// render menu and all syms except sink, split sink into display chunks
// TODO: Function too long, split up
func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[string]string, error) {
func (pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[string]string, error) {
var sink string
if pg.sizer == nil {
@ -204,7 +203,7 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
}
noSinkValues[k] = v
}
if sink == "" {
log.Printf("no sink found for sym %s", sym)
return values, nil
@ -259,7 +258,7 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
for i, v := range sinkValues {
l += len(v)
log.Printf("processing sinkvalue %v: %s", i, v)
if uint32(l) > netRemaining - 1 {
if uint32(l) > netRemaining-1 {
if tb.Len() == 0 {
return nil, fmt.Errorf("capacity insufficient for sink field %v", i)
}
@ -303,7 +302,7 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
}
// render template, menu (if it exists), and audit size constraint (if it exists).
func(pg *Page) render(sym string, values map[string]string, idx uint16) (string, error) {
func (pg *Page) render(sym string, values map[string]string, idx uint16) (string, error) {
var ok bool
r := ""
s, err := pg.RenderTemplate(sym, values, idx)
@ -332,4 +331,3 @@ func(pg *Page) render(sym string, values map[string]string, idx uint16) (string,
}
return r, nil
}

View File

@ -3,10 +3,9 @@ package render
import (
"testing"
"git.defalsify.org/vise/cache"
"git.grassecon.net/kamikazechaser/vise/cache"
)
func TestPageCurrentSize(t *testing.T) {
t.Skip("usage is not in use, and it is unclear how it should be calculated")
ca := cache.NewCache()

View File

@ -5,9 +5,9 @@ import (
"fmt"
"testing"
"git.defalsify.org/vise/state"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/cache"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
type TestSizeResource struct {
@ -141,7 +141,7 @@ func TestSizePages(t *testing.T) {
mn := NewMenu().WithOutputSize(32)
mrs := resource.NewMenuResource().WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
rs := TestSizeResource{
mrs,
mrs,
}
szr := NewSizer(128)
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
@ -159,7 +159,7 @@ func TestSizePages(t *testing.T) {
mn.Put("1", "foo the foo")
mn.Put("2", "go to bar")
r, err := pg.Render("pages", 0)
r, err := pg.Render("pages", 0)
if err != nil {
t.Fatal(err)
}
@ -172,7 +172,6 @@ lala poo
1:foo the foo
2:go to bar`
if r != expect {
t.Fatalf("expected:\n\t%x\ngot:\n\t%x\n", expect, r)
}
@ -200,7 +199,7 @@ func TestManySizes(t *testing.T) {
mn := NewMenu().WithOutputSize(32)
mrs := resource.NewMenuResource().WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
rs := TestSizeResource{
mrs,
mrs,
}
szr := NewSizer(uint32(i))
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
@ -228,7 +227,7 @@ func TestManySizesMenued(t *testing.T) {
mn := NewMenu().WithOutputSize(32)
mrs := resource.NewMenuResource().WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
rs := TestSizeResource{
mrs,
mrs,
}
szr := NewSizer(uint32(i))
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)

View File

@ -1,7 +1,7 @@
package resource
import (
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/state"
)
type StateResource struct {
@ -14,13 +14,13 @@ func ToStateResource(rs Resource) *StateResource {
}
func NewStateResource(st *state.State) *StateResource {
return &StateResource {
return &StateResource{
NewMenuResource(),
st,
}
}
func(sr *StateResource) WithState(st *state.State) *StateResource {
func (sr *StateResource) WithState(st *state.State) *StateResource {
if sr.st != nil {
panic("state already set")
}

View File

@ -3,7 +3,7 @@ package resource
import (
"testing"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/state"
)
func TestStateResourceInit(t *testing.T) {

View File

@ -8,7 +8,7 @@ import (
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/vise/vm"
"git.grassecon.net/kamikazechaser/vise/vm"
)
type genFunc func() error
@ -16,7 +16,7 @@ type genFunc func() error
var (
BaseDir = testdataloader.GetBasePath()
DataDir = ""
dirLock = false
dirLock = false
)
func out(sym string, b []byte, tpl string, data map[string]string) error {
@ -177,7 +177,6 @@ func Generate() (string, error) {
return dir, err
}
// Generate outputs bytecode, templates and content symbols to a specified directory.
//
// The directory must exist, and must not have been used already in the same code execution.

View File

@ -5,18 +5,17 @@ import (
"fmt"
"regexp"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/state"
)
var (
inputRegexStr = "^[a-zA-Z0-9].*$"
inputRegex = regexp.MustCompile(inputRegexStr)
ctrlRegexStr = "^[><_^.]$"
ctrlRegex = regexp.MustCompile(ctrlRegexStr)
symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$"
symRegex = regexp.MustCompile(symRegexStr)
inputRegex = regexp.MustCompile(inputRegexStr)
ctrlRegexStr = "^[><_^.]$"
ctrlRegex = regexp.MustCompile(ctrlRegexStr)
symRegexStr = "^[a-zA-Z0-9][a-zA-Z0-9_]+$"
symRegex = regexp.MustCompile(symRegexStr)
)
// CheckInput validates the given byte string as client input.
@ -61,7 +60,7 @@ func valid(target []byte) bool {
ok = true
}
}
return ok
return ok
}
// CheckTarget tests whether the navigation state transition is available in the current state.

View File

@ -5,29 +5,29 @@ import (
"fmt"
"log"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/render"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/render"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
// Vm holds sub-components mutated by the vm execution.
type Vm struct {
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.
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.
}
// NewVm creates a new Vm.
func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render.Sizer) *Vm {
vmi := &Vm{
st: st,
rs: rs,
ca: ca,
pg: render.NewPage(ca, rs),
st: st,
rs: rs,
ca: ca,
pg: render.NewPage(ca, rs),
sizer: sizer,
}
vmi.Reset()
@ -36,12 +36,12 @@ func NewVm(st *state.State, rs resource.Resource, ca cache.Memory, sizer *render
}
// Reset re-initializes sub-components for output rendering.
func(vmi *Vm) Reset() {
func (vmi *Vm) Reset() {
vmi.mn = render.NewMenu()
vmi.pg.Reset()
vmi.pg = vmi.pg.WithMenu(vmi.mn)
if vmi.sizer != nil {
vmi.pg = vmi.pg.WithSizer(vmi.sizer)
vmi.pg = vmi.pg.WithSizer(vmi.sizer)
}
}
@ -50,7 +50,7 @@ func(vmi *Vm) Reset() {
// Each step may update the state.
//
// On error, the remaining instructions will be returned. State will not be rolled back.
func(vm *Vm) Run(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) Run(b []byte, ctx context.Context) ([]byte, error) {
running := true
for running {
r, err := vm.st.MatchFlag(state.FLAG_TERMINATE, false)
@ -140,9 +140,9 @@ func(vm *Vm) Run(b []byte, ctx context.Context) ([]byte, error) {
// 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.
func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) {
if len(b) > 0 {
return b, nil
}
@ -167,7 +167,6 @@ func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) {
return b, nil
}
log.Printf("no code remaining but not terminating")
location, _ := vm.st.Where()
if location == "" {
@ -178,14 +177,14 @@ func(vm *Vm) RunDeadCheck(b []byte, ctx context.Context) ([]byte, error) {
}
// RunMap executes the MAP opcode
func(vm *Vm) RunMap(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunMap(b []byte, ctx context.Context) ([]byte, error) {
sym, b, err := ParseMap(b)
err = vm.pg.Map(sym)
return b, err
}
// RunMap executes the CATCH opcode
func(vm *Vm) RunCatch(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunCatch(b []byte, ctx context.Context) ([]byte, error) {
sym, sig, mode, b, err := ParseCatch(b)
if err != nil {
return b, err
@ -220,7 +219,7 @@ func(vm *Vm) RunCatch(b []byte, ctx context.Context) ([]byte, error) {
}
// RunMap executes the CROAK opcode
func(vm *Vm) RunCroak(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunCroak(b []byte, ctx context.Context) ([]byte, error) {
sig, mode, b, err := ParseCroak(b)
if err != nil {
return b, err
@ -240,7 +239,7 @@ func(vm *Vm) RunCroak(b []byte, ctx context.Context) ([]byte, error) {
}
// RunLoad executes the LOAD opcode
func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) {
sym, sz, b, err := ParseLoad(b)
if err != nil {
return b, err
@ -254,7 +253,7 @@ func(vm *Vm) RunLoad(b []byte, ctx context.Context) ([]byte, error) {
}
// RunLoad executes the RELOAD opcode
func(vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) {
sym, b, err := ParseReload(b)
if err != nil {
return b, err
@ -275,7 +274,7 @@ func(vm *Vm) RunReload(b []byte, ctx context.Context) ([]byte, error) {
}
// RunLoad executes the MOVE opcode
func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
sym, b, err := ParseMove(b)
if err != nil {
return b, err
@ -300,7 +299,7 @@ func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
// RunIncmp executes the INCMP opcode
// TODO: create state transition table and simplify flow
func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
sym, target, b, err := ParseInCmp(b)
if err != nil {
return b, err
@ -337,7 +336,7 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
if sym != string(input) {
log.Printf("foo")
return b, nil
}
}
log.Printf("input match for '%s', target '%s'", input, target)
}
_, err = vm.st.SetFlag(state.FLAG_INMATCH)
@ -350,7 +349,7 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
}
newTarget, _, err := applyTarget([]byte(target), vm.st, vm.ca, ctx)
_, ok := err.(*state.IndexError)
if ok {
_, err = vm.st.SetFlag(state.FLAG_READIN)
@ -377,14 +376,14 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
}
// RunHalt executes the HALT opcode
func(vm *Vm) RunHalt(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunHalt(b []byte, ctx context.Context) ([]byte, error) {
var err error
b, err = ParseHalt(b)
if err != nil {
return b, err
}
log.Printf("found HALT, stopping")
_, err = vm.st.SetFlag(state.FLAG_WAIT)
if err != nil {
panic(err)
@ -393,14 +392,14 @@ func(vm *Vm) RunHalt(b []byte, ctx context.Context) ([]byte, error) {
}
// RunMSize executes the MSIZE opcode
func(vm *Vm) RunMSize(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunMSize(b []byte, ctx context.Context) ([]byte, error) {
log.Printf("WARNING MSIZE not yet implemented")
_, _, b, err := ParseMSize(b)
return b, err
}
// RunMOut executes the MOUT opcode
func(vm *Vm) RunMOut(b []byte, ctx context.Context) ([]byte, error) {
func (vm *Vm) RunMOut(b []byte, ctx context.Context) ([]byte, error) {
choice, title, b, err := ParseMOut(b)
if err != nil {
return b, err
@ -410,38 +409,38 @@ func(vm *Vm) RunMOut(b []byte, ctx context.Context) ([]byte, error) {
}
// RunMNext executes the MNEXT opcode
func(vm *Vm) RunMNext(b []byte, ctx context.Context) ([]byte, error) {
selector, display, b, err := ParseMNext(b)
if err != nil {
return b, err
}
cfg := vm.mn.GetBrowseConfig()
cfg.NextSelector = selector
cfg.NextTitle = display
cfg.NextAvailable = true
vm.mn = vm.mn.WithBrowseConfig(cfg)
return b, nil
func (vm *Vm) RunMNext(b []byte, ctx context.Context) ([]byte, error) {
selector, display, b, err := ParseMNext(b)
if err != nil {
return b, err
}
cfg := vm.mn.GetBrowseConfig()
cfg.NextSelector = selector
cfg.NextTitle = display
cfg.NextAvailable = true
vm.mn = vm.mn.WithBrowseConfig(cfg)
return b, nil
}
// RunMPrev executes the MPREV opcode
func(vm *Vm) RunMPrev(b []byte, ctx context.Context) ([]byte, error) {
selector, display, b, err := ParseMPrev(b)
if err != nil {
return b, err
}
cfg := vm.mn.GetBrowseConfig()
cfg.PreviousSelector = selector
cfg.PreviousTitle = display
cfg.PreviousAvailable = true
vm.mn = vm.mn.WithBrowseConfig(cfg)
return b, nil
func (vm *Vm) RunMPrev(b []byte, ctx context.Context) ([]byte, error) {
selector, display, b, err := ParseMPrev(b)
if err != nil {
return b, err
}
cfg := vm.mn.GetBrowseConfig()
cfg.PreviousSelector = selector
cfg.PreviousTitle = display
cfg.PreviousAvailable = true
vm.mn = vm.mn.WithBrowseConfig(cfg)
return b, nil
}
// Render wraps output rendering, and handles error when attempting to browse beyond the rendered page count.
func(vm *Vm) Render(ctx context.Context) (string, error) {
func (vm *Vm) Render(ctx context.Context) (string, error) {
changed, err := vm.st.ResetFlag(state.FLAG_DIRTY)
if err != nil {
panic(err)
panic(err)
}
if !changed {
return "", nil
@ -464,7 +463,7 @@ func(vm *Vm) Render(ctx context.Context) (string, error) {
}
// retrieve data for key
func(vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
func (vm *Vm) refresh(key string, rs resource.Resource, ctx context.Context) (string, error) {
fn, err := rs.FuncFor(key)
if err != nil {
return "", err

View File

@ -6,18 +6,18 @@ import (
"fmt"
"log"
"testing"
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/render"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
"git.grassecon.net/kamikazechaser/vise/cache"
"git.grassecon.net/kamikazechaser/vise/render"
"git.grassecon.net/kamikazechaser/vise/resource"
"git.grassecon.net/kamikazechaser/vise/state"
)
var dynVal = "three"
type TestResource struct {
resource.MenuResource
state *state.State
state *state.State
RootCode []byte
}
@ -58,7 +58,7 @@ func setFlag(sym string, input []byte, ctx context.Context) (resource.Result, er
r.FlagReset = append(r.FlagReset, uint32(input[1]))
}
log.Printf("setflag %v", r)
return r, nil
return r, nil
}
@ -105,14 +105,14 @@ func (r TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
return nil, fmt.Errorf("invalid function: '%s'", sym)
}
func(r TestResource) getInput(sym string, input []byte, ctx context.Context) (resource.Result, error) {
func (r TestResource) getInput(sym string, input []byte, ctx context.Context) (resource.Result, error) {
v, err := r.state.GetInput()
return resource.Result{
Content: string(v),
}, err
}
func(r TestResource) GetCode(sym string) ([]byte, error) {
func (r TestResource) GetCode(sym string) ([]byte, error) {
var b []byte
switch sym {
case "_catch":
@ -139,13 +139,13 @@ func TestRun(t *testing.T) {
b = NewLine(b, HALT, nil, nil, nil)
_, err := vm.Run(b, context.TODO())
if err != nil {
t.Errorf("run error: %v", err)
t.Errorf("run error: %v", err)
}
b = []byte{0x01, 0x02}
_, err = vm.Run(b, context.TODO())
if err == nil {
t.Errorf("no error on invalid opcode")
t.Errorf("no error on invalid opcode")
}
}
@ -289,7 +289,7 @@ func TestRunArg(t *testing.T) {
bi = NewLine(bi, HALT, nil, nil, nil)
b, err := vm.Run(bi, context.TODO())
if err != nil {
t.Error(err)
t.Error(err)
}
l := len(b)
if l != 0 {
@ -320,7 +320,7 @@ func TestRunInputHandler(t *testing.T) {
var err error
_, err = vm.Run(bi, context.TODO())
if err != nil {
t.Fatal(err)
t.Fatal(err)
}
r, _ := st.Where()
if r != "foo" {
@ -337,13 +337,13 @@ func TestRunArgInvalid(t *testing.T) {
_ = st.SetInput([]byte("foo"))
var err error
st.Down("root")
b := NewLine(nil, INCMP, []string{"bar", "baz"}, nil, nil)
b, err = vm.Run(b, context.TODO())
if err != nil {
t.Fatal(err)
t.Fatal(err)
}
r, _ := st.Where()
if r != "_catch" {
@ -368,13 +368,13 @@ func TestRunMenu(t *testing.T) {
b, err = vm.Run(b, ctx)
if err != nil {
t.Error(err)
t.Error(err)
}
l := len(b)
if l != 0 {
t.Errorf("expected empty remainder, got length %v: %v", l, b)
}
r, err := vm.Render(ctx)
if err != nil {
t.Fatal(err)
@ -403,13 +403,13 @@ func TestRunMenuBrowse(t *testing.T) {
b, err = vm.Run(b, ctx)
if err != nil {
t.Error(err)
t.Error(err)
}
l := len(b)
if l != 0 {
t.Errorf("expected empty remainder, got length %v: %v", l, b)
}
r, err := vm.Render(ctx)
if err != nil {
t.Fatal(err)
@ -456,7 +456,6 @@ func TestRunReturn(t *testing.T) {
}
}
func TestRunLoadInput(t *testing.T) {
st := state.NewState(5)
rs := TestResource{}