Documentation ¶
Overview ¶
Package vm implements the virtual machine used to run the bytecode generated by http://github.com/lestrrat-go/xslate/compiler
The virtual machine is an extremely simple one: each opcode in the bytecode sequence returns the next opcode to execute. The virtual machine just keeps on calling the opcode until we reach the "end" opcode.
The virtual machine accepts the bytecode, and input variables:
vm.Run(bytecode, variables)
Index ¶
- type ByteCode
- type LoopVar
- type Op
- type OpHandler
- type OpType
- type State
- func (st *State) Advance()
- func (st *State) AdvanceBy(i int)
- func (st *State) AdvanceTo(i int)
- func (st *State) AppendOutput(b []byte)
- func (st *State) AppendOutputString(o string)
- func (st *State) CurrentFrame() *frame.Frame
- func (st *State) CurrentMark() int
- func (st *State) CurrentOp() Op
- func (st *State) CurrentPos() int
- func (st *State) LoadByteCode(key string) (*ByteCode, error)
- func (st *State) PopFrame() *frame.Frame
- func (st *State) Popmark() int
- func (st *State) PushFrame() *frame.Frame
- func (st *State) Pushmark()
- func (st *State) Reset()
- func (st *State) StackPop() interface{}
- func (st *State) StackPush(v interface{})
- func (st *State) StackTip() int
- func (st *State) Vars() Vars
- func (st *State) Warnf(format string, args ...interface{})
- type VM
- type Vars
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ByteCode ¶
ByteCode is the collection of op codes that the Xslate Virtual Machine should run. It is created from a compiler.Compiler
func (*ByteCode) AppendOp ¶
AppendOp is an utility method to create AND append a new op code to the current list of op codes
func (*ByteCode) Get ¶
Get returns an vm.Op struct at location i. No check is performed to see if this index is valid
type LoopVar ¶
type LoopVar struct { Index int // 0 origin, current index Count int // loop.Index + 1 Body reflect.Value // alias to array Size int // len(loop.Body) MaxIndex int // loop.Size - 1 PeekNext interface{} // previous item. nil if not available PeekPrev interface{} // next item. nil if not available IsFirst bool // true only if Index == 0 IsLast bool // true only if Index == MaxIndex }
LoopVar is the variable available within FOREACH loops
type Op ¶
type Op interface { Arg() interface{} ArgString() string ArgInt() int Call(*State) Comment() string Handler() OpHandler SetArg(interface{}) SetComment(string) String() string Type() OpType }
Op represents a single op. It has an OpType, OpHandler, and an optional parameter to be used
type OpType ¶
type OpType int
OpType is an integer identifying the type of op code
const ( TXOPNoop OpType = iota TXOPNil TXOPMoveToSb TXOPMoveFromSb TXOPLiteral TXOPFetchSymbol TXOPFetchFieldSymbol TXOPFetchArrayElement TXOPMarkRaw TXOPUnmarkRaw TXOPPrint TXOPPrintRaw TXOPPrintRawConst TXOPSaveToLvar TXOPLoadLvar TXOPAdd TXOPSub TXOPMul TXOPDiv TXOPAnd TXOPGoto TXOPForStart TXOPForIter TXOPHTMLEscape TXOPUriEscape TXOPEquals TXOPNotEquals TXOPLessThan TXOPGreaterThan TXOPPopmark TXOPPushmark TXOPPopFrame TXOPPushFrame TXOPPush TXOPPop TXOPFunCall TXOPFunCallSymbol TXOPFunCallOmni TXOPMethodCall TXOPRange TXOPMakeArray TXOPMakeHash TXOPInclude TXOPWrapper TXOPFilter TXOPSaveWriter TXOPRestoreWriter TXOPEnd TXOPMax )
These TXOP... constants are identifiers for each op
type State ¶
type State struct { Loader byteCodeLoader MaxLoopCount int // contains filtered or unexported fields }
State keeps track of Xslate Virtual Machine state
func (*State) AppendOutput ¶
AppendOutput appends the specified bytes to the output
func (*State) AppendOutputString ¶
AppendOutputString is the same as AppendOutput, but uses a string
func (*State) CurrentFrame ¶
CurrentFrame returns the frame currently at the top of the frame stack
func (*State) CurrentMark ¶
CurrentMark returns the mark stored at the top of the mark stack
func (*State) CurrentPos ¶
CurrentPos returns the position of the current executing op
func (*State) LoadByteCode ¶
LoadByteCode loads a new ByteCode. This is used for op codes that call to external templates such as `include`
func (*State) Pushmark ¶
func (st *State) Pushmark()
Pushmark records the current stack tip so we can remember where the current context started
type VM ¶
type VM struct { Loader byteCodeLoader // contains filtered or unexported fields }
VM represents the Xslate Virtual Machine
func (*VM) IsSupportedByteCodeVersion ¶
IsSupportedByteCodeVersion returns true if this VM can handle the provided bytecode version
func (*VM) Run ¶
Run executes the given vm.ByteCode using the given variables. For historical reasons, it also allows re-executing the previous bytecode instructions given to a virtual machine, but this will probably be removed in the future