func txWrapper(st *State) { // See txInclude vars := Vars(rvpool.Get()) defer rvpool.Release(vars) defer vars.Reset() if x := st.Vars(); x != nil { for k, v := range x { vars.Set(k, v) } } if x := st.sb; x != nil { hash := x.(map[interface{}]interface{}) // Need to covert this to Vars (map[string]interface{}) for k, v := range hash { vars.Set(interfaceToString(k), v) } } vars.Set("content", rawString(st.sa.(string))) target := st.CurrentOp().ArgString() bc, err := st.LoadByteCode(target) if err != nil { panic(fmt.Sprintf("Wrapper: Failed to compile %s: %s", target, err)) } vm := NewVM() vm.Run(bc, vars, st.output) st.Advance() }
// 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 func (vm *VM) Run(bc *ByteCode, vars Vars, output io.Writer) { if !vm.IsSupportedByteCodeVersion(bc) { panic(fmt.Sprintf( "error: ByteCode version %f no supported", bc.Version, )) } st := vm.st if _, ok := output.(*bufio.Writer); !ok { output = bufio.NewWriter(output) defer output.(*bufio.Writer).Flush() } st.Reset() st.pc = bc st.output = output newvars := Vars(rvpool.Get()) defer rvpool.Release(newvars) defer newvars.Reset() st.vars = newvars if fc := vm.functions; fc != nil { for k, v := range vm.functions { st.vars[k] = v } } if vars != nil { for k, v := range vars { st.vars[k] = v } } st.Loader = vm.Loader // This is the main loop for op := st.CurrentOp(); op.Type() != TXOPEnd; op = st.CurrentOp() { op.Call(st) } }
func txInclude(st *State) { // st.sa should contain the include target // st.sb should contain the map[interface{}]interface{} // object that gets passed to the included template vars := Vars(rvpool.Get()) defer rvpool.Release(vars) defer vars.Reset() if x := st.Vars(); x != nil { for k, v := range x { vars.Set(k, v) } } if x := st.sb; x != nil { hash := x.(map[interface{}]interface{}) // Need to covert this to Vars (map[string]interface{}) for k, v := range hash { vars.Set(interfaceToString(k), v) } } target := interfaceToString(st.sa) bc, err := st.LoadByteCode(target) if err != nil { panic(fmt.Sprintf("Include: Failed to compile %s: %s", target, err)) } buf := rbpool.Get() defer rbpool.Release(buf) vm := NewVM() vm.Run(bc, vars, buf) st.AppendOutputString(buf.String()) st.Advance() }