builtins.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package tengo
  2. var builtinFuncs = []*BuiltinFunction{
  3. {
  4. Name: "len",
  5. Value: builtinLen,
  6. },
  7. {
  8. Name: "copy",
  9. Value: builtinCopy,
  10. },
  11. {
  12. Name: "append",
  13. Value: builtinAppend,
  14. },
  15. {
  16. Name: "delete",
  17. Value: builtinDelete,
  18. },
  19. {
  20. Name: "splice",
  21. Value: builtinSplice,
  22. },
  23. {
  24. Name: "string",
  25. Value: builtinString,
  26. },
  27. {
  28. Name: "int",
  29. Value: builtinInt,
  30. },
  31. {
  32. Name: "bool",
  33. Value: builtinBool,
  34. },
  35. {
  36. Name: "float",
  37. Value: builtinFloat,
  38. },
  39. {
  40. Name: "char",
  41. Value: builtinChar,
  42. },
  43. {
  44. Name: "bytes",
  45. Value: builtinBytes,
  46. },
  47. {
  48. Name: "time",
  49. Value: builtinTime,
  50. },
  51. {
  52. Name: "is_int",
  53. Value: builtinIsInt,
  54. },
  55. {
  56. Name: "is_float",
  57. Value: builtinIsFloat,
  58. },
  59. {
  60. Name: "is_string",
  61. Value: builtinIsString,
  62. },
  63. {
  64. Name: "is_bool",
  65. Value: builtinIsBool,
  66. },
  67. {
  68. Name: "is_char",
  69. Value: builtinIsChar,
  70. },
  71. {
  72. Name: "is_bytes",
  73. Value: builtinIsBytes,
  74. },
  75. {
  76. Name: "is_array",
  77. Value: builtinIsArray,
  78. },
  79. {
  80. Name: "is_immutable_array",
  81. Value: builtinIsImmutableArray,
  82. },
  83. {
  84. Name: "is_map",
  85. Value: builtinIsMap,
  86. },
  87. {
  88. Name: "is_immutable_map",
  89. Value: builtinIsImmutableMap,
  90. },
  91. {
  92. Name: "is_iterable",
  93. Value: builtinIsIterable,
  94. },
  95. {
  96. Name: "is_time",
  97. Value: builtinIsTime,
  98. },
  99. {
  100. Name: "is_error",
  101. Value: builtinIsError,
  102. },
  103. {
  104. Name: "is_undefined",
  105. Value: builtinIsUndefined,
  106. },
  107. {
  108. Name: "is_function",
  109. Value: builtinIsFunction,
  110. },
  111. {
  112. Name: "is_callable",
  113. Value: builtinIsCallable,
  114. },
  115. {
  116. Name: "type_name",
  117. Value: builtinTypeName,
  118. },
  119. {
  120. Name: "format",
  121. Value: builtinFormat,
  122. },
  123. {
  124. Name: "range",
  125. Value: builtinRange,
  126. },
  127. }
  128. // GetAllBuiltinFunctions returns all builtin function objects.
  129. func GetAllBuiltinFunctions() []*BuiltinFunction {
  130. return append([]*BuiltinFunction{}, builtinFuncs...)
  131. }
  132. func builtinTypeName(args ...Object) (Object, error) {
  133. if len(args) != 1 {
  134. return nil, ErrWrongNumArguments
  135. }
  136. return &String{Value: args[0].TypeName()}, nil
  137. }
  138. func builtinIsString(args ...Object) (Object, error) {
  139. if len(args) != 1 {
  140. return nil, ErrWrongNumArguments
  141. }
  142. if _, ok := args[0].(*String); ok {
  143. return TrueValue, nil
  144. }
  145. return FalseValue, nil
  146. }
  147. func builtinIsInt(args ...Object) (Object, error) {
  148. if len(args) != 1 {
  149. return nil, ErrWrongNumArguments
  150. }
  151. if _, ok := args[0].(Int); ok {
  152. return TrueValue, nil
  153. }
  154. return FalseValue, nil
  155. }
  156. func builtinIsFloat(args ...Object) (Object, error) {
  157. if len(args) != 1 {
  158. return nil, ErrWrongNumArguments
  159. }
  160. if _, ok := args[0].(Float); ok {
  161. return TrueValue, nil
  162. }
  163. return FalseValue, nil
  164. }
  165. func builtinIsBool(args ...Object) (Object, error) {
  166. if len(args) != 1 {
  167. return nil, ErrWrongNumArguments
  168. }
  169. if _, ok := args[0].(Bool); ok {
  170. return TrueValue, nil
  171. }
  172. return FalseValue, nil
  173. }
  174. func builtinIsChar(args ...Object) (Object, error) {
  175. if len(args) != 1 {
  176. return nil, ErrWrongNumArguments
  177. }
  178. if _, ok := args[0].(Char); ok {
  179. return TrueValue, nil
  180. }
  181. return FalseValue, nil
  182. }
  183. func builtinIsBytes(args ...Object) (Object, error) {
  184. if len(args) != 1 {
  185. return nil, ErrWrongNumArguments
  186. }
  187. if _, ok := args[0].(*Bytes); ok {
  188. return TrueValue, nil
  189. }
  190. return FalseValue, nil
  191. }
  192. func builtinIsArray(args ...Object) (Object, error) {
  193. if len(args) != 1 {
  194. return nil, ErrWrongNumArguments
  195. }
  196. if _, ok := args[0].(*Array); ok {
  197. return TrueValue, nil
  198. }
  199. return FalseValue, nil
  200. }
  201. func builtinIsImmutableArray(args ...Object) (Object, error) {
  202. if len(args) != 1 {
  203. return nil, ErrWrongNumArguments
  204. }
  205. if _, ok := args[0].(*ImmutableArray); ok {
  206. return TrueValue, nil
  207. }
  208. return FalseValue, nil
  209. }
  210. func builtinIsMap(args ...Object) (Object, error) {
  211. if len(args) != 1 {
  212. return nil, ErrWrongNumArguments
  213. }
  214. if _, ok := args[0].(*Map); ok {
  215. return TrueValue, nil
  216. }
  217. return FalseValue, nil
  218. }
  219. func builtinIsImmutableMap(args ...Object) (Object, error) {
  220. if len(args) != 1 {
  221. return nil, ErrWrongNumArguments
  222. }
  223. if _, ok := args[0].(*ImmutableMap); ok {
  224. return TrueValue, nil
  225. }
  226. return FalseValue, nil
  227. }
  228. func builtinIsTime(args ...Object) (Object, error) {
  229. if len(args) != 1 {
  230. return nil, ErrWrongNumArguments
  231. }
  232. if _, ok := args[0].(*Time); ok {
  233. return TrueValue, nil
  234. }
  235. return FalseValue, nil
  236. }
  237. func builtinIsError(args ...Object) (Object, error) {
  238. if len(args) != 1 {
  239. return nil, ErrWrongNumArguments
  240. }
  241. if _, ok := args[0].(*Error); ok {
  242. return TrueValue, nil
  243. }
  244. return FalseValue, nil
  245. }
  246. func builtinIsUndefined(args ...Object) (Object, error) {
  247. if len(args) != 1 {
  248. return nil, ErrWrongNumArguments
  249. }
  250. if args[0] == UndefinedValue {
  251. return TrueValue, nil
  252. }
  253. return FalseValue, nil
  254. }
  255. func builtinIsFunction(args ...Object) (Object, error) {
  256. if len(args) != 1 {
  257. return nil, ErrWrongNumArguments
  258. }
  259. switch args[0].(type) {
  260. case *CompiledFunction:
  261. return TrueValue, nil
  262. }
  263. return FalseValue, nil
  264. }
  265. func builtinIsCallable(args ...Object) (Object, error) {
  266. if len(args) != 1 {
  267. return nil, ErrWrongNumArguments
  268. }
  269. if args[0].CanCall() {
  270. return TrueValue, nil
  271. }
  272. return FalseValue, nil
  273. }
  274. func builtinIsIterable(args ...Object) (Object, error) {
  275. if len(args) != 1 {
  276. return nil, ErrWrongNumArguments
  277. }
  278. if args[0].CanIterate() {
  279. return TrueValue, nil
  280. }
  281. return FalseValue, nil
  282. }
  283. // len(obj object) => int
  284. func builtinLen(args ...Object) (Object, error) {
  285. if len(args) != 1 {
  286. return nil, ErrWrongNumArguments
  287. }
  288. switch arg := args[0].(type) {
  289. case *Array:
  290. return Int{Value: int64(len(arg.Value))}, nil
  291. case *ImmutableArray:
  292. return Int{Value: int64(len(arg.Value))}, nil
  293. case *String:
  294. return Int{Value: int64(len(arg.Value))}, nil
  295. case *Bytes:
  296. return Int{Value: int64(len(arg.Value))}, nil
  297. case *Map:
  298. return Int{Value: int64(len(arg.Value))}, nil
  299. case *ImmutableMap:
  300. return Int{Value: int64(len(arg.Value))}, nil
  301. default:
  302. return nil, ErrInvalidArgumentType{
  303. Name: "first",
  304. Expected: "array/string/bytes/map",
  305. Found: arg.TypeName(),
  306. }
  307. }
  308. }
  309. //range(start, stop[, step])
  310. func builtinRange(args ...Object) (Object, error) {
  311. numArgs := len(args)
  312. if numArgs < 2 || numArgs > 3 {
  313. return nil, ErrWrongNumArguments
  314. }
  315. var start, stop Int
  316. step := Int{Value: 1}
  317. for i, arg := range args {
  318. v, ok := args[i].(Int)
  319. if !ok {
  320. var name string
  321. switch i {
  322. case 0:
  323. name = "start"
  324. case 1:
  325. name = "stop"
  326. case 2:
  327. name = "step"
  328. }
  329. return nil, ErrInvalidArgumentType{
  330. Name: name,
  331. Expected: "int",
  332. Found: arg.TypeName(),
  333. }
  334. }
  335. if i == 2 && v.Value <= 0 {
  336. return nil, ErrInvalidRangeStep
  337. }
  338. switch i {
  339. case 0:
  340. start = v
  341. case 1:
  342. stop = v
  343. case 2:
  344. step = v
  345. }
  346. }
  347. return buildRange(start.Value, stop.Value, step.Value), nil
  348. }
  349. func buildRange(start, stop, step int64) *Array {
  350. array := &Array{}
  351. if start <= stop {
  352. for i := start; i < stop; i += step {
  353. array.Value = append(array.Value, Int{
  354. Value: i,
  355. })
  356. }
  357. } else {
  358. for i := start; i > stop; i -= step {
  359. array.Value = append(array.Value, Int{
  360. Value: i,
  361. })
  362. }
  363. }
  364. return array
  365. }
  366. func builtinFormat(args ...Object) (Object, error) {
  367. numArgs := len(args)
  368. if numArgs == 0 {
  369. return nil, ErrWrongNumArguments
  370. }
  371. format, ok := args[0].(*String)
  372. if !ok {
  373. return nil, ErrInvalidArgumentType{
  374. Name: "format",
  375. Expected: "string",
  376. Found: args[0].TypeName(),
  377. }
  378. }
  379. if numArgs == 1 {
  380. // okay to return 'format' directly as String is immutable
  381. return format, nil
  382. }
  383. s, err := Format(format.Value, args[1:]...)
  384. if err != nil {
  385. return nil, err
  386. }
  387. return &String{Value: s}, nil
  388. }
  389. func builtinCopy(args ...Object) (Object, error) {
  390. if len(args) != 1 {
  391. return nil, ErrWrongNumArguments
  392. }
  393. return args[0].Copy(), nil
  394. }
  395. func builtinString(args ...Object) (Object, error) {
  396. argsLen := len(args)
  397. if !(argsLen == 1 || argsLen == 2) {
  398. return nil, ErrWrongNumArguments
  399. }
  400. if _, ok := args[0].(*String); ok {
  401. return args[0], nil
  402. }
  403. v, ok := ToString(args[0])
  404. if ok {
  405. if len(v) > MaxStringLen {
  406. return nil, ErrStringLimit
  407. }
  408. return &String{Value: v}, nil
  409. }
  410. if argsLen == 2 {
  411. return args[1], nil
  412. }
  413. return UndefinedValue, nil
  414. }
  415. func builtinInt(args ...Object) (Object, error) {
  416. argsLen := len(args)
  417. if !(argsLen == 1 || argsLen == 2) {
  418. return nil, ErrWrongNumArguments
  419. }
  420. if _, ok := args[0].(Int); ok {
  421. return args[0], nil
  422. }
  423. v, ok := ToInt64(args[0])
  424. if ok {
  425. return Int{Value: v}, nil
  426. }
  427. if argsLen == 2 {
  428. return args[1], nil
  429. }
  430. return UndefinedValue, nil
  431. }
  432. func builtinFloat(args ...Object) (Object, error) {
  433. argsLen := len(args)
  434. if !(argsLen == 1 || argsLen == 2) {
  435. return nil, ErrWrongNumArguments
  436. }
  437. if _, ok := args[0].(Float); ok {
  438. return args[0], nil
  439. }
  440. v, ok := ToFloat64(args[0])
  441. if ok {
  442. return Float{Value: v}, nil
  443. }
  444. if argsLen == 2 {
  445. return args[1], nil
  446. }
  447. return UndefinedValue, nil
  448. }
  449. func builtinBool(args ...Object) (Object, error) {
  450. if len(args) != 1 {
  451. return nil, ErrWrongNumArguments
  452. }
  453. if _, ok := args[0].(Bool); ok {
  454. return args[0], nil
  455. }
  456. v, ok := ToBool(args[0])
  457. if ok {
  458. if v {
  459. return TrueValue, nil
  460. }
  461. return FalseValue, nil
  462. }
  463. return UndefinedValue, nil
  464. }
  465. func builtinChar(args ...Object) (Object, error) {
  466. argsLen := len(args)
  467. if !(argsLen == 1 || argsLen == 2) {
  468. return nil, ErrWrongNumArguments
  469. }
  470. if _, ok := args[0].(Char); ok {
  471. return args[0], nil
  472. }
  473. v, ok := ToRune(args[0])
  474. if ok {
  475. return Char{Value: v}, nil
  476. }
  477. if argsLen == 2 {
  478. return args[1], nil
  479. }
  480. return UndefinedValue, nil
  481. }
  482. func builtinBytes(args ...Object) (Object, error) {
  483. argsLen := len(args)
  484. if !(argsLen == 1 || argsLen == 2) {
  485. return nil, ErrWrongNumArguments
  486. }
  487. // bytes(N) => create a new bytes with given size N
  488. if n, ok := args[0].(Int); ok {
  489. if n.Value > int64(MaxBytesLen) {
  490. return nil, ErrBytesLimit
  491. }
  492. return &Bytes{Value: make([]byte, int(n.Value))}, nil
  493. }
  494. v, ok := ToByteSlice(args[0])
  495. if ok {
  496. if len(v) > MaxBytesLen {
  497. return nil, ErrBytesLimit
  498. }
  499. return &Bytes{Value: v}, nil
  500. }
  501. if argsLen == 2 {
  502. return args[1], nil
  503. }
  504. return UndefinedValue, nil
  505. }
  506. func builtinTime(args ...Object) (Object, error) {
  507. argsLen := len(args)
  508. if !(argsLen == 1 || argsLen == 2) {
  509. return nil, ErrWrongNumArguments
  510. }
  511. if _, ok := args[0].(*Time); ok {
  512. return args[0], nil
  513. }
  514. v, ok := ToTime(args[0])
  515. if ok {
  516. return &Time{Value: v}, nil
  517. }
  518. if argsLen == 2 {
  519. return args[1], nil
  520. }
  521. return UndefinedValue, nil
  522. }
  523. // append(arr, items...)
  524. func builtinAppend(args ...Object) (Object, error) {
  525. if len(args) < 2 {
  526. return nil, ErrWrongNumArguments
  527. }
  528. switch arg := args[0].(type) {
  529. case *Array:
  530. return &Array{Value: append(arg.Value, args[1:]...)}, nil
  531. case *ImmutableArray:
  532. return &Array{Value: append(arg.Value, args[1:]...)}, nil
  533. default:
  534. return nil, ErrInvalidArgumentType{
  535. Name: "first",
  536. Expected: "array",
  537. Found: arg.TypeName(),
  538. }
  539. }
  540. }
  541. // builtinDelete deletes Map keys
  542. // usage: delete(map, "key")
  543. // key must be a string
  544. func builtinDelete(args ...Object) (Object, error) {
  545. argsLen := len(args)
  546. if argsLen != 2 {
  547. return nil, ErrWrongNumArguments
  548. }
  549. switch arg := args[0].(type) {
  550. case *Map:
  551. if key, ok := args[1].(*String); ok {
  552. delete(arg.Value, key.Value)
  553. return UndefinedValue, nil
  554. }
  555. return nil, ErrInvalidArgumentType{
  556. Name: "second",
  557. Expected: "string",
  558. Found: args[1].TypeName(),
  559. }
  560. default:
  561. return nil, ErrInvalidArgumentType{
  562. Name: "first",
  563. Expected: "map",
  564. Found: arg.TypeName(),
  565. }
  566. }
  567. }
  568. // builtinSplice deletes and changes given Array, returns deleted items.
  569. // usage:
  570. // deleted_items := splice(array[,start[,delete_count[,item1[,item2[,...]]]])
  571. func builtinSplice(args ...Object) (Object, error) {
  572. argsLen := len(args)
  573. if argsLen == 0 {
  574. return nil, ErrWrongNumArguments
  575. }
  576. array, ok := args[0].(*Array)
  577. if !ok {
  578. return nil, ErrInvalidArgumentType{
  579. Name: "first",
  580. Expected: "array",
  581. Found: args[0].TypeName(),
  582. }
  583. }
  584. arrayLen := len(array.Value)
  585. var startIdx int
  586. if argsLen > 1 {
  587. arg1, ok := args[1].(Int)
  588. if !ok {
  589. return nil, ErrInvalidArgumentType{
  590. Name: "second",
  591. Expected: "int",
  592. Found: args[1].TypeName(),
  593. }
  594. }
  595. startIdx = int(arg1.Value)
  596. if startIdx < 0 || startIdx > arrayLen {
  597. return nil, ErrIndexOutOfBounds
  598. }
  599. }
  600. delCount := len(array.Value)
  601. if argsLen > 2 {
  602. arg2, ok := args[2].(Int)
  603. if !ok {
  604. return nil, ErrInvalidArgumentType{
  605. Name: "third",
  606. Expected: "int",
  607. Found: args[2].TypeName(),
  608. }
  609. }
  610. delCount = int(arg2.Value)
  611. if delCount < 0 {
  612. return nil, ErrIndexOutOfBounds
  613. }
  614. }
  615. // if count of to be deleted items is bigger than expected, truncate it
  616. if startIdx+delCount > arrayLen {
  617. delCount = arrayLen - startIdx
  618. }
  619. // delete items
  620. endIdx := startIdx + delCount
  621. deleted := append([]Object{}, array.Value[startIdx:endIdx]...)
  622. head := array.Value[:startIdx]
  623. var items []Object
  624. if argsLen > 3 {
  625. items = make([]Object, 0, argsLen-3)
  626. for i := 3; i < argsLen; i++ {
  627. items = append(items, args[i])
  628. }
  629. }
  630. items = append(items, array.Value[endIdx:]...)
  631. array.Value = append(head, items...)
  632. // return deleted items
  633. return &Array{Value: deleted}, nil
  634. }