ui: move ops reader to ui package

To prepare support for cached OpBlock to refer to other Ops lists.

The exposure of OpsReader is alleviated by the removal of the Refs
and Data accessors for Ops.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-06-01 19:46:46 +02:00
parent 528a588f2e
commit 5966aab77e
6 changed files with 90 additions and 102 deletions
-81
View File
@@ -1,23 +1,5 @@
package ops
import (
"encoding/binary"
)
type Reader struct {
pc int
stack []block
Refs []interface{}
data []byte
pseudoOp [1]byte
}
type block struct {
retPC int
endPC int
}
type OpType byte
const (
@@ -53,66 +35,3 @@ const (
TypePushLen = 1
TypePopLen = 1
)
var typeLengths = [...]int{
TypeBlockDefLen,
TypeBlockLen,
TypeTransformLen,
TypeLayerLen,
TypeRedrawLen,
TypeClipLen,
TypeImageLen,
TypeDrawLen,
TypeColorLen,
TypePointerHandlerLen,
TypeKeyHandlerLen,
TypeHideInputLen,
TypePushLen,
TypePopLen,
}
// Reset start reading from the op list.
func (r *Reader) Reset(data []byte, refs []interface{}) {
r.Refs = refs
r.data = data
r.stack = r.stack[:0]
r.pc = 0
}
func (r *Reader) Decode() ([]byte, bool) {
bo := binary.LittleEndian
for {
if r.pc == len(r.data) {
return nil, false
}
if len(r.stack) > 0 {
b := r.stack[len(r.stack)-1]
if r.pc == b.endPC {
r.pc = b.retPC
r.stack = r.stack[:len(r.stack)-1]
r.pseudoOp[0] = byte(TypePop)
return r.pseudoOp[:], true
}
}
t := OpType(r.data[r.pc])
n := typeLengths[t]
data := r.data[r.pc : r.pc+n]
switch t {
case TypeBlock:
blockIdx := int(bo.Uint32(data[1:]))
if OpType(r.data[blockIdx]) != 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 + TypeBlockDefLen
r.pseudoOp[0] = byte(TypePush)
return r.pseudoOp[:], true
case TypeBlockDef:
r.pc += int(bo.Uint32(data[1:]))
continue
}
r.pc += n
return data, true
}
}