eval.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717
  1. // Copyright 2016 José Santos <henrique_1609@me.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jet
  15. import (
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "reflect"
  21. "runtime"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "sync"
  26. "github.com/CloudyKit/fastprinter"
  27. )
  28. var (
  29. funcType = reflect.TypeOf(Func(nil))
  30. stringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
  31. rangerType = reflect.TypeOf((*Ranger)(nil)).Elem()
  32. rendererType = reflect.TypeOf((*Renderer)(nil)).Elem()
  33. safeWriterType = reflect.TypeOf(SafeWriter(nil))
  34. pool_State = sync.Pool{
  35. New: func() interface{} {
  36. return &Runtime{scope: &scope{}, escapeeWriter: new(escapeeWriter)}
  37. },
  38. }
  39. )
  40. // Renderer is used to detect if a value has its own rendering logic. If the value an action evaluates to implements this
  41. // interface, it will not be printed using github.com/CloudyKit/fastprinter, instead, its Render() method will be called
  42. // and is responsible for writing the value to the render output.
  43. type Renderer interface {
  44. Render(*Runtime)
  45. }
  46. // RendererFunc func implementing interface Renderer
  47. type RendererFunc func(*Runtime)
  48. func (renderer RendererFunc) Render(r *Runtime) {
  49. renderer(r)
  50. }
  51. type escapeeWriter struct {
  52. Writer io.Writer
  53. escapee SafeWriter
  54. set *Set
  55. }
  56. func (w *escapeeWriter) Write(b []byte) (int, error) {
  57. if w.set.escapee == nil {
  58. w.Writer.Write(b)
  59. } else {
  60. w.set.escapee(w.Writer, b)
  61. }
  62. return 0, nil
  63. }
  64. // Runtime this type holds the state of the execution of an template
  65. type Runtime struct {
  66. *escapeeWriter
  67. *scope
  68. content func(*Runtime, Expression)
  69. context reflect.Value
  70. }
  71. // Context returns the current context value
  72. func (r *Runtime) Context() reflect.Value {
  73. return r.context
  74. }
  75. func (st *Runtime) newScope() {
  76. st.scope = &scope{parent: st.scope, variables: make(VarMap), blocks: st.blocks}
  77. }
  78. func (st *Runtime) releaseScope() {
  79. st.scope = st.scope.parent
  80. }
  81. type scope struct {
  82. parent *scope
  83. variables VarMap
  84. blocks map[string]*BlockNode
  85. }
  86. func (s scope) sortedBlocks() []string {
  87. r := make([]string, 0, len(s.blocks))
  88. for k := range s.blocks {
  89. r = append(r, k)
  90. }
  91. sort.Strings(r)
  92. return r
  93. }
  94. // YieldBlock yields a block in the current context, will panic if the context is not available
  95. func (st *Runtime) YieldBlock(name string, context interface{}) {
  96. block, has := st.getBlock(name)
  97. if has == false {
  98. panic(fmt.Errorf("Block %q was not found!!", name))
  99. }
  100. if context != nil {
  101. current := st.context
  102. st.context = reflect.ValueOf(context)
  103. st.executeList(block.List)
  104. st.context = current
  105. }
  106. st.executeList(block.List)
  107. }
  108. func (st *scope) getBlock(name string) (block *BlockNode, has bool) {
  109. block, has = st.blocks[name]
  110. for !has && st.parent != nil {
  111. st = st.parent
  112. block, has = st.blocks[name]
  113. }
  114. return
  115. }
  116. func (state *Runtime) setValue(name string, val reflect.Value) error {
  117. // try changing existing variable in current or parent scope
  118. sc := state.scope
  119. for sc != nil {
  120. if _, ok := sc.variables[name]; ok {
  121. sc.variables[name] = val
  122. return nil
  123. }
  124. sc = sc.parent
  125. }
  126. return fmt.Errorf("could not assign %q = %v because variable %q is uninitialised", name, val, name)
  127. }
  128. // LetGlobal sets or initialises a variable in the top-most template scope.
  129. func (state *Runtime) LetGlobal(name string, val interface{}) {
  130. sc := state.scope
  131. // walk up to top-most valid scope
  132. for sc.parent != nil && sc.parent.variables != nil {
  133. sc = sc.parent
  134. }
  135. sc.variables[name] = reflect.ValueOf(val)
  136. }
  137. // Set sets an existing variable in the template scope it lives in.
  138. func (state *Runtime) Set(name string, val interface{}) error {
  139. return state.setValue(name, reflect.ValueOf(val))
  140. }
  141. // Let initialises a variable in the current template scope (possibly shadowing an existing variable of the same name in a parent scope).
  142. func (state *Runtime) Let(name string, val interface{}) {
  143. state.scope.variables[name] = reflect.ValueOf(val)
  144. }
  145. // SetOrLet calls Set() (if a variable with the given name is visible from the current scope) or Let() (if there is no variable with the given name in the current or any parent scope).
  146. func (state *Runtime) SetOrLet(name string, val interface{}) {
  147. _, err := state.resolve(name)
  148. if err != nil {
  149. state.Let(name, val)
  150. } else {
  151. state.Set(name, val)
  152. }
  153. }
  154. // Resolve resolves a value from the execution context.
  155. func (state *Runtime) resolve(name string) (reflect.Value, error) {
  156. if name == "." {
  157. return state.context, nil
  158. }
  159. // try current, then parent variable scopes
  160. sc := state.scope
  161. for sc != nil {
  162. v, ok := sc.variables[name]
  163. if ok {
  164. return indirectEface(v), nil
  165. }
  166. sc = sc.parent
  167. }
  168. // try globals
  169. state.set.gmx.RLock()
  170. v, ok := state.set.globals[name]
  171. state.set.gmx.RUnlock()
  172. if ok {
  173. return indirectEface(v), nil
  174. }
  175. // try default variables
  176. v, ok = defaultVariables[name]
  177. if ok {
  178. return indirectEface(v), nil
  179. }
  180. return reflect.Value{}, fmt.Errorf("identifier %q not available in current (%+v) or parent scope, global, or default variables", name, state.scope.variables)
  181. }
  182. // Resolve calls resolve() and ignores any errors, meaning it may return a zero reflect.Value.
  183. func (state *Runtime) Resolve(name string) reflect.Value {
  184. v, _ := state.resolve(name)
  185. return v
  186. }
  187. // Resolve calls resolve() and panics if there is an error.
  188. func (state *Runtime) MustResolve(name string) reflect.Value {
  189. v, err := state.resolve(name)
  190. if err != nil {
  191. panic(err)
  192. }
  193. return v
  194. }
  195. func (st *Runtime) recover(err *error) {
  196. // reset state scope and context just to be safe (they might not be cleared properly if there was a panic while using the state)
  197. st.scope = &scope{}
  198. st.context = reflect.Value{}
  199. pool_State.Put(st)
  200. if recovered := recover(); recovered != nil {
  201. var ok bool
  202. if _, ok = recovered.(runtime.Error); ok {
  203. panic(recovered)
  204. }
  205. *err, ok = recovered.(error)
  206. if !ok {
  207. panic(recovered)
  208. }
  209. }
  210. }
  211. func (st *Runtime) executeSet(left Expression, right reflect.Value) {
  212. typ := left.Type()
  213. if typ == NodeIdentifier {
  214. err := st.setValue(left.(*IdentifierNode).Ident, right)
  215. if err != nil {
  216. left.error(err)
  217. }
  218. return
  219. }
  220. var value reflect.Value
  221. var fields []string
  222. if typ == NodeChain {
  223. chain := left.(*ChainNode)
  224. value = st.evalPrimaryExpressionGroup(chain.Node)
  225. fields = chain.Field
  226. } else {
  227. fields = left.(*FieldNode).Ident
  228. value = st.context
  229. }
  230. lef := len(fields) - 1
  231. for i := 0; i < lef; i++ {
  232. var err error
  233. value, err = resolveIndex(value, reflect.Value{}, fields[i])
  234. if err != nil {
  235. left.errorf("%v", err)
  236. }
  237. }
  238. RESTART:
  239. switch value.Kind() {
  240. case reflect.Ptr:
  241. value = value.Elem()
  242. goto RESTART
  243. case reflect.Struct:
  244. value = value.FieldByName(fields[lef])
  245. if !value.IsValid() {
  246. left.errorf("identifier %q is not available in the current scope", fields[lef])
  247. }
  248. value.Set(right)
  249. case reflect.Map:
  250. value.SetMapIndex(reflect.ValueOf(&fields[lef]).Elem(), right)
  251. }
  252. }
  253. func (st *Runtime) executeSetList(set *SetNode) {
  254. if set.IndexExprGetLookup {
  255. value := st.evalPrimaryExpressionGroup(set.Right[0])
  256. if set.Left[0].Type() != NodeUnderscore {
  257. st.executeSet(set.Left[0], value)
  258. }
  259. if set.Left[1].Type() != NodeUnderscore {
  260. if value.IsValid() {
  261. st.executeSet(set.Left[1], valueBoolTRUE)
  262. } else {
  263. st.executeSet(set.Left[1], valueBoolFALSE)
  264. }
  265. }
  266. } else {
  267. for i := 0; i < len(set.Left); i++ {
  268. value := st.evalPrimaryExpressionGroup(set.Right[i])
  269. if set.Left[i].Type() != NodeUnderscore {
  270. st.executeSet(set.Left[i], value)
  271. }
  272. }
  273. }
  274. }
  275. func (st *Runtime) executeLetList(set *SetNode) {
  276. if set.IndexExprGetLookup {
  277. value := st.evalPrimaryExpressionGroup(set.Right[0])
  278. if set.Left[0].Type() != NodeUnderscore {
  279. st.variables[set.Left[0].(*IdentifierNode).Ident] = value
  280. }
  281. if set.Left[1].Type() != NodeUnderscore {
  282. if value.IsValid() {
  283. st.variables[set.Left[1].(*IdentifierNode).Ident] = valueBoolTRUE
  284. } else {
  285. st.variables[set.Left[1].(*IdentifierNode).Ident] = valueBoolFALSE
  286. }
  287. }
  288. } else {
  289. for i := 0; i < len(set.Left); i++ {
  290. value := st.evalPrimaryExpressionGroup(set.Right[i])
  291. if set.Left[i].Type() != NodeUnderscore {
  292. st.variables[set.Left[i].(*IdentifierNode).Ident] = value
  293. }
  294. }
  295. }
  296. }
  297. func (st *Runtime) executeYieldBlock(block *BlockNode, blockParam, yieldParam *BlockParameterList, expression Expression, content *ListNode) {
  298. needNewScope := len(blockParam.List) > 0 || len(yieldParam.List) > 0
  299. if needNewScope {
  300. st.newScope()
  301. for i := 0; i < len(yieldParam.List); i++ {
  302. p := &yieldParam.List[i]
  303. if p.Expression == nil {
  304. block.errorf("missing name for block parameter '%s'", blockParam.List[i].Identifier)
  305. }
  306. st.variables[p.Identifier] = st.evalPrimaryExpressionGroup(p.Expression)
  307. }
  308. for i := 0; i < len(blockParam.List); i++ {
  309. p := &blockParam.List[i]
  310. if _, found := st.variables[p.Identifier]; !found {
  311. if p.Expression == nil {
  312. st.variables[p.Identifier] = valueBoolFALSE
  313. } else {
  314. st.variables[p.Identifier] = st.evalPrimaryExpressionGroup(p.Expression)
  315. }
  316. }
  317. }
  318. }
  319. mycontent := st.content
  320. if content != nil {
  321. myscope := st.scope
  322. st.content = func(st *Runtime, expression Expression) {
  323. outscope := st.scope
  324. outcontent := st.content
  325. st.scope = myscope
  326. st.content = mycontent
  327. if expression != nil {
  328. context := st.context
  329. st.context = st.evalPrimaryExpressionGroup(expression)
  330. st.executeList(content)
  331. st.context = context
  332. } else {
  333. st.executeList(content)
  334. }
  335. st.scope = outscope
  336. st.content = outcontent
  337. }
  338. }
  339. if expression != nil {
  340. context := st.context
  341. st.context = st.evalPrimaryExpressionGroup(expression)
  342. st.executeList(block.List)
  343. st.context = context
  344. } else {
  345. st.executeList(block.List)
  346. }
  347. st.content = mycontent
  348. if needNewScope {
  349. st.releaseScope()
  350. }
  351. }
  352. func (st *Runtime) executeList(list *ListNode) (returnValue reflect.Value) {
  353. inNewScope := false // to use just one scope for multiple actions with variable declarations
  354. for i := 0; i < len(list.Nodes); i++ {
  355. node := list.Nodes[i]
  356. switch node.Type() {
  357. case NodeText:
  358. node := node.(*TextNode)
  359. _, err := st.Writer.Write(node.Text)
  360. if err != nil {
  361. node.error(err)
  362. }
  363. case NodeAction:
  364. node := node.(*ActionNode)
  365. if node.Set != nil {
  366. if node.Set.Let {
  367. if !inNewScope {
  368. st.newScope()
  369. inNewScope = true
  370. defer st.releaseScope()
  371. }
  372. st.executeLetList(node.Set)
  373. } else {
  374. st.executeSetList(node.Set)
  375. }
  376. }
  377. if node.Pipe != nil {
  378. v, safeWriter := st.evalPipelineExpression(node.Pipe)
  379. if !safeWriter && v.IsValid() {
  380. if v.Type().Implements(rendererType) {
  381. v.Interface().(Renderer).Render(st)
  382. } else {
  383. _, err := fastprinter.PrintValue(st.escapeeWriter, v)
  384. if err != nil {
  385. node.error(err)
  386. }
  387. }
  388. }
  389. }
  390. case NodeIf:
  391. node := node.(*IfNode)
  392. var isLet bool
  393. if node.Set != nil {
  394. if node.Set.Let {
  395. isLet = true
  396. st.newScope()
  397. st.executeLetList(node.Set)
  398. } else {
  399. st.executeSetList(node.Set)
  400. }
  401. }
  402. if isTrue(st.evalPrimaryExpressionGroup(node.Expression)) {
  403. returnValue = st.executeList(node.List)
  404. } else if node.ElseList != nil {
  405. returnValue = st.executeList(node.ElseList)
  406. }
  407. if isLet {
  408. st.releaseScope()
  409. }
  410. case NodeRange:
  411. node := node.(*RangeNode)
  412. var expression reflect.Value
  413. isSet := node.Set != nil
  414. isLet := false
  415. keyVarSlot := 0
  416. valVarSlot := -1
  417. context := st.context
  418. if isSet {
  419. if len(node.Set.Left) > 1 {
  420. valVarSlot = 1
  421. }
  422. expression = st.evalPrimaryExpressionGroup(node.Set.Right[0])
  423. if node.Set.Let {
  424. isLet = true
  425. st.newScope()
  426. }
  427. } else {
  428. expression = st.evalPrimaryExpressionGroup(node.Expression)
  429. }
  430. ranger, cleanup, err := getRanger(expression)
  431. if err != nil {
  432. node.error(err)
  433. }
  434. if !ranger.ProvidesIndex() {
  435. if isSet && len(node.Set.Left) > 1 {
  436. // two-vars assignment with ranger that doesn't provide an index
  437. node.error(errors.New("two-var range over ranger that does not provide an index"))
  438. } else if isSet {
  439. keyVarSlot, valVarSlot = -1, 0
  440. }
  441. }
  442. indexValue, rangeValue, end := ranger.Range()
  443. if !end {
  444. for !end && !returnValue.IsValid() {
  445. if isSet {
  446. if isLet {
  447. if keyVarSlot >= 0 {
  448. st.variables[node.Set.Left[keyVarSlot].String()] = indexValue
  449. }
  450. if valVarSlot >= 0 {
  451. st.variables[node.Set.Left[valVarSlot].String()] = rangeValue
  452. }
  453. } else {
  454. if keyVarSlot >= 0 {
  455. st.executeSet(node.Set.Left[keyVarSlot], indexValue)
  456. }
  457. if valVarSlot >= 0 {
  458. st.executeSet(node.Set.Left[valVarSlot], rangeValue)
  459. }
  460. }
  461. }
  462. if valVarSlot < 0 {
  463. st.context = rangeValue
  464. }
  465. returnValue = st.executeList(node.List)
  466. indexValue, rangeValue, end = ranger.Range()
  467. }
  468. } else if node.ElseList != nil {
  469. returnValue = st.executeList(node.ElseList)
  470. }
  471. cleanup()
  472. st.context = context
  473. if isLet {
  474. st.releaseScope()
  475. }
  476. case NodeTry:
  477. node := node.(*TryNode)
  478. returnValue = st.executeTry(node)
  479. case NodeYield:
  480. node := node.(*YieldNode)
  481. if node.IsContent {
  482. if st.content != nil {
  483. st.content(st, node.Expression)
  484. }
  485. } else {
  486. block, has := st.getBlock(node.Name)
  487. if has == false || block == nil {
  488. node.errorf("unresolved block %q!!", node.Name)
  489. }
  490. st.executeYieldBlock(block, block.Parameters, node.Parameters, node.Expression, node.Content)
  491. }
  492. case NodeBlock:
  493. node := node.(*BlockNode)
  494. block, has := st.getBlock(node.Name)
  495. if has == false {
  496. block = node
  497. }
  498. st.executeYieldBlock(block, block.Parameters, block.Parameters, block.Expression, block.Content)
  499. case NodeInclude:
  500. node := node.(*IncludeNode)
  501. returnValue = st.executeInclude(node)
  502. case NodeReturn:
  503. node := node.(*ReturnNode)
  504. returnValue = st.evalPrimaryExpressionGroup(node.Value)
  505. }
  506. }
  507. return returnValue
  508. }
  509. func (st *Runtime) executeTry(try *TryNode) (returnValue reflect.Value) {
  510. writer := st.Writer
  511. buf := new(bytes.Buffer)
  512. defer func() {
  513. r := recover()
  514. // copy buffered render output to writer only if no panic occured
  515. if r == nil {
  516. io.Copy(writer, buf)
  517. } else {
  518. // st.Writer is already set to its original value since the later defer ran first
  519. if try.Catch != nil {
  520. if try.Catch.Err != nil {
  521. st.newScope()
  522. st.scope.variables[try.Catch.Err.Ident] = reflect.ValueOf(r)
  523. }
  524. if try.Catch.List != nil {
  525. returnValue = st.executeList(try.Catch.List)
  526. }
  527. if try.Catch.Err != nil {
  528. st.releaseScope()
  529. }
  530. }
  531. }
  532. }()
  533. st.Writer = buf
  534. defer func() { st.Writer = writer }()
  535. return st.executeList(try.List)
  536. }
  537. func (st *Runtime) executeInclude(node *IncludeNode) (returnValue reflect.Value) {
  538. var templatePath string
  539. name := st.evalPrimaryExpressionGroup(node.Name)
  540. if !name.IsValid() {
  541. node.errorf("evaluating name of template to include: name is not a valid value")
  542. }
  543. if name.Type().Implements(stringerType) {
  544. templatePath = name.String()
  545. } else if name.Kind() == reflect.String {
  546. templatePath = name.String()
  547. } else {
  548. node.errorf("evaluating name of template to include: unexpected expression type %q", getTypeString(name))
  549. }
  550. t, err := st.set.getSiblingTemplate(templatePath, node.TemplatePath, true)
  551. if err != nil {
  552. node.error(err)
  553. return reflect.Value{}
  554. }
  555. st.newScope()
  556. defer st.releaseScope()
  557. st.blocks = t.processedBlocks
  558. var context reflect.Value
  559. if node.Context != nil {
  560. context = st.context
  561. defer func() { st.context = context }()
  562. st.context = st.evalPrimaryExpressionGroup(node.Context)
  563. }
  564. Root := t.Root
  565. for t.extends != nil {
  566. t = t.extends
  567. Root = t.Root
  568. }
  569. return st.executeList(Root)
  570. }
  571. var (
  572. valueBoolTRUE = reflect.ValueOf(true)
  573. valueBoolFALSE = reflect.ValueOf(false)
  574. )
  575. func (st *Runtime) evalPrimaryExpressionGroup(node Expression) reflect.Value {
  576. switch node.Type() {
  577. case NodeAdditiveExpr:
  578. return st.evalAdditiveExpression(node.(*AdditiveExprNode))
  579. case NodeMultiplicativeExpr:
  580. return st.evalMultiplicativeExpression(node.(*MultiplicativeExprNode))
  581. case NodeComparativeExpr:
  582. return st.evalComparativeExpression(node.(*ComparativeExprNode))
  583. case NodeNumericComparativeExpr:
  584. return st.evalNumericComparativeExpression(node.(*NumericComparativeExprNode))
  585. case NodeLogicalExpr:
  586. return st.evalLogicalExpression(node.(*LogicalExprNode))
  587. case NodeNotExpr:
  588. return reflect.ValueOf(!isTrue(st.evalPrimaryExpressionGroup(node.(*NotExprNode).Expr)))
  589. case NodeTernaryExpr:
  590. node := node.(*TernaryExprNode)
  591. if isTrue(st.evalPrimaryExpressionGroup(node.Boolean)) {
  592. return st.evalPrimaryExpressionGroup(node.Left)
  593. }
  594. return st.evalPrimaryExpressionGroup(node.Right)
  595. case NodeCallExpr:
  596. node := node.(*CallExprNode)
  597. baseExpr := st.evalBaseExpressionGroup(node.BaseExpr)
  598. if baseExpr.Kind() != reflect.Func {
  599. node.errorf("node %q is not func kind %q", node.BaseExpr, baseExpr.Type())
  600. }
  601. ret, err := st.evalCallExpression(baseExpr, node.CallArgs)
  602. if err != nil {
  603. node.error(err)
  604. }
  605. return ret
  606. case NodeIndexExpr:
  607. node := node.(*IndexExprNode)
  608. base := st.evalPrimaryExpressionGroup(node.Base)
  609. index := st.evalPrimaryExpressionGroup(node.Index)
  610. resolved, err := resolveIndex(base, index, "")
  611. if err != nil {
  612. node.error(err)
  613. }
  614. return resolved
  615. case NodeSliceExpr:
  616. node := node.(*SliceExprNode)
  617. baseExpression := st.evalPrimaryExpressionGroup(node.Base)
  618. var index, length int
  619. if node.Index != nil {
  620. indexExpression := st.evalPrimaryExpressionGroup(node.Index)
  621. if canNumber(indexExpression.Kind()) {
  622. index = int(castInt64(indexExpression))
  623. } else {
  624. node.Index.errorf("non numeric value in index expression kind %s", indexExpression.Kind().String())
  625. }
  626. }
  627. if node.EndIndex != nil {
  628. indexExpression := st.evalPrimaryExpressionGroup(node.EndIndex)
  629. if canNumber(indexExpression.Kind()) {
  630. length = int(castInt64(indexExpression))
  631. } else {
  632. node.EndIndex.errorf("non numeric value in index expression kind %s", indexExpression.Kind().String())
  633. }
  634. } else {
  635. length = baseExpression.Len()
  636. }
  637. return baseExpression.Slice(index, length)
  638. }
  639. return st.evalBaseExpressionGroup(node)
  640. }
  641. // notNil returns false when v.IsValid() == false
  642. // or when v's kind can be nil and v.IsNil() == true
  643. func notNil(v reflect.Value) bool {
  644. if !v.IsValid() {
  645. return false
  646. }
  647. switch v.Kind() {
  648. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  649. return !v.IsNil()
  650. default:
  651. return true
  652. }
  653. }
  654. func (st *Runtime) isSet(node Node) (ok bool) {
  655. defer func() {
  656. if r := recover(); r != nil {
  657. // something panicked while evaluating node
  658. ok = false
  659. }
  660. }()
  661. nodeType := node.Type()
  662. switch nodeType {
  663. case NodeIndexExpr:
  664. node := node.(*IndexExprNode)
  665. if !st.isSet(node.Base) || !st.isSet(node.Index) {
  666. return false
  667. }
  668. base := st.evalPrimaryExpressionGroup(node.Base)
  669. index := st.evalPrimaryExpressionGroup(node.Index)
  670. resolved, err := resolveIndex(base, index, "")
  671. return err == nil && notNil(resolved)
  672. case NodeIdentifier:
  673. value, err := st.resolve(node.String())
  674. return err == nil && notNil(value)
  675. case NodeField:
  676. node := node.(*FieldNode)
  677. resolved := st.context
  678. for i := 0; i < len(node.Ident); i++ {
  679. var err error
  680. resolved, err = resolveIndex(resolved, reflect.Value{}, node.Ident[i])
  681. if err != nil || !notNil(resolved) {
  682. return false
  683. }
  684. }
  685. case NodeChain:
  686. node := node.(*ChainNode)
  687. resolved, err := st.evalChainNodeExpression(node)
  688. return err == nil && notNil(resolved)
  689. default:
  690. //todo: maybe work some edge cases
  691. if !(nodeType > beginExpressions && nodeType < endExpressions) {
  692. node.errorf("unexpected %q node in isset clause", node)
  693. }
  694. }
  695. return true
  696. }
  697. func (st *Runtime) evalNumericComparativeExpression(node *NumericComparativeExprNode) reflect.Value {
  698. left, right := st.evalPrimaryExpressionGroup(node.Left), st.evalPrimaryExpressionGroup(node.Right)
  699. isTrue := false
  700. kind := left.Kind()
  701. // if the left value is not a float and the right is, we need to promote the left value to a float before the calculation
  702. // this is necessary for expressions like 4*1.23
  703. needFloatPromotion := !isFloat(kind) && isFloat(right.Kind())
  704. switch node.Operator.typ {
  705. case itemGreat:
  706. if isInt(kind) {
  707. if needFloatPromotion {
  708. isTrue = float64(left.Int()) > right.Float()
  709. } else {
  710. isTrue = left.Int() > toInt(right)
  711. }
  712. } else if isFloat(kind) {
  713. isTrue = left.Float() > toFloat(right)
  714. } else if isUint(kind) {
  715. if needFloatPromotion {
  716. isTrue = float64(left.Uint()) > right.Float()
  717. } else {
  718. isTrue = left.Uint() > toUint(right)
  719. }
  720. } else {
  721. node.Left.errorf("a non numeric value in numeric comparative expression")
  722. }
  723. case itemGreatEquals:
  724. if isInt(kind) {
  725. if needFloatPromotion {
  726. isTrue = float64(left.Int()) >= right.Float()
  727. } else {
  728. isTrue = left.Int() >= toInt(right)
  729. }
  730. } else if isFloat(kind) {
  731. isTrue = left.Float() >= toFloat(right)
  732. } else if isUint(kind) {
  733. if needFloatPromotion {
  734. isTrue = float64(left.Uint()) >= right.Float()
  735. } else {
  736. isTrue = left.Uint() >= toUint(right)
  737. }
  738. } else {
  739. node.Left.errorf("a non numeric value in numeric comparative expression")
  740. }
  741. case itemLess:
  742. if isInt(kind) {
  743. if needFloatPromotion {
  744. isTrue = float64(left.Int()) < right.Float()
  745. } else {
  746. isTrue = left.Int() < toInt(right)
  747. }
  748. } else if isFloat(kind) {
  749. isTrue = left.Float() < toFloat(right)
  750. } else if isUint(kind) {
  751. if needFloatPromotion {
  752. isTrue = float64(left.Uint()) < right.Float()
  753. } else {
  754. isTrue = left.Uint() < toUint(right)
  755. }
  756. } else {
  757. node.Left.errorf("a non numeric value in numeric comparative expression")
  758. }
  759. case itemLessEquals:
  760. if isInt(kind) {
  761. if needFloatPromotion {
  762. isTrue = float64(left.Int()) <= right.Float()
  763. } else {
  764. isTrue = left.Int() <= toInt(right)
  765. }
  766. } else if isFloat(kind) {
  767. isTrue = left.Float() <= toFloat(right)
  768. } else if isUint(kind) {
  769. if needFloatPromotion {
  770. isTrue = float64(left.Uint()) <= right.Float()
  771. } else {
  772. isTrue = left.Uint() <= toUint(right)
  773. }
  774. } else {
  775. node.Left.errorf("a non numeric value in numeric comparative expression")
  776. }
  777. }
  778. return reflect.ValueOf(isTrue)
  779. }
  780. func (st *Runtime) evalLogicalExpression(node *LogicalExprNode) reflect.Value {
  781. truthy := isTrue(st.evalPrimaryExpressionGroup(node.Left))
  782. if node.Operator.typ == itemAnd {
  783. truthy = truthy && isTrue(st.evalPrimaryExpressionGroup(node.Right))
  784. } else {
  785. truthy = truthy || isTrue(st.evalPrimaryExpressionGroup(node.Right))
  786. }
  787. return reflect.ValueOf(truthy)
  788. }
  789. func (st *Runtime) evalComparativeExpression(node *ComparativeExprNode) reflect.Value {
  790. left, right := st.evalPrimaryExpressionGroup(node.Left), st.evalPrimaryExpressionGroup(node.Right)
  791. equal := checkEquality(left, right)
  792. if node.Operator.typ == itemNotEquals {
  793. return reflect.ValueOf(!equal)
  794. }
  795. return reflect.ValueOf(equal)
  796. }
  797. func toInt(v reflect.Value) int64 {
  798. if !v.IsValid() {
  799. panic(fmt.Errorf("invalid value can't be converted to int64"))
  800. }
  801. kind := v.Kind()
  802. if isInt(kind) {
  803. return v.Int()
  804. } else if isFloat(kind) {
  805. return int64(v.Float())
  806. } else if isUint(kind) {
  807. return int64(v.Uint())
  808. } else if kind == reflect.String {
  809. n, e := strconv.ParseInt(v.String(), 10, 0)
  810. if e != nil {
  811. panic(e)
  812. }
  813. return n
  814. } else if kind == reflect.Bool {
  815. if v.Bool() {
  816. return 0
  817. }
  818. return 1
  819. }
  820. panic(fmt.Errorf("type: %q can't be converted to int64", v.Type()))
  821. }
  822. func toUint(v reflect.Value) uint64 {
  823. if !v.IsValid() {
  824. panic(fmt.Errorf("invalid value can't be converted to uint64"))
  825. }
  826. kind := v.Kind()
  827. if isUint(kind) {
  828. return v.Uint()
  829. } else if isInt(kind) {
  830. return uint64(v.Int())
  831. } else if isFloat(kind) {
  832. return uint64(v.Float())
  833. } else if kind == reflect.String {
  834. n, e := strconv.ParseUint(v.String(), 10, 0)
  835. if e != nil {
  836. panic(e)
  837. }
  838. return n
  839. } else if kind == reflect.Bool {
  840. if v.Bool() {
  841. return 0
  842. }
  843. return 1
  844. }
  845. panic(fmt.Errorf("type: %q can't be converted to uint64", v.Type()))
  846. }
  847. func toFloat(v reflect.Value) float64 {
  848. if !v.IsValid() {
  849. panic(fmt.Errorf("invalid value can't be converted to float64"))
  850. }
  851. kind := v.Kind()
  852. if isFloat(kind) {
  853. return v.Float()
  854. } else if isInt(kind) {
  855. return float64(v.Int())
  856. } else if isUint(kind) {
  857. return float64(v.Uint())
  858. } else if kind == reflect.String {
  859. n, e := strconv.ParseFloat(v.String(), 0)
  860. if e != nil {
  861. panic(e)
  862. }
  863. return n
  864. } else if kind == reflect.Bool {
  865. if v.Bool() {
  866. return 0
  867. }
  868. return 1
  869. }
  870. panic(fmt.Errorf("type: %q can't be converted to float64", v.Type()))
  871. }
  872. func (st *Runtime) evalMultiplicativeExpression(node *MultiplicativeExprNode) reflect.Value {
  873. left, right := st.evalPrimaryExpressionGroup(node.Left), st.evalPrimaryExpressionGroup(node.Right)
  874. kind := left.Kind()
  875. // if the left value is not a float and the right is, we need to promote the left value to a float before the calculation
  876. // this is necessary for expressions like 4*1.23
  877. needFloatPromotion := !isFloat(kind) && isFloat(right.Kind())
  878. switch node.Operator.typ {
  879. case itemMul:
  880. if isInt(kind) {
  881. if needFloatPromotion {
  882. // do the promotion and calculates
  883. left = reflect.ValueOf(float64(left.Int()) * right.Float())
  884. } else {
  885. // do not need float promotion
  886. left = reflect.ValueOf(left.Int() * toInt(right))
  887. }
  888. } else if isFloat(kind) {
  889. left = reflect.ValueOf(left.Float() * toFloat(right))
  890. } else if isUint(kind) {
  891. if needFloatPromotion {
  892. left = reflect.ValueOf(float64(left.Uint()) * right.Float())
  893. } else {
  894. left = reflect.ValueOf(left.Uint() * toUint(right))
  895. }
  896. } else {
  897. node.Left.errorf("a non numeric value in multiplicative expression")
  898. }
  899. case itemDiv:
  900. if isInt(kind) {
  901. if needFloatPromotion {
  902. left = reflect.ValueOf(float64(left.Int()) / right.Float())
  903. } else {
  904. left = reflect.ValueOf(left.Int() / toInt(right))
  905. }
  906. } else if isFloat(kind) {
  907. left = reflect.ValueOf(left.Float() / toFloat(right))
  908. } else if isUint(kind) {
  909. if needFloatPromotion {
  910. left = reflect.ValueOf(float64(left.Uint()) / right.Float())
  911. } else {
  912. left = reflect.ValueOf(left.Uint() / toUint(right))
  913. }
  914. } else {
  915. node.Left.errorf("a non numeric value in multiplicative expression")
  916. }
  917. case itemMod:
  918. if isInt(kind) {
  919. left = reflect.ValueOf(left.Int() % toInt(right))
  920. } else if isFloat(kind) {
  921. left = reflect.ValueOf(int64(left.Float()) % toInt(right))
  922. } else if isUint(kind) {
  923. left = reflect.ValueOf(left.Uint() % toUint(right))
  924. } else {
  925. node.Left.errorf("a non numeric value in multiplicative expression")
  926. }
  927. }
  928. return left
  929. }
  930. func (st *Runtime) evalAdditiveExpression(node *AdditiveExprNode) reflect.Value {
  931. isAdditive := node.Operator.typ == itemAdd
  932. if node.Left == nil {
  933. right := st.evalPrimaryExpressionGroup(node.Right)
  934. if !right.IsValid() {
  935. node.errorf("right side of additive expression is invalid value")
  936. }
  937. kind := right.Kind()
  938. // todo: optimize
  939. if isInt(kind) {
  940. if isAdditive {
  941. return reflect.ValueOf(+right.Int())
  942. } else {
  943. return reflect.ValueOf(-right.Int())
  944. }
  945. } else if isUint(kind) {
  946. if isAdditive {
  947. return right
  948. } else {
  949. return reflect.ValueOf(-int64(right.Uint()))
  950. }
  951. } else if isFloat(kind) {
  952. if isAdditive {
  953. return reflect.ValueOf(+right.Float())
  954. } else {
  955. return reflect.ValueOf(-right.Float())
  956. }
  957. }
  958. node.Left.errorf("additive expression: right side %s (%s) is not a numeric value (no left side)", node.Right, getTypeString(right))
  959. }
  960. left, right := st.evalPrimaryExpressionGroup(node.Left), st.evalPrimaryExpressionGroup(node.Right)
  961. if !left.IsValid() {
  962. node.errorf("left side of additive expression is invalid value")
  963. }
  964. if !right.IsValid() {
  965. node.errorf("right side of additive expression is invalid value")
  966. }
  967. kind := left.Kind()
  968. // if the left value is not a float and the right is, we need to promote the left value to a float before the calculation
  969. // this is necessary for expressions like 4+1.23
  970. needFloatPromotion := !isFloat(kind) && kind != reflect.String && isFloat(right.Kind())
  971. if needFloatPromotion {
  972. if isInt(kind) {
  973. if isAdditive {
  974. left = reflect.ValueOf(float64(left.Int()) + right.Float())
  975. } else {
  976. left = reflect.ValueOf(float64(left.Int()) - right.Float())
  977. }
  978. } else if isUint(kind) {
  979. if isAdditive {
  980. left = reflect.ValueOf(float64(left.Uint()) + right.Float())
  981. } else {
  982. left = reflect.ValueOf(float64(left.Uint()) - right.Float())
  983. }
  984. } else {
  985. node.Left.errorf("additive expression: left side (%s (%s) needs float promotion but neither int nor uint)", node.Left, getTypeString(left))
  986. }
  987. } else {
  988. if isInt(kind) {
  989. if isAdditive {
  990. left = reflect.ValueOf(left.Int() + toInt(right))
  991. } else {
  992. left = reflect.ValueOf(left.Int() - toInt(right))
  993. }
  994. } else if isFloat(kind) {
  995. if isAdditive {
  996. left = reflect.ValueOf(left.Float() + toFloat(right))
  997. } else {
  998. left = reflect.ValueOf(left.Float() - toFloat(right))
  999. }
  1000. } else if isUint(kind) {
  1001. if isAdditive {
  1002. left = reflect.ValueOf(left.Uint() + toUint(right))
  1003. } else {
  1004. left = reflect.ValueOf(left.Uint() - toUint(right))
  1005. }
  1006. } else if kind == reflect.String {
  1007. if !isAdditive {
  1008. node.Right.errorf("minus signal is not allowed with strings")
  1009. }
  1010. // converts []byte (and alias types of []byte) to string
  1011. if right.Kind() == reflect.Slice && right.Type().Elem().Kind() == reflect.Uint8 {
  1012. right = right.Convert(left.Type())
  1013. }
  1014. left = reflect.ValueOf(left.String() + fmt.Sprint(right))
  1015. } else {
  1016. node.Left.errorf("additive expression: left side %s (%s) is not a numeric value", node.Left, getTypeString(left))
  1017. }
  1018. }
  1019. return left
  1020. }
  1021. func getTypeString(value reflect.Value) string {
  1022. if value.IsValid() {
  1023. return value.Type().String()
  1024. }
  1025. return "<invalid>"
  1026. }
  1027. func (st *Runtime) evalBaseExpressionGroup(node Node) reflect.Value {
  1028. switch node.Type() {
  1029. case NodeNil:
  1030. return reflect.ValueOf(nil)
  1031. case NodeBool:
  1032. if node.(*BoolNode).True {
  1033. return valueBoolTRUE
  1034. }
  1035. return valueBoolFALSE
  1036. case NodeString:
  1037. return reflect.ValueOf(&node.(*StringNode).Text).Elem()
  1038. case NodeIdentifier:
  1039. resolved, err := st.resolve(node.(*IdentifierNode).Ident)
  1040. if err != nil {
  1041. node.error(err)
  1042. }
  1043. return resolved
  1044. case NodeField:
  1045. node := node.(*FieldNode)
  1046. resolved := st.context
  1047. for i := 0; i < len(node.Ident); i++ {
  1048. field, err := resolveIndex(resolved, reflect.Value{}, node.Ident[i])
  1049. if err != nil {
  1050. node.errorf("%v", err)
  1051. }
  1052. if !field.IsValid() {
  1053. node.errorf("there is no field or method '%s' in %s (.%s)", node.Ident[i], getTypeString(resolved), strings.Join(node.Ident, "."))
  1054. }
  1055. resolved = field
  1056. }
  1057. return resolved
  1058. case NodeChain:
  1059. resolved, err := st.evalChainNodeExpression(node.(*ChainNode))
  1060. if err != nil {
  1061. node.error(err)
  1062. }
  1063. return resolved
  1064. case NodeNumber:
  1065. node := node.(*NumberNode)
  1066. if node.IsFloat {
  1067. return reflect.ValueOf(&node.Float64).Elem()
  1068. }
  1069. if node.IsInt {
  1070. return reflect.ValueOf(&node.Int64).Elem()
  1071. }
  1072. if node.IsUint {
  1073. return reflect.ValueOf(&node.Uint64).Elem()
  1074. }
  1075. }
  1076. node.errorf("unexpected node type %s in unary expression evaluating", node)
  1077. return reflect.Value{}
  1078. }
  1079. func (st *Runtime) evalCallExpression(baseExpr reflect.Value, args CallArgs) (reflect.Value, error) {
  1080. return st.evalPipeCallExpression(baseExpr, args, nil)
  1081. }
  1082. func (st *Runtime) evalPipeCallExpression(baseExpr reflect.Value, args CallArgs, pipedArg *reflect.Value) (reflect.Value, error) {
  1083. if !baseExpr.IsValid() {
  1084. return reflect.Value{}, errors.New("base of call expression is invalid value")
  1085. }
  1086. if funcType.AssignableTo(baseExpr.Type()) {
  1087. return baseExpr.Interface().(Func)(Arguments{runtime: st, args: args, pipedVal: pipedArg}), nil
  1088. }
  1089. argValues, err := st.evaluateArgs(baseExpr.Type(), args, pipedArg)
  1090. if err != nil {
  1091. return reflect.Value{}, fmt.Errorf("call expression: %v", err)
  1092. }
  1093. var returns = baseExpr.Call(argValues)
  1094. if len(returns) == 0 {
  1095. return reflect.Value{}, nil
  1096. }
  1097. return returns[0], nil
  1098. }
  1099. func (st *Runtime) evalCommandExpression(node *CommandNode) (reflect.Value, bool) {
  1100. term := st.evalPrimaryExpressionGroup(node.BaseExpr)
  1101. if term.IsValid() && node.Exprs != nil {
  1102. if term.Kind() == reflect.Func {
  1103. if term.Type() == safeWriterType {
  1104. st.evalSafeWriter(term, node)
  1105. return reflect.Value{}, true
  1106. }
  1107. ret, err := st.evalCallExpression(term, node.CallArgs)
  1108. if err != nil {
  1109. node.BaseExpr.error(err)
  1110. }
  1111. return ret, false
  1112. }
  1113. node.Exprs[0].errorf("command %q has arguments but is %s, not a function", node.Exprs[0], term.Type())
  1114. }
  1115. return term, false
  1116. }
  1117. func (st *Runtime) evalChainNodeExpression(node *ChainNode) (reflect.Value, error) {
  1118. resolved := st.evalPrimaryExpressionGroup(node.Node)
  1119. for i := 0; i < len(node.Field); i++ {
  1120. field, err := resolveIndex(resolved, reflect.Value{}, node.Field[i])
  1121. if err != nil {
  1122. return reflect.Value{}, err
  1123. }
  1124. if !field.IsValid() {
  1125. if resolved.Kind() == reflect.Map && i == len(node.Field)-1 {
  1126. // return reflect.Zero(resolved.Type().Elem()), nil
  1127. return reflect.Value{}, nil
  1128. }
  1129. return reflect.Value{}, fmt.Errorf("there is no field or method '%s' in %s (%s)", node.Field[i], getTypeString(resolved), node)
  1130. }
  1131. resolved = field
  1132. }
  1133. return resolved, nil
  1134. }
  1135. type escapeWriter struct {
  1136. rawWriter io.Writer
  1137. safeWriter SafeWriter
  1138. }
  1139. func (w *escapeWriter) Write(b []byte) (int, error) {
  1140. w.safeWriter(w.rawWriter, b)
  1141. return 0, nil
  1142. }
  1143. func (st *Runtime) evalSafeWriter(term reflect.Value, node *CommandNode, v ...reflect.Value) {
  1144. sw := &escapeWriter{rawWriter: st.Writer, safeWriter: term.Interface().(SafeWriter)}
  1145. for i := 0; i < len(v); i++ {
  1146. fastprinter.PrintValue(sw, v[i])
  1147. }
  1148. for i := 0; i < len(node.Exprs); i++ {
  1149. fastprinter.PrintValue(sw, st.evalPrimaryExpressionGroup(node.Exprs[i]))
  1150. }
  1151. }
  1152. func (st *Runtime) evalCommandPipeExpression(node *CommandNode, value reflect.Value) (reflect.Value, bool) {
  1153. term := st.evalPrimaryExpressionGroup(node.BaseExpr)
  1154. if !term.IsValid() {
  1155. node.errorf("base expression of command pipe node is invalid value")
  1156. }
  1157. if term.Kind() != reflect.Func {
  1158. node.BaseExpr.errorf("pipe command %q must be a function, but is %s", node.BaseExpr, term.Type())
  1159. }
  1160. if term.Type() == safeWriterType {
  1161. st.evalSafeWriter(term, node, value)
  1162. return reflect.Value{}, true
  1163. }
  1164. ret, err := st.evalPipeCallExpression(term, node.CallArgs, &value)
  1165. if err != nil {
  1166. node.BaseExpr.error(err)
  1167. }
  1168. return ret, false
  1169. }
  1170. func (st *Runtime) evalPipelineExpression(node *PipeNode) (value reflect.Value, safeWriter bool) {
  1171. value, safeWriter = st.evalCommandExpression(node.Cmds[0])
  1172. for i := 1; i < len(node.Cmds); i++ {
  1173. if safeWriter {
  1174. node.Cmds[i].errorf("unexpected command %s, writer command should be the last command", node.Cmds[i])
  1175. }
  1176. value, safeWriter = st.evalCommandPipeExpression(node.Cmds[i], value)
  1177. }
  1178. return
  1179. }
  1180. func (st *Runtime) evaluateArgs(fnType reflect.Type, args CallArgs, pipedArg *reflect.Value) ([]reflect.Value, error) {
  1181. numArgs := len(args.Exprs)
  1182. if !args.HasPipeSlot && pipedArg != nil {
  1183. numArgs++
  1184. }
  1185. numArgsRequired := fnType.NumIn()
  1186. isVariadic := fnType.IsVariadic()
  1187. if isVariadic {
  1188. numArgsRequired--
  1189. if numArgs < numArgsRequired {
  1190. return nil, fmt.Errorf("%s needs at least %d arguments, but have %d", fnType, numArgsRequired, numArgs)
  1191. }
  1192. } else {
  1193. if numArgs != numArgsRequired {
  1194. return nil, fmt.Errorf("%s needs %d arguments, but have %d", fnType, numArgsRequired, numArgs)
  1195. }
  1196. }
  1197. argValues := make([]reflect.Value, numArgs)
  1198. slot := 0 // index in argument values (evaluated expressions combined with piped argument if applicable)
  1199. if !args.HasPipeSlot && pipedArg != nil {
  1200. in := fnType.In(slot)
  1201. if !(*pipedArg).IsValid() {
  1202. return nil, fmt.Errorf("piped first argument for %s is not a valid value", fnType)
  1203. }
  1204. if !(*pipedArg).Type().AssignableTo(in) {
  1205. *pipedArg = (*pipedArg).Convert(in)
  1206. }
  1207. argValues[slot] = *pipedArg
  1208. slot++
  1209. }
  1210. i := 0 // index in parsed argument expression list
  1211. for slot < numArgsRequired {
  1212. in := fnType.In(slot)
  1213. var term reflect.Value
  1214. if args.Exprs[i].Type() == NodeUnderscore {
  1215. term = *pipedArg
  1216. } else {
  1217. term = st.evalPrimaryExpressionGroup(args.Exprs[i])
  1218. }
  1219. if !term.IsValid() {
  1220. return nil, fmt.Errorf("argument for position %d in %s is not a valid value", slot, fnType)
  1221. }
  1222. if !term.Type().AssignableTo(in) {
  1223. term = term.Convert(in)
  1224. }
  1225. argValues[slot] = term
  1226. i++
  1227. slot++
  1228. }
  1229. if isVariadic {
  1230. in := fnType.In(numArgsRequired).Elem()
  1231. for i < len(args.Exprs) {
  1232. var term reflect.Value
  1233. if args.Exprs[i].Type() == NodeUnderscore {
  1234. term = *pipedArg
  1235. } else {
  1236. term = st.evalPrimaryExpressionGroup(args.Exprs[i])
  1237. }
  1238. if !term.IsValid() {
  1239. return nil, fmt.Errorf("argument for position %d in %s is not a valid value", slot, fnType)
  1240. }
  1241. if !term.Type().AssignableTo(in) {
  1242. term = term.Convert(in)
  1243. }
  1244. argValues[slot] = term
  1245. i++
  1246. slot++
  1247. }
  1248. }
  1249. return argValues, nil
  1250. }
  1251. func isUint(kind reflect.Kind) bool {
  1252. return kind >= reflect.Uint && kind <= reflect.Uint64
  1253. }
  1254. func isInt(kind reflect.Kind) bool {
  1255. return kind >= reflect.Int && kind <= reflect.Int64
  1256. }
  1257. func isFloat(kind reflect.Kind) bool {
  1258. return kind == reflect.Float32 || kind == reflect.Float64
  1259. }
  1260. // checkEquality of two reflect values in the semantic of the jet runtime
  1261. func checkEquality(v1, v2 reflect.Value) bool {
  1262. v1 = indirectInterface(v1)
  1263. v2 = indirectInterface(v2)
  1264. if !v1.IsValid() || !v2.IsValid() {
  1265. return v1.IsValid() == v2.IsValid()
  1266. }
  1267. v1Type := v1.Type()
  1268. v2Type := v2.Type()
  1269. // fast path
  1270. if v1Type != v2Type && !v2Type.AssignableTo(v1Type) && !v2Type.ConvertibleTo(v1Type) {
  1271. return false
  1272. }
  1273. kind := v1.Kind()
  1274. if isInt(kind) {
  1275. return v1.Int() == toInt(v2)
  1276. }
  1277. if isFloat(kind) {
  1278. return v1.Float() == toFloat(v2)
  1279. }
  1280. if isUint(kind) {
  1281. return v1.Uint() == toUint(v2)
  1282. }
  1283. switch kind {
  1284. case reflect.Bool:
  1285. return v1.Bool() == isTrue(v2)
  1286. case reflect.String:
  1287. return v1.String() == v2.String()
  1288. case reflect.Array:
  1289. vlen := v1.Len()
  1290. if vlen == v2.Len() {
  1291. return false
  1292. }
  1293. for i := 0; i < vlen; i++ {
  1294. if !checkEquality(v1.Index(i), v2.Index(i)) {
  1295. return false
  1296. }
  1297. }
  1298. return true
  1299. case reflect.Slice:
  1300. if v1.IsNil() != v2.IsNil() {
  1301. return false
  1302. }
  1303. vlen := v1.Len()
  1304. if vlen != v2.Len() {
  1305. return false
  1306. }
  1307. if v1.CanAddr() && v2.CanAddr() && v1.Pointer() == v2.Pointer() {
  1308. return true
  1309. }
  1310. for i := 0; i < vlen; i++ {
  1311. if !checkEquality(v1.Index(i), v2.Index(i)) {
  1312. return false
  1313. }
  1314. }
  1315. return true
  1316. case reflect.Interface:
  1317. if v1.IsNil() || v2.IsNil() {
  1318. return v1.IsNil() == v2.IsNil()
  1319. }
  1320. return checkEquality(v1.Elem(), v2.Elem())
  1321. case reflect.Ptr:
  1322. return v1.Pointer() == v2.Pointer()
  1323. case reflect.Struct:
  1324. numField := v1.NumField()
  1325. for i, n := 0, numField; i < n; i++ {
  1326. if !checkEquality(v1.Field(i), v2.Field(i)) {
  1327. return false
  1328. }
  1329. }
  1330. return true
  1331. case reflect.Map:
  1332. if v1.IsNil() != v2.IsNil() {
  1333. return false
  1334. }
  1335. if v1.Len() != v2.Len() {
  1336. return false
  1337. }
  1338. if v1.Pointer() == v2.Pointer() {
  1339. return true
  1340. }
  1341. for _, k := range v1.MapKeys() {
  1342. val1 := v1.MapIndex(k)
  1343. val2 := v2.MapIndex(k)
  1344. if !val1.IsValid() || !val2.IsValid() || !checkEquality(v1.MapIndex(k), v2.MapIndex(k)) {
  1345. return false
  1346. }
  1347. }
  1348. return true
  1349. case reflect.Func:
  1350. return v1.IsNil() && v2.IsNil()
  1351. default:
  1352. // Normal equality suffices
  1353. return v1.Interface() == v2.Interface()
  1354. }
  1355. }
  1356. func isTrue(v reflect.Value) bool {
  1357. return v.IsValid() && !v.IsZero()
  1358. }
  1359. func canNumber(kind reflect.Kind) bool {
  1360. return isInt(kind) || isUint(kind) || isFloat(kind)
  1361. }
  1362. func castInt64(v reflect.Value) int64 {
  1363. kind := v.Kind()
  1364. switch {
  1365. case isInt(kind):
  1366. return v.Int()
  1367. case isUint(kind):
  1368. return int64(v.Uint())
  1369. case isFloat(kind):
  1370. return int64(v.Float())
  1371. }
  1372. return 0
  1373. }
  1374. var cachedStructsMutex = sync.RWMutex{}
  1375. var cachedStructsFieldIndex = map[reflect.Type]map[string][]int{}
  1376. // from text/template's exec.go:
  1377. //
  1378. // indirect returns the item at the end of indirection, and a bool to indicate
  1379. // if it's nil. If the returned bool is true, the returned value's kind will be
  1380. // either a pointer or interface.
  1381. func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
  1382. for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
  1383. if v.IsNil() {
  1384. return v, true
  1385. }
  1386. }
  1387. return v, false
  1388. }
  1389. // indirectInterface returns the concrete value in an interface value, or else v itself.
  1390. // That is, if v represents the interface value x, the result is the same as reflect.ValueOf(x):
  1391. // the fact that x was an interface value is forgotten.
  1392. func indirectInterface(v reflect.Value) reflect.Value {
  1393. if v.Kind() == reflect.Interface {
  1394. return v.Elem()
  1395. }
  1396. return v
  1397. }
  1398. // indirectEface is the same as indirectInterface, but only indirects through v if its type
  1399. // is the empty interface and its value is not nil.
  1400. func indirectEface(v reflect.Value) reflect.Value {
  1401. if v.Kind() == reflect.Interface && v.Type().NumMethod() == 0 && !v.IsNil() {
  1402. return v.Elem()
  1403. }
  1404. return v
  1405. }
  1406. // mostly copied from text/template's evalField() (exec.go):
  1407. //
  1408. // The index to use to access v can be specified in either index or indexAsStr.
  1409. // Which parameter is filled depends on the call path up to when a particular
  1410. // call to resolveIndex is made and whether that call site already has access
  1411. // to a reflect.Value for the index or just a string identifier.
  1412. //
  1413. // While having both options makes the implementation of this function more
  1414. // complex, it improves the memory allocation story for the most common
  1415. // execution paths when executing a template, such as when accessing a field
  1416. // element.
  1417. func resolveIndex(v, index reflect.Value, indexAsStr string) (reflect.Value, error) {
  1418. if !v.IsValid() {
  1419. return reflect.Value{}, fmt.Errorf("there is no field or method '%s' in %s (%s)", index, v, getTypeString(v))
  1420. }
  1421. v, isNil := indirect(v)
  1422. if v.Kind() == reflect.Interface && isNil {
  1423. // Calling a method on a nil interface can't work. The
  1424. // MethodByName method call below would panic.
  1425. return reflect.Value{}, fmt.Errorf("nil pointer evaluating %s.%s", v.Type(), index)
  1426. }
  1427. // Handle the caller passing either index or indexAsStr.
  1428. indexIsStr := indexAsStr != ""
  1429. indexAsValue := func() reflect.Value { return index }
  1430. if indexIsStr {
  1431. // indexAsStr was specified, so make the indexAsValue function
  1432. // obtain the corresponding reflect.Value. This is only used in
  1433. // some code paths, and since it causes an allocation, a
  1434. // function is used instead of always extracting the
  1435. // reflect.Value.
  1436. indexAsValue = func() reflect.Value {
  1437. return reflect.ValueOf(indexAsStr)
  1438. }
  1439. } else {
  1440. // index was specified, so extract the string value if the index
  1441. // is in fact a string.
  1442. indexIsStr = index.Kind() == reflect.String
  1443. if indexIsStr {
  1444. indexAsStr = index.String()
  1445. }
  1446. }
  1447. // Unless it's an interface, need to get to a value of type *T to guarantee
  1448. // we see all methods of T and *T.
  1449. if indexIsStr {
  1450. ptr := v
  1451. if ptr.Kind() != reflect.Interface && ptr.Kind() != reflect.Ptr && ptr.CanAddr() {
  1452. ptr = ptr.Addr()
  1453. }
  1454. if method := ptr.MethodByName(indexAsStr); method.IsValid() {
  1455. return method, nil
  1456. }
  1457. }
  1458. // It's not a method on v; so now:
  1459. // - if v is array/slice/string, use index as numeric index
  1460. // - if v is a struct, use index as field name
  1461. // - if v is a map, use index as key
  1462. // - if v is (still) a pointer, indexing will fail but we check for nil to get a useful error
  1463. switch v.Kind() {
  1464. case reflect.Array, reflect.Slice, reflect.String:
  1465. indexVal := indexAsValue()
  1466. x, err := indexArg(indexVal, v.Len())
  1467. if err != nil {
  1468. return reflect.Value{}, err
  1469. }
  1470. return indirectEface(v.Index(x)), nil
  1471. case reflect.Struct:
  1472. if !indexIsStr {
  1473. return reflect.Value{}, fmt.Errorf("can't use %s (%s, not string) as field name in struct type %s", index, indexAsValue().Type(), v.Type())
  1474. }
  1475. typ := v.Type()
  1476. key := indexAsStr
  1477. // Fast path: use the struct cache to avoid allocations.
  1478. cachedStructsMutex.RLock()
  1479. cache, ok := cachedStructsFieldIndex[typ]
  1480. cachedStructsMutex.RUnlock()
  1481. if !ok {
  1482. cachedStructsMutex.Lock()
  1483. if cache, ok = cachedStructsFieldIndex[typ]; !ok {
  1484. cache = make(map[string][]int)
  1485. buildCache(typ, cache, nil)
  1486. cachedStructsFieldIndex[typ] = cache
  1487. }
  1488. cachedStructsMutex.Unlock()
  1489. }
  1490. if id, ok := cache[key]; ok {
  1491. return v.FieldByIndex(id), nil
  1492. }
  1493. // Slow path: use reflect directly
  1494. tField, ok := typ.FieldByName(key)
  1495. if ok {
  1496. field := v.FieldByIndex(tField.Index)
  1497. if tField.PkgPath != "" { // field is unexported
  1498. return reflect.Value{}, fmt.Errorf("%s is an unexported field of struct type %s", indexAsStr, v.Type())
  1499. }
  1500. return indirectEface(field), nil
  1501. }
  1502. return reflect.Value{}, fmt.Errorf("can't use %s as field name in struct type %s", indexAsStr, v.Type())
  1503. case reflect.Map:
  1504. // If it's a map, attempt to use the field name as a key.
  1505. indexVal := indexAsValue()
  1506. if !indexVal.Type().ConvertibleTo(v.Type().Key()) {
  1507. return reflect.Value{}, fmt.Errorf("can't use %s (%s) as key for map of type %s", indexAsStr, indexVal.Type(), v.Type())
  1508. }
  1509. index = indexVal.Convert(v.Type().Key()) // noop in most cases, but not expensive
  1510. return indirectEface(v.MapIndex(indexVal)), nil
  1511. case reflect.Ptr:
  1512. etyp := v.Type().Elem()
  1513. if etyp.Kind() == reflect.Struct && indexIsStr {
  1514. if _, ok := etyp.FieldByName(indexAsStr); !ok {
  1515. // If there's no such field, say "can't evaluate"
  1516. // instead of "nil pointer evaluating".
  1517. break
  1518. }
  1519. }
  1520. if isNil {
  1521. return reflect.Value{}, fmt.Errorf("nil pointer evaluating %s.%s", v.Type(), index)
  1522. }
  1523. }
  1524. return reflect.Value{}, fmt.Errorf("can't evaluate index %s (%s) in type %s", index, indexAsStr, getTypeString(v))
  1525. }
  1526. // from Go's text/template's funcs.go:
  1527. //
  1528. // indexArg checks if a reflect.Value can be used as an index, and converts it to int if possible.
  1529. func indexArg(index reflect.Value, cap int) (int, error) {
  1530. var x int64
  1531. switch index.Kind() {
  1532. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1533. x = index.Int()
  1534. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  1535. x = int64(index.Uint())
  1536. case reflect.Float32, reflect.Float64:
  1537. x = int64(index.Float())
  1538. case reflect.Invalid:
  1539. return 0, fmt.Errorf("cannot index slice/array/string with nil")
  1540. default:
  1541. return 0, fmt.Errorf("cannot index slice/array/string with type %s", getTypeString(index))
  1542. }
  1543. if int(x) < 0 || int(x) >= cap {
  1544. return 0, fmt.Errorf("index out of range: %d", x)
  1545. }
  1546. return int(x), nil
  1547. }
  1548. func buildCache(typ reflect.Type, cache map[string][]int, parent []int) {
  1549. numFields := typ.NumField()
  1550. max := len(parent) + 1
  1551. for i := 0; i < numFields; i++ {
  1552. index := make([]int, max)
  1553. copy(index, parent)
  1554. index[len(parent)] = i
  1555. field := typ.Field(i)
  1556. if field.Anonymous {
  1557. typ := field.Type
  1558. if typ.Kind() == reflect.Struct {
  1559. buildCache(typ, cache, index)
  1560. }
  1561. }
  1562. cache[field.Name] = index
  1563. }
  1564. }