mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
ui: let OpsReader keep track of references
Instead of exposing the entire reference slice, return the relevant references from Next. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
// Ops holds a list of serialized Ops.
|
||||
type Ops struct {
|
||||
// Stack of block start indices.
|
||||
stack []int
|
||||
stack []pc
|
||||
// Serialized ops.
|
||||
data []byte
|
||||
// Op references.
|
||||
@@ -17,17 +17,22 @@ type Ops struct {
|
||||
}
|
||||
|
||||
type OpsReader struct {
|
||||
pc int
|
||||
pc pc
|
||||
stack []block
|
||||
Refs []interface{}
|
||||
refs []interface{}
|
||||
data []byte
|
||||
|
||||
pseudoOp [1]byte
|
||||
}
|
||||
|
||||
type block struct {
|
||||
retPC int
|
||||
endPC int
|
||||
retPC pc
|
||||
endPC pc
|
||||
}
|
||||
|
||||
type pc struct {
|
||||
data int
|
||||
refs int
|
||||
}
|
||||
|
||||
var typeLengths = [...]int{
|
||||
@@ -47,16 +52,51 @@ var typeLengths = [...]int{
|
||||
ops.TypePopLen,
|
||||
}
|
||||
|
||||
var refLengths = [...]int{
|
||||
ops.TypeBlockDefRefs,
|
||||
ops.TypeBlockRefs,
|
||||
ops.TypeTransformRefs,
|
||||
ops.TypeLayerRefs,
|
||||
ops.TypeRedrawRefs,
|
||||
ops.TypeClipRefs,
|
||||
ops.TypeImageRefs,
|
||||
ops.TypeDrawRefs,
|
||||
ops.TypeColorRefs,
|
||||
ops.TypePointerHandlerRefs,
|
||||
ops.TypeKeyHandlerRefs,
|
||||
ops.TypeHideInputRefs,
|
||||
ops.TypePushRefs,
|
||||
ops.TypePopRefs,
|
||||
}
|
||||
|
||||
type OpBlock struct {
|
||||
idx int
|
||||
pc pc
|
||||
}
|
||||
|
||||
type opBlockDef struct {
|
||||
endpc pc
|
||||
}
|
||||
|
||||
// Begin a block of ops.
|
||||
func (o *Ops) Begin() {
|
||||
o.stack = append(o.stack, o.Size())
|
||||
data := make([]byte, ops.TypeBlockDefLen)
|
||||
data[0] = byte(ops.TypeBlockDef)
|
||||
o.Write(data)
|
||||
o.stack = append(o.stack, o.pc())
|
||||
// Make room for a block definition. Filled out in End.
|
||||
o.data = append(o.data, make([]byte, ops.TypeBlockDefLen)...)
|
||||
}
|
||||
|
||||
func (op *opBlockDef) decode(data []byte) {
|
||||
if ops.OpType(data[0]) != ops.TypeBlockDef {
|
||||
panic("invalid op")
|
||||
}
|
||||
bo := binary.LittleEndian
|
||||
dataIdx := int(bo.Uint32(data[1:]))
|
||||
refsIdx := int(bo.Uint32(data[5:]))
|
||||
*op = opBlockDef{
|
||||
endpc: pc{
|
||||
data: dataIdx,
|
||||
refs: refsIdx,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// End the most recent block and return
|
||||
@@ -64,9 +104,13 @@ func (o *Ops) Begin() {
|
||||
func (o *Ops) End() OpBlock {
|
||||
start := o.stack[len(o.stack)-1]
|
||||
o.stack = o.stack[:len(o.stack)-1]
|
||||
blockLen := o.Size() - start
|
||||
pc := o.pc()
|
||||
// Write the block header reserved in Begin.
|
||||
data := o.data[start.data : start.data+ops.TypeBlockDefLen]
|
||||
data[0] = byte(ops.TypeBlockDef)
|
||||
bo := binary.LittleEndian
|
||||
bo.PutUint32(o.data[start+1:], uint32(blockLen))
|
||||
bo.PutUint32(data[1:], uint32(pc.data))
|
||||
bo.PutUint32(data[5:], uint32(pc.refs))
|
||||
return OpBlock{start}
|
||||
}
|
||||
|
||||
@@ -77,41 +121,51 @@ func (o *Ops) Reset() {
|
||||
o.data = o.data[:0]
|
||||
}
|
||||
|
||||
func (o *Ops) Ref(r interface{}) int {
|
||||
o.refs = append(o.refs, r)
|
||||
return len(o.refs) - 1
|
||||
}
|
||||
|
||||
func (o *Ops) Write(op []byte) {
|
||||
func (o *Ops) Write(op []byte, refs []interface{}) {
|
||||
o.data = append(o.data, op...)
|
||||
o.refs = append(o.refs, refs...)
|
||||
}
|
||||
|
||||
// Size returns the length of the serialized Op data.
|
||||
func (o *Ops) Size() int {
|
||||
return len(o.data)
|
||||
func (o *Ops) pc() pc {
|
||||
return pc{data: len(o.data), refs: len(o.refs)}
|
||||
}
|
||||
|
||||
func (b *OpBlock) decode(data []byte) {
|
||||
if ops.OpType(data[0]) != ops.TypeBlock {
|
||||
panic("invalid op")
|
||||
}
|
||||
bo := binary.LittleEndian
|
||||
dataIdx := int(bo.Uint32(data[1:]))
|
||||
refsIdx := int(bo.Uint32(data[5:]))
|
||||
*b = OpBlock{
|
||||
pc: pc{
|
||||
data: dataIdx,
|
||||
refs: refsIdx,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (b OpBlock) Add(o *Ops) {
|
||||
data := make([]byte, ops.TypeBlockLen)
|
||||
data[0] = byte(ops.TypeBlock)
|
||||
bo := binary.LittleEndian
|
||||
bo.PutUint32(data[1:], uint32(b.idx))
|
||||
o.Write(data)
|
||||
bo.PutUint32(data[1:], uint32(b.pc.data))
|
||||
bo.PutUint32(data[5:], uint32(b.pc.refs))
|
||||
o.Write(data, nil)
|
||||
}
|
||||
|
||||
// Reset start reading from the op list.
|
||||
func (r *OpsReader) Reset(ops *Ops) {
|
||||
r.Refs = ops.refs
|
||||
r.refs = ops.refs
|
||||
r.data = ops.data
|
||||
r.stack = r.stack[:0]
|
||||
r.pc = 0
|
||||
r.pc = pc{}
|
||||
}
|
||||
|
||||
func (r *OpsReader) Decode() ([]byte, bool) {
|
||||
bo := binary.LittleEndian
|
||||
func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
|
||||
for {
|
||||
if r.pc == len(r.data) {
|
||||
return nil, false
|
||||
if r.pc.data == len(r.data) {
|
||||
return nil, nil, false
|
||||
}
|
||||
if len(r.stack) > 0 {
|
||||
b := r.stack[len(r.stack)-1]
|
||||
@@ -119,28 +173,40 @@ func (r *OpsReader) Decode() ([]byte, bool) {
|
||||
r.pc = b.retPC
|
||||
r.stack = r.stack[:len(r.stack)-1]
|
||||
r.pseudoOp[0] = byte(ops.TypePop)
|
||||
return r.pseudoOp[:], true
|
||||
return r.pseudoOp[:], nil, true
|
||||
}
|
||||
}
|
||||
t := ops.OpType(r.data[r.pc])
|
||||
n := typeLengths[t]
|
||||
data := r.data[r.pc : r.pc+n]
|
||||
t := ops.OpType(r.data[r.pc.data])
|
||||
n := typeLengths[t-ops.FirstOpIndex]
|
||||
nrefs := refLengths[t-ops.FirstOpIndex]
|
||||
data := r.data[r.pc.data : r.pc.data+n]
|
||||
refs := r.refs[r.pc.refs : r.pc.refs+nrefs]
|
||||
switch t {
|
||||
case ops.TypeBlock:
|
||||
blockIdx := int(bo.Uint32(data[1:]))
|
||||
if ops.OpType(r.data[blockIdx]) != ops.TypeBlockDef {
|
||||
var op OpBlock
|
||||
op.decode(data)
|
||||
if ops.OpType(r.data[op.pc.data]) != ops.TypeBlockDef {
|
||||
panic("invalid block reference")
|
||||
}
|
||||
blockLen := int(bo.Uint32(r.data[blockIdx+1:]))
|
||||
r.stack = append(r.stack, block{r.pc + n, blockIdx + blockLen})
|
||||
r.pc = blockIdx + ops.TypeBlockDefLen
|
||||
var opDef opBlockDef
|
||||
opDef.decode(r.data[op.pc.data : op.pc.data+ops.TypeBlockDefLen])
|
||||
retPC := r.pc
|
||||
retPC.data += n
|
||||
retPC.refs += nrefs
|
||||
r.stack = append(r.stack, block{retPC: retPC, endPC: opDef.endpc})
|
||||
r.pc = op.pc
|
||||
r.pc.data += ops.TypeBlockDefLen
|
||||
r.pc.refs += ops.TypeBlockDefRefs
|
||||
r.pseudoOp[0] = byte(ops.TypePush)
|
||||
return r.pseudoOp[:], true
|
||||
return r.pseudoOp[:], nil, true
|
||||
case ops.TypeBlockDef:
|
||||
r.pc += int(bo.Uint32(data[1:]))
|
||||
var op opBlockDef
|
||||
op.decode(data)
|
||||
r.pc = op.endpc
|
||||
continue
|
||||
}
|
||||
r.pc += n
|
||||
return data, true
|
||||
r.pc.data += n
|
||||
r.pc.refs += nrefs
|
||||
return data, refs, true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user