ui,ui/internal/ops: store owner Ops in OpBlock

Allows users to refer to other (cached) Ops from a main Ops.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-01 21:41:32 +02:00
parent 9d661277da
commit bf5d083b8a
2 changed files with 22 additions and 12 deletions
+1 -1
View File
@@ -39,7 +39,7 @@ const (
TypePopLen = 1 TypePopLen = 1
TypeBlockDefRefs = 0 TypeBlockDefRefs = 0
TypeBlockRefs = 0 TypeBlockRefs = 1
TypeTransformRefs = 0 TypeTransformRefs = 0
TypeLayerRefs = 0 TypeLayerRefs = 0
TypeRedrawRefs = 0 TypeRedrawRefs = 0
+21 -11
View File
@@ -29,6 +29,7 @@ type OpsReader struct {
} }
type block struct { type block struct {
ops opsData
retPC pc retPC pc
endPC pc endPC pc
} }
@@ -73,7 +74,8 @@ var refLengths = [...]int{
} }
type OpBlock struct { type OpBlock struct {
pc pc ops *Ops
pc pc
} }
type opBlockDef struct { type opBlockDef struct {
@@ -114,7 +116,7 @@ func (o *Ops) End() OpBlock {
bo := binary.LittleEndian bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(pc.data)) bo.PutUint32(data[1:], uint32(pc.data))
bo.PutUint32(data[5:], uint32(pc.refs)) bo.PutUint32(data[5:], uint32(pc.refs))
return OpBlock{start} return OpBlock{ops: o, pc: start}
} }
// Reset clears the Ops. // Reset clears the Ops.
@@ -141,7 +143,7 @@ func (d *opsData) pc() pc {
return pc{data: len(d.data), refs: len(d.refs)} return pc{data: len(d.data), refs: len(d.refs)}
} }
func (b *OpBlock) decode(data []byte) { func (b *OpBlock) decode(data []byte, refs []interface{}) {
if ops.OpType(data[0]) != ops.TypeBlock { if ops.OpType(data[0]) != ops.TypeBlock {
panic("invalid op") panic("invalid op")
} }
@@ -149,6 +151,7 @@ func (b *OpBlock) decode(data []byte) {
dataIdx := int(bo.Uint32(data[1:])) dataIdx := int(bo.Uint32(data[1:]))
refsIdx := int(bo.Uint32(data[5:])) refsIdx := int(bo.Uint32(data[5:]))
*b = OpBlock{ *b = OpBlock{
ops: refs[0].(*Ops),
pc: pc{ pc: pc{
data: dataIdx, data: dataIdx,
refs: refsIdx, refs: refsIdx,
@@ -162,7 +165,7 @@ func (b OpBlock) Add(o *Ops) {
bo := binary.LittleEndian bo := binary.LittleEndian
bo.PutUint32(data[1:], uint32(b.pc.data)) bo.PutUint32(data[1:], uint32(b.pc.data))
bo.PutUint32(data[5:], uint32(b.pc.refs)) bo.PutUint32(data[5:], uint32(b.pc.refs))
o.Write(data, nil) o.Write(data, []interface{}{b.ops})
} }
// Reset start reading from the op list. // Reset start reading from the op list.
@@ -174,18 +177,19 @@ func (r *OpsReader) Reset(ops *Ops) {
func (r *OpsReader) Decode() ([]byte, []interface{}, bool) { func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
for { for {
if r.pc.data == len(r.ops.data) {
return nil, nil, false
}
if len(r.stack) > 0 { if len(r.stack) > 0 {
b := r.stack[len(r.stack)-1] b := r.stack[len(r.stack)-1]
if r.pc == b.endPC { if r.pc == b.endPC {
r.ops = b.ops
r.pc = b.retPC r.pc = b.retPC
r.stack = r.stack[:len(r.stack)-1] r.stack = r.stack[:len(r.stack)-1]
r.pseudoOp[0] = byte(ops.TypePop) r.pseudoOp[0] = byte(ops.TypePop)
return r.pseudoOp[:], nil, true return r.pseudoOp[:], nil, true
} }
} }
if r.pc.data == len(r.ops.data) {
return nil, nil, false
}
t := ops.OpType(r.ops.data[r.pc.data]) t := ops.OpType(r.ops.data[r.pc.data])
n := typeLengths[t-ops.FirstOpIndex] n := typeLengths[t-ops.FirstOpIndex]
nrefs := refLengths[t-ops.FirstOpIndex] nrefs := refLengths[t-ops.FirstOpIndex]
@@ -194,16 +198,22 @@ func (r *OpsReader) Decode() ([]byte, []interface{}, bool) {
switch t { switch t {
case ops.TypeBlock: case ops.TypeBlock:
var op OpBlock var op OpBlock
op.decode(data) op.decode(data, refs)
if ops.OpType(r.ops.data[op.pc.data]) != ops.TypeBlockDef { blockOps := op.ops.ops
if ops.OpType(blockOps.data[op.pc.data]) != ops.TypeBlockDef {
panic("invalid block reference") panic("invalid block reference")
} }
var opDef opBlockDef var opDef opBlockDef
opDef.decode(r.ops.data[op.pc.data : op.pc.data+ops.TypeBlockDefLen]) opDef.decode(blockOps.data[op.pc.data : op.pc.data+ops.TypeBlockDefLen])
retPC := r.pc retPC := r.pc
retPC.data += n retPC.data += n
retPC.refs += nrefs retPC.refs += nrefs
r.stack = append(r.stack, block{retPC: retPC, endPC: opDef.endpc}) r.stack = append(r.stack, block{
ops: r.ops,
retPC: retPC,
endPC: opDef.endpc,
})
r.ops = blockOps
r.pc = op.pc r.pc = op.pc
r.pc.data += ops.TypeBlockDefLen r.pc.data += ops.TypeBlockDefLen
r.pc.refs += ops.TypeBlockDefRefs r.pc.refs += ops.TypeBlockDefRefs