op: move Ops internal methods and state to internal package ops

Merge package opconsts into ops as well; it only existed to break
import cycles.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-10-04 15:44:48 +02:00
parent 936c266b03
commit 391725b9d0
18 changed files with 558 additions and 561 deletions
+20 -32
View File
@@ -4,17 +4,14 @@ package ops
import (
"encoding/binary"
"gioui.org/internal/opconst"
"gioui.org/op"
)
// Reader parses an ops list.
type Reader struct {
pc PC
stack []macro
ops *op.Ops
deferOps op.Ops
ops *Ops
deferOps Ops
deferDone bool
}
@@ -28,14 +25,14 @@ type EncodedOp struct {
// Key is a unique key for a given op.
type Key struct {
ops *op.Ops
ops *Ops
pc int
version int
}
// Shadow of op.MacroOp.
type macroOp struct {
ops *op.Ops
ops *Ops
pc PC
}
@@ -46,7 +43,7 @@ type PC struct {
}
type macro struct {
ops *op.Ops
ops *Ops
retPC PC
endPC PC
}
@@ -56,12 +53,12 @@ type opMacroDef struct {
}
// Reset start reading from the beginning of ops.
func (r *Reader) Reset(ops *op.Ops) {
func (r *Reader) Reset(ops *Ops) {
r.ResetAt(ops, PC{})
}
// ResetAt is like Reset, except it starts reading from pc.
func (r *Reader) ResetAt(ops *op.Ops, pc PC) {
func (r *Reader) ResetAt(ops *Ops, pc PC) {
r.stack = r.stack[:0]
r.deferOps.Reset()
r.deferDone = false
@@ -69,15 +66,6 @@ func (r *Reader) ResetAt(ops *op.Ops, 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 (r *Reader) Decode() (EncodedOp, bool) {
if r.ops == nil {
return EncodedOp{}, false
@@ -107,25 +95,25 @@ func (r *Reader) Decode() (EncodedOp, bool) {
continue
}
key := Key{ops: r.ops, pc: r.pc.data, version: r.ops.Version()}
t := opconst.OpType(data[0])
t := OpType(data[0])
n := t.Size()
nrefs := t.NumRefs()
data = data[:n]
refs = refs[r.pc.refs:]
refs = refs[:nrefs]
switch t {
case opconst.TypeDefer:
case TypeDefer:
deferring = true
r.pc.data += n
r.pc.refs += nrefs
continue
case opconst.TypeAux:
case TypeAux:
// An Aux operations is always wrapped in a macro, and
// its length is the remaining space.
block := r.stack[len(r.stack)-1]
n += block.endPC.data - r.pc.data - opconst.TypeAuxLen
n += block.endPC.data - r.pc.data - TypeAuxLen
data = data[:n]
case opconst.TypeCall:
case TypeCall:
if deferring {
deferring = false
// Copy macro for deferred execution.
@@ -141,11 +129,11 @@ func (r *Reader) Decode() (EncodedOp, bool) {
var op macroOp
op.decode(data, refs)
macroData := op.ops.Data()[op.pc.data:]
if opconst.OpType(macroData[0]) != opconst.TypeMacro {
if OpType(macroData[0]) != TypeMacro {
panic("invalid macro reference")
}
var opDef opMacroDef
opDef.decode(macroData[:opconst.TypeMacro.Size()])
opDef.decode(macroData[:TypeMacro.Size()])
retPC := r.pc
retPC.data += n
retPC.refs += nrefs
@@ -156,10 +144,10 @@ func (r *Reader) Decode() (EncodedOp, bool) {
})
r.ops = op.ops
r.pc = op.pc
r.pc.data += opconst.TypeMacro.Size()
r.pc.refs += opconst.TypeMacro.NumRefs()
r.pc.data += TypeMacro.Size()
r.pc.refs += TypeMacro.NumRefs()
continue
case opconst.TypeMacro:
case TypeMacro:
var op opMacroDef
op.decode(data)
r.pc = op.endpc
@@ -172,7 +160,7 @@ func (r *Reader) Decode() (EncodedOp, bool) {
}
func (op *opMacroDef) decode(data []byte) {
if opconst.OpType(data[0]) != opconst.TypeMacro {
if OpType(data[0]) != TypeMacro {
panic("invalid op")
}
bo := binary.LittleEndian
@@ -188,7 +176,7 @@ func (op *opMacroDef) decode(data []byte) {
}
func (m *macroOp) decode(data []byte, refs []interface{}) {
if opconst.OpType(data[0]) != opconst.TypeCall {
if OpType(data[0]) != TypeCall {
panic("invalid op")
}
data = data[:9]
@@ -196,7 +184,7 @@ func (m *macroOp) decode(data []byte, refs []interface{}) {
dataIdx := int(int32(bo.Uint32(data[1:])))
refsIdx := int(int32(bo.Uint32(data[5:])))
*m = macroOp{
ops: refs[0].(*op.Ops),
ops: refs[0].(*Ops),
pc: PC{
data: dataIdx,
refs: refsIdx,