builtins.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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, step *Int
  316. for i, arg := range args {
  317. v, ok := args[i].(*Int)
  318. if !ok {
  319. var name string
  320. switch i {
  321. case 0:
  322. name = "start"
  323. case 1:
  324. name = "stop"
  325. case 2:
  326. name = "step"
  327. }
  328. return nil, ErrInvalidArgumentType{
  329. Name: name,
  330. Expected: "int",
  331. Found: arg.TypeName(),
  332. }
  333. }
  334. if i == 2 && v.Value <= 0 {
  335. return nil, ErrInvalidRangeStep
  336. }
  337. switch i {
  338. case 0:
  339. start = v
  340. case 1:
  341. stop = v
  342. case 2:
  343. step = v
  344. }
  345. }
  346. if step == nil {
  347. step = &Int{Value: int64(1)}
  348. }
  349. return buildRange(start.Value, stop.Value, step.Value), nil
  350. }
  351. func buildRange(start, stop, step int64) *Array {
  352. array := &Array{}
  353. if start <= stop {
  354. for i := start; i < stop; i += step {
  355. array.Value = append(array.Value, &Int{
  356. Value: i,
  357. })
  358. }
  359. } else {
  360. for i := start; i > stop; i -= step {
  361. array.Value = append(array.Value, &Int{
  362. Value: i,
  363. })
  364. }
  365. }
  366. return array
  367. }
  368. func builtinFormat(args ...Object) (Object, error) {
  369. numArgs := len(args)
  370. if numArgs == 0 {
  371. return nil, ErrWrongNumArguments
  372. }
  373. format, ok := args[0].(*String)
  374. if !ok {
  375. return nil, ErrInvalidArgumentType{
  376. Name: "format",
  377. Expected: "string",
  378. Found: args[0].TypeName(),
  379. }
  380. }
  381. if numArgs == 1 {
  382. // okay to return 'format' directly as String is immutable
  383. return format, nil
  384. }
  385. s, err := Format(format.Value, args[1:]...)
  386. if err != nil {
  387. return nil, err
  388. }
  389. return &String{Value: s}, nil
  390. }
  391. func builtinCopy(args ...Object) (Object, error) {
  392. if len(args) != 1 {
  393. return nil, ErrWrongNumArguments
  394. }
  395. return args[0].Copy(), nil
  396. }
  397. func builtinString(args ...Object) (Object, error) {
  398. argsLen := len(args)
  399. if !(argsLen == 1 || argsLen == 2) {
  400. return nil, ErrWrongNumArguments
  401. }
  402. if _, ok := args[0].(*String); ok {
  403. return args[0], nil
  404. }
  405. v, ok := ToString(args[0])
  406. if ok {
  407. if len(v) > MaxStringLen {
  408. return nil, ErrStringLimit
  409. }
  410. return &String{Value: v}, nil
  411. }
  412. if argsLen == 2 {
  413. return args[1], nil
  414. }
  415. return UndefinedValue, nil
  416. }
  417. func builtinInt(args ...Object) (Object, error) {
  418. argsLen := len(args)
  419. if !(argsLen == 1 || argsLen == 2) {
  420. return nil, ErrWrongNumArguments
  421. }
  422. if _, ok := args[0].(*Int); ok {
  423. return args[0], nil
  424. }
  425. v, ok := ToInt64(args[0])
  426. if ok {
  427. return &Int{Value: v}, nil
  428. }
  429. if argsLen == 2 {
  430. return args[1], nil
  431. }
  432. return UndefinedValue, nil
  433. }
  434. func builtinFloat(args ...Object) (Object, error) {
  435. argsLen := len(args)
  436. if !(argsLen == 1 || argsLen == 2) {
  437. return nil, ErrWrongNumArguments
  438. }
  439. if _, ok := args[0].(*Float); ok {
  440. return args[0], nil
  441. }
  442. v, ok := ToFloat64(args[0])
  443. if ok {
  444. return &Float{Value: v}, nil
  445. }
  446. if argsLen == 2 {
  447. return args[1], nil
  448. }
  449. return UndefinedValue, nil
  450. }
  451. func builtinBool(args ...Object) (Object, error) {
  452. if len(args) != 1 {
  453. return nil, ErrWrongNumArguments
  454. }
  455. if _, ok := args[0].(*Bool); ok {
  456. return args[0], nil
  457. }
  458. v, ok := ToBool(args[0])
  459. if ok {
  460. if v {
  461. return TrueValue, nil
  462. }
  463. return FalseValue, nil
  464. }
  465. return UndefinedValue, nil
  466. }
  467. func builtinChar(args ...Object) (Object, error) {
  468. argsLen := len(args)
  469. if !(argsLen == 1 || argsLen == 2) {
  470. return nil, ErrWrongNumArguments
  471. }
  472. if _, ok := args[0].(*Char); ok {
  473. return args[0], nil
  474. }
  475. v, ok := ToRune(args[0])
  476. if ok {
  477. return &Char{Value: v}, nil
  478. }
  479. if argsLen == 2 {
  480. return args[1], nil
  481. }
  482. return UndefinedValue, nil
  483. }
  484. func builtinBytes(args ...Object) (Object, error) {
  485. argsLen := len(args)
  486. if !(argsLen == 1 || argsLen == 2) {
  487. return nil, ErrWrongNumArguments
  488. }
  489. // bytes(N) => create a new bytes with given size N
  490. if n, ok := args[0].(*Int); ok {
  491. if n.Value > int64(MaxBytesLen) {
  492. return nil, ErrBytesLimit
  493. }
  494. return &Bytes{Value: make([]byte, int(n.Value))}, nil
  495. }
  496. v, ok := ToByteSlice(args[0])
  497. if ok {
  498. if len(v) > MaxBytesLen {
  499. return nil, ErrBytesLimit
  500. }
  501. return &Bytes{Value: v}, nil
  502. }
  503. if argsLen == 2 {
  504. return args[1], nil
  505. }
  506. return UndefinedValue, nil
  507. }
  508. func builtinTime(args ...Object) (Object, error) {
  509. argsLen := len(args)
  510. if !(argsLen == 1 || argsLen == 2) {
  511. return nil, ErrWrongNumArguments
  512. }
  513. if _, ok := args[0].(*Time); ok {
  514. return args[0], nil
  515. }
  516. v, ok := ToTime(args[0])
  517. if ok {
  518. return &Time{Value: v}, nil
  519. }
  520. if argsLen == 2 {
  521. return args[1], nil
  522. }
  523. return UndefinedValue, nil
  524. }
  525. // append(arr, items...)
  526. func builtinAppend(args ...Object) (Object, error) {
  527. if len(args) < 2 {
  528. return nil, ErrWrongNumArguments
  529. }
  530. switch arg := args[0].(type) {
  531. case *Array:
  532. return &Array{Value: append(arg.Value, args[1:]...)}, nil
  533. case *ImmutableArray:
  534. return &Array{Value: append(arg.Value, args[1:]...)}, nil
  535. default:
  536. return nil, ErrInvalidArgumentType{
  537. Name: "first",
  538. Expected: "array",
  539. Found: arg.TypeName(),
  540. }
  541. }
  542. }
  543. // builtinDelete deletes Map keys
  544. // usage: delete(map, "key")
  545. // key must be a string
  546. func builtinDelete(args ...Object) (Object, error) {
  547. argsLen := len(args)
  548. if argsLen != 2 {
  549. return nil, ErrWrongNumArguments
  550. }
  551. switch arg := args[0].(type) {
  552. case *Map:
  553. if key, ok := args[1].(*String); ok {
  554. delete(arg.Value, key.Value)
  555. return UndefinedValue, nil
  556. }
  557. return nil, ErrInvalidArgumentType{
  558. Name: "second",
  559. Expected: "string",
  560. Found: args[1].TypeName(),
  561. }
  562. default:
  563. return nil, ErrInvalidArgumentType{
  564. Name: "first",
  565. Expected: "map",
  566. Found: arg.TypeName(),
  567. }
  568. }
  569. }
  570. // builtinSplice deletes and changes given Array, returns deleted items.
  571. // usage:
  572. // deleted_items := splice(array[,start[,delete_count[,item1[,item2[,...]]]])
  573. func builtinSplice(args ...Object) (Object, error) {
  574. argsLen := len(args)
  575. if argsLen == 0 {
  576. return nil, ErrWrongNumArguments
  577. }
  578. array, ok := args[0].(*Array)
  579. if !ok {
  580. return nil, ErrInvalidArgumentType{
  581. Name: "first",
  582. Expected: "array",
  583. Found: args[0].TypeName(),
  584. }
  585. }
  586. arrayLen := len(array.Value)
  587. var startIdx int
  588. if argsLen > 1 {
  589. arg1, ok := args[1].(*Int)
  590. if !ok {
  591. return nil, ErrInvalidArgumentType{
  592. Name: "second",
  593. Expected: "int",
  594. Found: args[1].TypeName(),
  595. }
  596. }
  597. startIdx = int(arg1.Value)
  598. if startIdx < 0 || startIdx > arrayLen {
  599. return nil, ErrIndexOutOfBounds
  600. }
  601. }
  602. delCount := len(array.Value)
  603. if argsLen > 2 {
  604. arg2, ok := args[2].(*Int)
  605. if !ok {
  606. return nil, ErrInvalidArgumentType{
  607. Name: "third",
  608. Expected: "int",
  609. Found: args[2].TypeName(),
  610. }
  611. }
  612. delCount = int(arg2.Value)
  613. if delCount < 0 {
  614. return nil, ErrIndexOutOfBounds
  615. }
  616. }
  617. // if count of to be deleted items is bigger than expected, truncate it
  618. if startIdx+delCount > arrayLen {
  619. delCount = arrayLen - startIdx
  620. }
  621. // delete items
  622. endIdx := startIdx + delCount
  623. deleted := append([]Object{}, array.Value[startIdx:endIdx]...)
  624. head := array.Value[:startIdx]
  625. var items []Object
  626. if argsLen > 3 {
  627. items = make([]Object, 0, argsLen-3)
  628. for i := 3; i < argsLen; i++ {
  629. items = append(items, args[i])
  630. }
  631. }
  632. items = append(items, array.Value[endIdx:]...)
  633. array.Value = append(head, items...)
  634. // return deleted items
  635. return &Array{Value: deleted}, nil
  636. }