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.
type Reader struct {
pc pc
pc PC
stack []macro
ops *op.Ops
deferOps op.Ops
@@ -38,33 +38,48 @@ type Key struct {
// Shadow of op.MacroOp.
type macroOp struct {
ops *op.Ops
pc pc
pc PC
}
type pc struct {
// PC is an instruction counter for an operation list.
type PC struct {
data int
refs int
}
type macro struct {
ops *op.Ops
retPC pc
endPC pc
retPC PC
endPC PC
}
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) {
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.deferOps.Reset()
r.deferDone = false
r.pc = pc{}
r.pc = pc
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 {
sx, hx, _, hy, sy, _ := t.Elems()
k.sx = sx
@@ -99,7 +114,7 @@ func (r *Reader) Decode() (EncodedOp, bool) {
r.deferDone = true
// Execute deferred macros.
r.ops = &r.deferOps
r.pc = pc{}
r.pc = PC{}
continue
}
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:])))
refsIdx := int(int32(bo.Uint32(data[5:])))
*op = opMacroDef{
endpc: pc{
endpc: PC{
data: dataIdx,
refs: refsIdx,
},
@@ -191,7 +206,7 @@ func (m *macroOp) decode(data []byte, refs []interface{}) {
refsIdx := int(int32(bo.Uint32(data[5:])))
*m = macroOp{
ops: refs[0].(*op.Ops),
pc: pc{
pc: PC{
data: dataIdx,
refs: refsIdx,
},