commit e13f8d52f0fcfcef02c6d82e3db5d13f6fa01efc
parent 0917e9132fbbabd5dc54d68e46c36f2c1f49a66e
Author: lash <dev@holbrook.no>
Date: Mon, 2 Sep 2024 04:00:27 +0100
State constructor returns pointer
Diffstat:
14 files changed, 81 insertions(+), 82 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -10,6 +10,7 @@
* Use last cached value on clean exit without remaining code.
* Add missing go.sum file.
* Single constructor for engine with automagical handling for state, cache and persistence.
+ * State constructor returns pointer.
- 0.0.15
* Add comprehensive documentation with texinfo.
* Prevent cursor leak in sizer.
diff --git a/engine/db.go b/engine/db.go
@@ -127,7 +127,7 @@ func(en *DefaultEngine) WithFirst(fn resource.EntryFunc) *DefaultEngine {
func(en *DefaultEngine) ensureState() {
if en.st == nil {
st := state.NewState(en.cfg.FlagCount)
- en.st = &st
+ en.st = st
en.st.SetLanguage(en.cfg.Language)
if en.st.Language != nil {
en.st.SetFlag(state.FLAG_LANG)
diff --git a/engine/engine_test.go b/engine/engine_test.go
@@ -119,14 +119,14 @@ func TestEngineInit(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
@@ -179,14 +179,14 @@ func TestEngineExecInvalidInput(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
var err error
_, err = en.Init(ctx)
@@ -203,14 +203,14 @@ func TestEngineResumeTerminated(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(17)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
var err error
_, err = en.Init(ctx)
@@ -241,14 +241,14 @@ func TestLanguageSet(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
var err error
@@ -296,14 +296,14 @@ func TestLanguageRender(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache()
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
var err error
@@ -338,7 +338,7 @@ func TestConfigLanguageRender(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache()
cfg := Config{
@@ -346,7 +346,7 @@ func TestConfigLanguageRender(t *testing.T) {
Language: "nor",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
var err error
@@ -397,14 +397,14 @@ func TestPreVm(t *testing.T) {
ctx := context.Background()
st := state.NewState(0)
st.UseDebug()
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache()
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
en = en.WithFirst(preBlock)
r, err := en.Init(ctx)
@@ -426,7 +426,7 @@ func TestPreVm(t *testing.T) {
st = state.NewState(0)
ca = cache.NewCache()
en = NewEngine(cfg, &rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
en = en.WithFirst(preAllow)
r, err = en.Init(ctx)
diff --git a/engine/loop_test.go b/engine/loop_test.go
@@ -17,14 +17,14 @@ func TestLoopTop(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
+ rs := newTestWrapper(dataDir, st)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
@@ -57,15 +57,14 @@ func TestLoopBackForth(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
- ca := cache.NewCache().WithCacheSize(1024)
+ rs := newTestWrapper(dataDir, st)
cfg := Config{
Root: "root",
+ CacheSize: 1024,
}
en := NewEngine(cfg, rs)
- en = en.WithState(&st)
- en = en.WithMemory(ca)
+ en = en.WithState(st)
_, err = en.Init(ctx)
if err != nil {
@@ -94,16 +93,15 @@ func TestLoopBrowse(t *testing.T) {
generateTestData(t)
ctx := context.Background()
st := state.NewState(0)
- rs := newTestWrapper(dataDir, &st)
- ca := cache.NewCache().WithCacheSize(1024)
+ rs := newTestWrapper(dataDir, st)
cfg := Config{
OutputSize: 68,
Root: "root",
+ CacheSize: 1024,
}
en := NewEngine(cfg, rs)
- en = en.WithState(&st)
- en = en.WithMemory(ca)
+ en = en.WithState(st)
_, err = en.Init(ctx)
if err != nil {
diff --git a/examples/intro/main.go b/examples/intro/main.go
@@ -94,7 +94,7 @@ func main() {
OutputSize: uint32(size),
}
en := engine.NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
diff --git a/examples/pincheck/main.go b/examples/pincheck/main.go
@@ -83,7 +83,7 @@ func main() {
panic(err)
}
rsf := resource.NewDbResource(store)
- rs := newPinResource(rsf, &st)
+ rs := newPinResource(rsf, st)
rsf.AddLocalFunc("pincheck", rs.pinCheck)
rsf.AddLocalFunc("pinclear", rs.pinClear)
ca := cache.NewCache()
@@ -92,7 +92,7 @@ func main() {
StateDebug: true,
}
en := engine.NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
if err != nil {
diff --git a/examples/preprocessor/main.go b/examples/preprocessor/main.go
@@ -93,7 +93,7 @@ func main() {
Root: "root",
}
en := engine.NewEngine(cfg, rsf)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
diff --git a/examples/profile/main.go b/examples/profile/main.go
@@ -108,7 +108,7 @@ func main() {
}
rsf := resource.NewDbResource(store)
rsf.With(db.DATATYPE_STATICLOAD)
- rs, ok := newProfileResource(&st, rsf).(*profileResource)
+ rs, ok := newProfileResource(st, rsf).(*profileResource)
if !ok {
os.Exit(1)
}
@@ -121,7 +121,7 @@ func main() {
OutputSize: uint32(size),
}
en := engine.NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithMemory(ca)
_, err = en.Init(ctx)
if err != nil {
diff --git a/examples/state/main.go b/examples/state/main.go
@@ -83,7 +83,7 @@ func main() {
Root: "root",
}
- aux := &flagResource{st: &st}
+ aux := &flagResource{st: st}
rs.AddLocalFunc("do_foo", aux.do)
rs.AddLocalFunc("do_bar", aux.do)
rs.AddLocalFunc("do_baz", aux.do)
@@ -94,7 +94,7 @@ func main() {
state.FlagDebugger.Register(USER_BAZ, "BAZ")
en := engine.NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
en = en.WithDebug(nil)
_, err = en.Init(ctx)
diff --git a/examples/validate/main.go b/examples/validate/main.go
@@ -65,7 +65,7 @@ func main() {
panic(err)
}
rsf := resource.NewDbResource(store)
- rs := verifyResource{rsf, &st}
+ rs := verifyResource{rsf, st}
rs.AddLocalFunc("verifyinput", rs.verify)
rs.AddLocalFunc("again", rs.again)
@@ -76,7 +76,7 @@ func main() {
}
en := engine.NewEngine(cfg, rs)
- en = en.WithState(&st)
+ en = en.WithState(st)
_, err = en.Init(ctx)
if err != nil {
diff --git a/persist/fs_test.go b/persist/fs_test.go
@@ -31,7 +31,7 @@ func TestSerializeState(t *testing.T) {
ctx := context.Background()
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithContext(context.Background()).WithSession("xyzzy").WithContent(&st, ca)
+ pr := NewPersister(store).WithContext(context.Background()).WithSession("xyzzy").WithContent(st, ca)
v, err := pr.Serialize()
if err != nil {
t.Error(err)
@@ -83,7 +83,7 @@ func TestSaveLoad(t *testing.T) {
ctx := context.Background()
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithContent(&st, ca)
+ pr := NewPersister(store).WithContent(st, ca)
err := pr.Save("xyzzy")
if err != nil {
t.Fatal(err)
@@ -124,7 +124,7 @@ func TestSaveLoadFlags(t *testing.T) {
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithContent(&st, ca)
+ pr := NewPersister(store).WithContent(st, ca)
err := pr.Save("xyzzy")
if err != nil {
t.Fatal(err)
diff --git a/persist/persist_test.go b/persist/persist_test.go
@@ -16,7 +16,7 @@ func TestInvalidateState(t *testing.T) {
ctx := context.Background()
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
+ pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca)
err := pr.Save("foo")
if err != nil {
t.Fatal(err)
@@ -38,7 +38,7 @@ func TestInvalidateCache(t *testing.T) {
ctx := context.Background()
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
+ pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca)
err := pr.Save("foo")
if err != nil {
t.Fatal(err)
@@ -60,7 +60,7 @@ func TestInvalidateAll(t *testing.T) {
ctx := context.Background()
store := mem.NewMemDb()
store.Connect(ctx, "")
- pr := NewPersister(store).WithSession("xyzzy").WithContent(&st, ca)
+ pr := NewPersister(store).WithSession("xyzzy").WithContent(st, ca)
err := pr.Save("foo")
if err != nil {
t.Fatal(err)
diff --git a/state/state.go b/state/state.go
@@ -78,8 +78,8 @@ func(st *State) Invalid() bool {
//}
// NewState creates a new State object with BitSize number of error condition states in ADDITION to the 8 builtin flags.
-func NewState(BitSize uint32) State {
- st := State{
+func NewState(BitSize uint32) *State {
+ st := &State{
BitSize: BitSize + 8,
}
byteSize := toByteSize(BitSize + 8)
diff --git a/vm/runner_test.go b/vm/runner_test.go
@@ -180,10 +180,10 @@ func(r testResource) getCode(sym string) ([]byte, error) {
func TestRun(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
b := NewLine(nil, MOVE, []string{"foo"}, nil, nil)
b = NewLine(b, HALT, nil, nil, nil)
@@ -202,10 +202,10 @@ func TestRun(t *testing.T) {
func TestRunLoadRender(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
st.Down("bar")
@@ -255,10 +255,10 @@ func TestRunLoadRender(t *testing.T) {
func TestRunMultiple(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
ctx := context.Background()
b := NewLine(nil, MOVE, []string{"test"}, nil, nil)
@@ -274,11 +274,11 @@ func TestRunMultiple(t *testing.T) {
func TestRunReload(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
szr := render.NewSizer(128)
- vm := NewVm(&st, &rs, ca, szr)
+ vm := NewVm(st, &rs, ca, szr)
ctx := context.Background()
b := NewLine(nil, MOVE, []string{"root"}, nil, nil)
@@ -307,10 +307,10 @@ func TestRunReload(t *testing.T) {
func TestHalt(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
b := NewLine(nil, MOVE, []string{"root"}, nil, nil)
b = NewLine(b, LOAD, []string{"one"}, nil, []uint8{0})
@@ -333,10 +333,10 @@ func TestHalt(t *testing.T) {
func TestRunArg(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
input := []byte("bar")
_ = st.SetInput(input)
@@ -361,10 +361,10 @@ func TestRunArg(t *testing.T) {
func TestRunInputHandler(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
_ = st.SetInput([]byte("baz"))
@@ -390,10 +390,10 @@ func TestRunInputHandler(t *testing.T) {
func TestRunArgInvalid(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
_ = st.SetInput([]byte("foo"))
@@ -425,9 +425,9 @@ func TestRunArgInvalid(t *testing.T) {
func TestRunMenu(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -462,9 +462,9 @@ func TestRunMenu(t *testing.T) {
func TestRunMenuBrowse(t *testing.T) {
log.Printf("This test is incomplete, it must check the output of a menu browser once one is implemented. For now it only checks whether it can execute the runner endpoints for the instrucitons.")
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -498,10 +498,10 @@ func TestRunMenuBrowse(t *testing.T) {
func TestRunReturn(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -535,10 +535,10 @@ func TestRunReturn(t *testing.T) {
func TestRunLoadInput(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -566,9 +566,9 @@ func TestRunLoadInput(t *testing.T) {
func TestInputBranch(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -607,10 +607,10 @@ func TestInputBranch(t *testing.T) {
func TestInputIgnore(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -636,10 +636,10 @@ func TestInputIgnore(t *testing.T) {
func TestInputIgnoreWildcard(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -664,10 +664,10 @@ func TestInputIgnoreWildcard(t *testing.T) {
func TestCatchCleanMenu(t *testing.T) {
st := state.NewState(5)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -701,10 +701,10 @@ func TestCatchCleanMenu(t *testing.T) {
func TestSetLang(t *testing.T) {
st := state.NewState(0)
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
var err error
@@ -728,10 +728,10 @@ func TestSetLang(t *testing.T) {
func TestLoadError(t *testing.T) {
st := state.NewState(0)
st.UseDebug()
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
st.Down("root")
st.SetInput([]byte{})
@@ -762,10 +762,10 @@ func TestMatchFlag(t *testing.T) {
st := state.NewState(1)
st.UseDebug()
- rs := newTestResource(&st)
+ rs := newTestResource(st)
rs.Lock()
ca := cache.NewCache()
- vm := NewVm(&st, &rs, ca, nil)
+ vm := NewVm(st, &rs, ca, nil)
st.Down("root")
st.SetFlag(state.FLAG_USERSTART)