ui: expose internal Ops fields as methods

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-08-08 15:20:58 +02:00
parent 3c924e2a18
commit a6c262c1e1
2 changed files with 49 additions and 30 deletions
+15 -13
View File
@@ -62,10 +62,6 @@ type opAux struct {
func (r *Reader) Reset(ops *ui.Ops) { func (r *Reader) Reset(ops *ui.Ops) {
r.stack = r.stack[:0] r.stack = r.stack[:0]
r.pc = pc{} r.pc = pc{}
r.ops = nil
if ops == nil {
return
}
r.ops = ops r.ops = ops
} }
@@ -83,33 +79,39 @@ func (r *Reader) Decode() (EncodedOp, bool) {
continue continue
} }
} }
if r.pc.data == len(r.ops.Data) { data := r.ops.Data()
data = data[r.pc.data:]
if len(data) == 0 {
return EncodedOp{}, false return EncodedOp{}, false
} }
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()}
t := opconst.OpType(r.ops.Data[r.pc.data]) t := opconst.OpType(data[0])
n := t.Size() n := t.Size()
nrefs := t.NumRefs() nrefs := t.NumRefs()
data := r.ops.Data[r.pc.data : r.pc.data+n] data = data[:n]
refs := r.ops.Refs[r.pc.refs : r.pc.refs+nrefs] refs := r.ops.Refs()
refs = refs[r.pc.refs:]
refs = refs[:nrefs]
switch t { switch t {
case opconst.TypeAux: case opconst.TypeAux:
var op opAux var op opAux
op.decode(data) op.decode(data)
n += op.len n += op.len
data = r.ops.Data[r.pc.data : r.pc.data+n] data = data[:n]
case opconst.TypeMacro: case opconst.TypeMacro:
var op macroOp var op macroOp
op.decode(data, refs) op.decode(data, refs)
macroOps := op.ops macroOps := op.ops
if opconst.OpType(macroOps.Data[op.pc.data]) != opconst.TypeMacroDef { macroData := macroOps.Data()
macroData = macroData[op.pc.data:]
if opconst.OpType(macroData[0]) != opconst.TypeMacroDef {
panic("invalid macro reference") panic("invalid macro reference")
} }
if op.version != op.ops.Version { if op.version != op.ops.Version() {
panic("invalid MacroOp reference to reset Ops") panic("invalid MacroOp reference to reset Ops")
} }
var opDef opMacroDef var opDef opMacroDef
opDef.decode(macroOps.Data[op.pc.data : op.pc.data+opconst.TypeMacroDef.Size()]) opDef.decode(macroData[:opconst.TypeMacroDef.Size()])
retPC := r.pc retPC := r.pc
retPC.data += n retPC.data += n
retPC.refs += nrefs retPC.refs += nrefs
+34 -17
View File
@@ -10,12 +10,12 @@ import (
// Ops holds a list of serialized operations. // Ops holds a list of serialized operations.
type Ops struct { type Ops struct {
// Version is incremented at each Reset. // version is incremented at each Reset.
Version int version int
// Serialized operations. // data contains the serialized operations.
Data []byte data []byte
// External references for operations. // External references for operations.
Refs []interface{} refs []interface{}
stackDepth int stackDepth int
@@ -77,12 +77,27 @@ func (o *Ops) Reset() {
o.inAux = false o.inAux = false
o.stackDepth = 0 o.stackDepth = 0
// Leave references to the GC. // Leave references to the GC.
for i := range o.Refs { for i := range o.refs {
o.Refs[i] = nil o.refs[i] = nil
} }
o.Data = o.Data[:0] o.data = o.data[:0]
o.Refs = o.Refs[:0] o.refs = o.refs[:0]
o.Version++ o.version++
}
// Internal use only.
func (o *Ops) Data() []byte {
return o.data
}
// Internal use only.
func (o *Ops) Refs() []interface{} {
return o.refs
}
// Internal use only.
func (o *Ops) Version() int {
return o.version
} }
// Internal use only. // Internal use only.
@@ -90,12 +105,13 @@ func (o *Ops) Aux() []byte {
if !o.inAux { if !o.inAux {
return nil return nil
} }
return o.Data[o.auxOff+opconst.TypeAuxLen : o.auxOff+opconst.TypeAuxLen+o.auxLen] aux := o.data[o.auxOff+opconst.TypeAuxLen:]
return aux[:o.auxLen]
} }
func (d *Ops) write(op []byte, refs ...interface{}) { func (d *Ops) write(op []byte, refs ...interface{}) {
d.Data = append(d.Data, op...) d.data = append(d.data, op...)
d.Refs = append(d.Refs, refs...) d.refs = append(d.refs, refs...)
} }
// Internal use only. // Internal use only.
@@ -121,14 +137,14 @@ func (o *Ops) Write(op []byte, refs ...interface{}) {
if o.inAux { if o.inAux {
o.inAux = false o.inAux = false
bo := binary.LittleEndian bo := binary.LittleEndian
bo.PutUint32(o.Data[o.auxOff+1:], uint32(o.auxLen)) bo.PutUint32(o.data[o.auxOff+1:], uint32(o.auxLen))
} }
} }
o.write(op, refs...) o.write(op, refs...)
} }
func (d *Ops) pc() pc { func (d *Ops) pc() pc {
return pc{data: len(d.Data), refs: len(d.Refs)} return pc{data: len(d.data), refs: len(d.refs)}
} }
// Record a macro of operations. // Record a macro of operations.
@@ -151,12 +167,13 @@ func (m *MacroOp) Stop() {
m.recording = false m.recording = false
pc := m.ops.pc() pc := m.ops.pc()
// Fill out the macro definition reserved in Record. // Fill out the macro definition reserved in Record.
data := m.ops.Data[m.pc.data : m.pc.data+opconst.TypeMacroDefLen] data := m.ops.data[m.pc.data:]
data = data[:opconst.TypeMacroDefLen]
data[0] = byte(opconst.TypeMacroDef) data[0] = byte(opconst.TypeMacroDef)
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))
m.version = m.ops.Version m.version = m.ops.version
} }
func (m MacroOp) Add(o *Ops) { func (m MacroOp) Add(o *Ops) {