internal/ops: expose PC and ResetAt

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-03-23 16:19:58 +01:00
parent 0a4b6549da
commit 06c53c3777
+26 -11
View File
@@ -12,7 +12,7 @@ import (
// Reader parses an ops list. // Reader parses an ops list.
type Reader struct { type Reader struct {
pc pc pc PC
stack []macro stack []macro
ops *op.Ops ops *op.Ops
deferOps op.Ops deferOps op.Ops
@@ -38,33 +38,48 @@ type Key struct {
// Shadow of op.MacroOp. // Shadow of op.MacroOp.
type macroOp struct { type macroOp struct {
ops *op.Ops ops *op.Ops
pc pc pc PC
} }
type pc struct { // PC is an instruction counter for an operation list.
type PC struct {
data int data int
refs int refs int
} }
type macro struct { type macro struct {
ops *op.Ops ops *op.Ops
retPC pc retPC PC
endPC pc endPC PC
} }
type opMacroDef struct { type opMacroDef struct {
endpc pc endpc PC
} }
// Reset start reading from the op list. // Reset start reading from the beginning of ops.
func (r *Reader) Reset(ops *op.Ops) { func (r *Reader) Reset(ops *op.Ops) {
r.ResetAt(ops, PC{})
}
// ResetAt is like Reset, except it starts reading from pc.
func (r *Reader) ResetAt(ops *op.Ops, pc PC) {
r.stack = r.stack[:0] r.stack = r.stack[:0]
r.deferOps.Reset() r.deferOps.Reset()
r.deferDone = false r.deferDone = false
r.pc = pc{} r.pc = pc
r.ops = ops r.ops = ops
} }
// NewPC returns a PC representing the current instruction counter of
// ops.
func NewPC(ops *op.Ops) PC {
return PC{
data: len(ops.Data()),
refs: len(ops.Refs()),
}
}
func (k Key) SetTransform(t f32.Affine2D) Key { func (k Key) SetTransform(t f32.Affine2D) Key {
sx, hx, _, hy, sy, _ := t.Elems() sx, hx, _, hy, sy, _ := t.Elems()
k.sx = sx k.sx = sx
@@ -99,7 +114,7 @@ func (r *Reader) Decode() (EncodedOp, bool) {
r.deferDone = true r.deferDone = true
// Execute deferred macros. // Execute deferred macros.
r.ops = &r.deferOps r.ops = &r.deferOps
r.pc = pc{} r.pc = PC{}
continue continue
} }
key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()} key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()}
@@ -174,7 +189,7 @@ func (op *opMacroDef) decode(data []byte) {
dataIdx := int(int32(bo.Uint32(data[1:]))) dataIdx := int(int32(bo.Uint32(data[1:])))
refsIdx := int(int32(bo.Uint32(data[5:]))) refsIdx := int(int32(bo.Uint32(data[5:])))
*op = opMacroDef{ *op = opMacroDef{
endpc: pc{ endpc: PC{
data: dataIdx, data: dataIdx,
refs: refsIdx, refs: refsIdx,
}, },
@@ -191,7 +206,7 @@ func (m *macroOp) decode(data []byte, refs []interface{}) {
refsIdx := int(int32(bo.Uint32(data[5:]))) refsIdx := int(int32(bo.Uint32(data[5:])))
*m = macroOp{ *m = macroOp{
ops: refs[0].(*op.Ops), ops: refs[0].(*op.Ops),
pc: pc{ pc: PC{
data: dataIdx, data: dataIdx,
refs: refsIdx, refs: refsIdx,
}, },