func_typedefs.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. package stdlib
  2. import (
  3. "fmt"
  4. "github.com/d5/tengo/v2"
  5. )
  6. // FuncAR transform a function of 'func()' signature into CallableFunc type.
  7. func FuncAR(fn func()) tengo.CallableFunc {
  8. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  9. if len(args) != 0 {
  10. return nil, tengo.ErrWrongNumArguments
  11. }
  12. fn()
  13. return tengo.UndefinedValue, nil
  14. }
  15. }
  16. // FuncARI transform a function of 'func() int' signature into CallableFunc
  17. // type.
  18. func FuncARI(fn func() int) tengo.CallableFunc {
  19. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  20. if len(args) != 0 {
  21. return nil, tengo.ErrWrongNumArguments
  22. }
  23. return &tengo.Int{Value: int64(fn())}, nil
  24. }
  25. }
  26. // FuncARI64 transform a function of 'func() int64' signature into CallableFunc
  27. // type.
  28. func FuncARI64(fn func() int64) tengo.CallableFunc {
  29. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  30. if len(args) != 0 {
  31. return nil, tengo.ErrWrongNumArguments
  32. }
  33. return &tengo.Int{Value: fn()}, nil
  34. }
  35. }
  36. // FuncAI64RI64 transform a function of 'func(int64) int64' signature into
  37. // CallableFunc type.
  38. func FuncAI64RI64(fn func(int64) int64) tengo.CallableFunc {
  39. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  40. if len(args) != 1 {
  41. return nil, tengo.ErrWrongNumArguments
  42. }
  43. i1, ok := tengo.ToInt64(args[0])
  44. if !ok {
  45. return nil, tengo.ErrInvalidArgumentType{
  46. Name: "first",
  47. Expected: "int(compatible)",
  48. Found: args[0].TypeName(),
  49. }
  50. }
  51. return &tengo.Int{Value: fn(i1)}, nil
  52. }
  53. }
  54. // FuncAI64R transform a function of 'func(int64)' signature into CallableFunc
  55. // type.
  56. func FuncAI64R(fn func(int64)) tengo.CallableFunc {
  57. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  58. if len(args) != 1 {
  59. return nil, tengo.ErrWrongNumArguments
  60. }
  61. i1, ok := tengo.ToInt64(args[0])
  62. if !ok {
  63. return nil, tengo.ErrInvalidArgumentType{
  64. Name: "first",
  65. Expected: "int(compatible)",
  66. Found: args[0].TypeName(),
  67. }
  68. }
  69. fn(i1)
  70. return tengo.UndefinedValue, nil
  71. }
  72. }
  73. // FuncARB transform a function of 'func() bool' signature into CallableFunc
  74. // type.
  75. func FuncARB(fn func() bool) tengo.CallableFunc {
  76. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  77. if len(args) != 0 {
  78. return nil, tengo.ErrWrongNumArguments
  79. }
  80. if fn() {
  81. return tengo.TrueValue, nil
  82. }
  83. return tengo.FalseValue, nil
  84. }
  85. }
  86. // FuncARE transform a function of 'func() error' signature into CallableFunc
  87. // type.
  88. func FuncARE(fn func() error) tengo.CallableFunc {
  89. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  90. if len(args) != 0 {
  91. return nil, tengo.ErrWrongNumArguments
  92. }
  93. return wrapError(fn()), nil
  94. }
  95. }
  96. // FuncARS transform a function of 'func() string' signature into CallableFunc
  97. // type.
  98. func FuncARS(fn func() string) tengo.CallableFunc {
  99. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  100. if len(args) != 0 {
  101. return nil, tengo.ErrWrongNumArguments
  102. }
  103. s := fn()
  104. if len(s) > tengo.MaxStringLen {
  105. return nil, tengo.ErrStringLimit
  106. }
  107. return &tengo.String{Value: s}, nil
  108. }
  109. }
  110. // FuncARSE transform a function of 'func() (string, error)' signature into
  111. // CallableFunc type.
  112. func FuncARSE(fn func() (string, error)) tengo.CallableFunc {
  113. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  114. if len(args) != 0 {
  115. return nil, tengo.ErrWrongNumArguments
  116. }
  117. res, err := fn()
  118. if err != nil {
  119. return wrapError(err), nil
  120. }
  121. if len(res) > tengo.MaxStringLen {
  122. return nil, tengo.ErrStringLimit
  123. }
  124. return &tengo.String{Value: res}, nil
  125. }
  126. }
  127. // FuncARYE transform a function of 'func() ([]byte, error)' signature into
  128. // CallableFunc type.
  129. func FuncARYE(fn func() ([]byte, error)) tengo.CallableFunc {
  130. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  131. if len(args) != 0 {
  132. return nil, tengo.ErrWrongNumArguments
  133. }
  134. res, err := fn()
  135. if err != nil {
  136. return wrapError(err), nil
  137. }
  138. if len(res) > tengo.MaxBytesLen {
  139. return nil, tengo.ErrBytesLimit
  140. }
  141. return &tengo.Bytes{Value: res}, nil
  142. }
  143. }
  144. // FuncARF transform a function of 'func() float64' signature into CallableFunc
  145. // type.
  146. func FuncARF(fn func() float64) tengo.CallableFunc {
  147. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  148. if len(args) != 0 {
  149. return nil, tengo.ErrWrongNumArguments
  150. }
  151. return &tengo.Float{Value: fn()}, nil
  152. }
  153. }
  154. // FuncARSs transform a function of 'func() []string' signature into
  155. // CallableFunc type.
  156. func FuncARSs(fn func() []string) tengo.CallableFunc {
  157. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  158. if len(args) != 0 {
  159. return nil, tengo.ErrWrongNumArguments
  160. }
  161. arr := &tengo.Array{}
  162. for _, elem := range fn() {
  163. if len(elem) > tengo.MaxStringLen {
  164. return nil, tengo.ErrStringLimit
  165. }
  166. arr.Value = append(arr.Value, &tengo.String{Value: elem})
  167. }
  168. return arr, nil
  169. }
  170. }
  171. // FuncARIsE transform a function of 'func() ([]int, error)' signature into
  172. // CallableFunc type.
  173. func FuncARIsE(fn func() ([]int, error)) tengo.CallableFunc {
  174. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  175. if len(args) != 0 {
  176. return nil, tengo.ErrWrongNumArguments
  177. }
  178. res, err := fn()
  179. if err != nil {
  180. return wrapError(err), nil
  181. }
  182. arr := &tengo.Array{}
  183. for _, v := range res {
  184. arr.Value = append(arr.Value, &tengo.Int{Value: int64(v)})
  185. }
  186. return arr, nil
  187. }
  188. }
  189. // FuncAIRIs transform a function of 'func(int) []int' signature into
  190. // CallableFunc type.
  191. func FuncAIRIs(fn func(int) []int) tengo.CallableFunc {
  192. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  193. if len(args) != 1 {
  194. return nil, tengo.ErrWrongNumArguments
  195. }
  196. i1, ok := tengo.ToInt(args[0])
  197. if !ok {
  198. return nil, tengo.ErrInvalidArgumentType{
  199. Name: "first",
  200. Expected: "int(compatible)",
  201. Found: args[0].TypeName(),
  202. }
  203. }
  204. res := fn(i1)
  205. arr := &tengo.Array{}
  206. for _, v := range res {
  207. arr.Value = append(arr.Value, &tengo.Int{Value: int64(v)})
  208. }
  209. return arr, nil
  210. }
  211. }
  212. // FuncAFRF transform a function of 'func(float64) float64' signature into
  213. // CallableFunc type.
  214. func FuncAFRF(fn func(float64) float64) tengo.CallableFunc {
  215. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  216. if len(args) != 1 {
  217. return nil, tengo.ErrWrongNumArguments
  218. }
  219. f1, ok := tengo.ToFloat64(args[0])
  220. if !ok {
  221. return nil, tengo.ErrInvalidArgumentType{
  222. Name: "first",
  223. Expected: "float(compatible)",
  224. Found: args[0].TypeName(),
  225. }
  226. }
  227. return &tengo.Float{Value: fn(f1)}, nil
  228. }
  229. }
  230. // FuncAIR transform a function of 'func(int)' signature into CallableFunc type.
  231. func FuncAIR(fn func(int)) tengo.CallableFunc {
  232. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  233. if len(args) != 1 {
  234. return nil, tengo.ErrWrongNumArguments
  235. }
  236. i1, ok := tengo.ToInt(args[0])
  237. if !ok {
  238. return nil, tengo.ErrInvalidArgumentType{
  239. Name: "first",
  240. Expected: "int(compatible)",
  241. Found: args[0].TypeName(),
  242. }
  243. }
  244. fn(i1)
  245. return tengo.UndefinedValue, nil
  246. }
  247. }
  248. // FuncAIRF transform a function of 'func(int) float64' signature into
  249. // CallableFunc type.
  250. func FuncAIRF(fn func(int) float64) tengo.CallableFunc {
  251. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  252. if len(args) != 1 {
  253. return nil, tengo.ErrWrongNumArguments
  254. }
  255. i1, ok := tengo.ToInt(args[0])
  256. if !ok {
  257. return nil, tengo.ErrInvalidArgumentType{
  258. Name: "first",
  259. Expected: "int(compatible)",
  260. Found: args[0].TypeName(),
  261. }
  262. }
  263. return &tengo.Float{Value: fn(i1)}, nil
  264. }
  265. }
  266. // FuncAFRI transform a function of 'func(float64) int' signature into
  267. // CallableFunc type.
  268. func FuncAFRI(fn func(float64) int) tengo.CallableFunc {
  269. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  270. if len(args) != 1 {
  271. return nil, tengo.ErrWrongNumArguments
  272. }
  273. f1, ok := tengo.ToFloat64(args[0])
  274. if !ok {
  275. return nil, tengo.ErrInvalidArgumentType{
  276. Name: "first",
  277. Expected: "float(compatible)",
  278. Found: args[0].TypeName(),
  279. }
  280. }
  281. return &tengo.Int{Value: int64(fn(f1))}, nil
  282. }
  283. }
  284. // FuncAFFRF transform a function of 'func(float64, float64) float64' signature
  285. // into CallableFunc type.
  286. func FuncAFFRF(fn func(float64, float64) float64) tengo.CallableFunc {
  287. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  288. if len(args) != 2 {
  289. return nil, tengo.ErrWrongNumArguments
  290. }
  291. f1, ok := tengo.ToFloat64(args[0])
  292. if !ok {
  293. return nil, tengo.ErrInvalidArgumentType{
  294. Name: "first",
  295. Expected: "float(compatible)",
  296. Found: args[0].TypeName(),
  297. }
  298. }
  299. f2, ok := tengo.ToFloat64(args[1])
  300. if !ok {
  301. return nil, tengo.ErrInvalidArgumentType{
  302. Name: "second",
  303. Expected: "float(compatible)",
  304. Found: args[1].TypeName(),
  305. }
  306. }
  307. return &tengo.Float{Value: fn(f1, f2)}, nil
  308. }
  309. }
  310. // FuncAIFRF transform a function of 'func(int, float64) float64' signature
  311. // into CallableFunc type.
  312. func FuncAIFRF(fn func(int, float64) float64) tengo.CallableFunc {
  313. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  314. if len(args) != 2 {
  315. return nil, tengo.ErrWrongNumArguments
  316. }
  317. i1, ok := tengo.ToInt(args[0])
  318. if !ok {
  319. return nil, tengo.ErrInvalidArgumentType{
  320. Name: "first",
  321. Expected: "int(compatible)",
  322. Found: args[0].TypeName(),
  323. }
  324. }
  325. f2, ok := tengo.ToFloat64(args[1])
  326. if !ok {
  327. return nil, tengo.ErrInvalidArgumentType{
  328. Name: "second",
  329. Expected: "float(compatible)",
  330. Found: args[1].TypeName(),
  331. }
  332. }
  333. return &tengo.Float{Value: fn(i1, f2)}, nil
  334. }
  335. }
  336. // FuncAFIRF transform a function of 'func(float64, int) float64' signature
  337. // into CallableFunc type.
  338. func FuncAFIRF(fn func(float64, int) float64) tengo.CallableFunc {
  339. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  340. if len(args) != 2 {
  341. return nil, tengo.ErrWrongNumArguments
  342. }
  343. f1, ok := tengo.ToFloat64(args[0])
  344. if !ok {
  345. return nil, tengo.ErrInvalidArgumentType{
  346. Name: "first",
  347. Expected: "float(compatible)",
  348. Found: args[0].TypeName(),
  349. }
  350. }
  351. i2, ok := tengo.ToInt(args[1])
  352. if !ok {
  353. return nil, tengo.ErrInvalidArgumentType{
  354. Name: "second",
  355. Expected: "int(compatible)",
  356. Found: args[1].TypeName(),
  357. }
  358. }
  359. return &tengo.Float{Value: fn(f1, i2)}, nil
  360. }
  361. }
  362. // FuncAFIRB transform a function of 'func(float64, int) bool' signature
  363. // into CallableFunc type.
  364. func FuncAFIRB(fn func(float64, int) bool) tengo.CallableFunc {
  365. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  366. if len(args) != 2 {
  367. return nil, tengo.ErrWrongNumArguments
  368. }
  369. f1, ok := tengo.ToFloat64(args[0])
  370. if !ok {
  371. return nil, tengo.ErrInvalidArgumentType{
  372. Name: "first",
  373. Expected: "float(compatible)",
  374. Found: args[0].TypeName(),
  375. }
  376. }
  377. i2, ok := tengo.ToInt(args[1])
  378. if !ok {
  379. return nil, tengo.ErrInvalidArgumentType{
  380. Name: "second",
  381. Expected: "int(compatible)",
  382. Found: args[1].TypeName(),
  383. }
  384. }
  385. if fn(f1, i2) {
  386. return tengo.TrueValue, nil
  387. }
  388. return tengo.FalseValue, nil
  389. }
  390. }
  391. // FuncAFRB transform a function of 'func(float64) bool' signature
  392. // into CallableFunc type.
  393. func FuncAFRB(fn func(float64) bool) tengo.CallableFunc {
  394. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  395. if len(args) != 1 {
  396. return nil, tengo.ErrWrongNumArguments
  397. }
  398. f1, ok := tengo.ToFloat64(args[0])
  399. if !ok {
  400. return nil, tengo.ErrInvalidArgumentType{
  401. Name: "first",
  402. Expected: "float(compatible)",
  403. Found: args[0].TypeName(),
  404. }
  405. }
  406. if fn(f1) {
  407. return tengo.TrueValue, nil
  408. }
  409. return tengo.FalseValue, nil
  410. }
  411. }
  412. // FuncASRS transform a function of 'func(string) string' signature into
  413. // CallableFunc type. User function will return 'true' if underlying native
  414. // function returns nil.
  415. func FuncASRS(fn func(string) string) tengo.CallableFunc {
  416. return func(args ...tengo.Object) (tengo.Object, error) {
  417. if len(args) != 1 {
  418. return nil, tengo.ErrWrongNumArguments
  419. }
  420. s1, ok := tengo.ToString(args[0])
  421. if !ok {
  422. return nil, tengo.ErrInvalidArgumentType{
  423. Name: "first",
  424. Expected: "string(compatible)",
  425. Found: args[0].TypeName(),
  426. }
  427. }
  428. s := fn(s1)
  429. if len(s) > tengo.MaxStringLen {
  430. return nil, tengo.ErrStringLimit
  431. }
  432. return &tengo.String{Value: s}, nil
  433. }
  434. }
  435. // FuncASRSs transform a function of 'func(string) []string' signature into
  436. // CallableFunc type.
  437. func FuncASRSs(fn func(string) []string) tengo.CallableFunc {
  438. return func(args ...tengo.Object) (tengo.Object, error) {
  439. if len(args) != 1 {
  440. return nil, tengo.ErrWrongNumArguments
  441. }
  442. s1, ok := tengo.ToString(args[0])
  443. if !ok {
  444. return nil, tengo.ErrInvalidArgumentType{
  445. Name: "first",
  446. Expected: "string(compatible)",
  447. Found: args[0].TypeName(),
  448. }
  449. }
  450. res := fn(s1)
  451. arr := &tengo.Array{}
  452. for _, elem := range res {
  453. if len(elem) > tengo.MaxStringLen {
  454. return nil, tengo.ErrStringLimit
  455. }
  456. arr.Value = append(arr.Value, &tengo.String{Value: elem})
  457. }
  458. return arr, nil
  459. }
  460. }
  461. // FuncASRSE transform a function of 'func(string) (string, error)' signature
  462. // into CallableFunc type. User function will return 'true' if underlying
  463. // native function returns nil.
  464. func FuncASRSE(fn func(string) (string, error)) tengo.CallableFunc {
  465. return func(args ...tengo.Object) (tengo.Object, error) {
  466. if len(args) != 1 {
  467. return nil, tengo.ErrWrongNumArguments
  468. }
  469. s1, ok := tengo.ToString(args[0])
  470. if !ok {
  471. return nil, tengo.ErrInvalidArgumentType{
  472. Name: "first",
  473. Expected: "string(compatible)",
  474. Found: args[0].TypeName(),
  475. }
  476. }
  477. res, err := fn(s1)
  478. if err != nil {
  479. return wrapError(err), nil
  480. }
  481. if len(res) > tengo.MaxStringLen {
  482. return nil, tengo.ErrStringLimit
  483. }
  484. return &tengo.String{Value: res}, nil
  485. }
  486. }
  487. // FuncASRE transform a function of 'func(string) error' signature into
  488. // CallableFunc type. User function will return 'true' if underlying native
  489. // function returns nil.
  490. func FuncASRE(fn func(string) error) tengo.CallableFunc {
  491. return func(args ...tengo.Object) (tengo.Object, error) {
  492. if len(args) != 1 {
  493. return nil, tengo.ErrWrongNumArguments
  494. }
  495. s1, ok := tengo.ToString(args[0])
  496. if !ok {
  497. return nil, tengo.ErrInvalidArgumentType{
  498. Name: "first",
  499. Expected: "string(compatible)",
  500. Found: args[0].TypeName(),
  501. }
  502. }
  503. return wrapError(fn(s1)), nil
  504. }
  505. }
  506. // FuncASSRE transform a function of 'func(string, string) error' signature
  507. // into CallableFunc type. User function will return 'true' if underlying
  508. // native function returns nil.
  509. func FuncASSRE(fn func(string, string) error) tengo.CallableFunc {
  510. return func(args ...tengo.Object) (tengo.Object, error) {
  511. if len(args) != 2 {
  512. return nil, tengo.ErrWrongNumArguments
  513. }
  514. s1, ok := tengo.ToString(args[0])
  515. if !ok {
  516. return nil, tengo.ErrInvalidArgumentType{
  517. Name: "first",
  518. Expected: "string(compatible)",
  519. Found: args[0].TypeName(),
  520. }
  521. }
  522. s2, ok := tengo.ToString(args[1])
  523. if !ok {
  524. return nil, tengo.ErrInvalidArgumentType{
  525. Name: "second",
  526. Expected: "string(compatible)",
  527. Found: args[1].TypeName(),
  528. }
  529. }
  530. return wrapError(fn(s1, s2)), nil
  531. }
  532. }
  533. // FuncASSRSs transform a function of 'func(string, string) []string'
  534. // signature into CallableFunc type.
  535. func FuncASSRSs(fn func(string, string) []string) tengo.CallableFunc {
  536. return func(args ...tengo.Object) (tengo.Object, error) {
  537. if len(args) != 2 {
  538. return nil, tengo.ErrWrongNumArguments
  539. }
  540. s1, ok := tengo.ToString(args[0])
  541. if !ok {
  542. return nil, tengo.ErrInvalidArgumentType{
  543. Name: "first",
  544. Expected: "string(compatible)",
  545. Found: args[0].TypeName(),
  546. }
  547. }
  548. s2, ok := tengo.ToString(args[1])
  549. if !ok {
  550. return nil, tengo.ErrInvalidArgumentType{
  551. Name: "first",
  552. Expected: "string(compatible)",
  553. Found: args[1].TypeName(),
  554. }
  555. }
  556. arr := &tengo.Array{}
  557. for _, res := range fn(s1, s2) {
  558. if len(res) > tengo.MaxStringLen {
  559. return nil, tengo.ErrStringLimit
  560. }
  561. arr.Value = append(arr.Value, &tengo.String{Value: res})
  562. }
  563. return arr, nil
  564. }
  565. }
  566. // FuncASSIRSs transform a function of 'func(string, string, int) []string'
  567. // signature into CallableFunc type.
  568. func FuncASSIRSs(fn func(string, string, int) []string) tengo.CallableFunc {
  569. return func(args ...tengo.Object) (tengo.Object, error) {
  570. if len(args) != 3 {
  571. return nil, tengo.ErrWrongNumArguments
  572. }
  573. s1, ok := tengo.ToString(args[0])
  574. if !ok {
  575. return nil, tengo.ErrInvalidArgumentType{
  576. Name: "first",
  577. Expected: "string(compatible)",
  578. Found: args[0].TypeName(),
  579. }
  580. }
  581. s2, ok := tengo.ToString(args[1])
  582. if !ok {
  583. return nil, tengo.ErrInvalidArgumentType{
  584. Name: "second",
  585. Expected: "string(compatible)",
  586. Found: args[1].TypeName(),
  587. }
  588. }
  589. i3, ok := tengo.ToInt(args[2])
  590. if !ok {
  591. return nil, tengo.ErrInvalidArgumentType{
  592. Name: "third",
  593. Expected: "int(compatible)",
  594. Found: args[2].TypeName(),
  595. }
  596. }
  597. arr := &tengo.Array{}
  598. for _, res := range fn(s1, s2, i3) {
  599. if len(res) > tengo.MaxStringLen {
  600. return nil, tengo.ErrStringLimit
  601. }
  602. arr.Value = append(arr.Value, &tengo.String{Value: res})
  603. }
  604. return arr, nil
  605. }
  606. }
  607. // FuncASSRI transform a function of 'func(string, string) int' signature into
  608. // CallableFunc type.
  609. func FuncASSRI(fn func(string, string) int) tengo.CallableFunc {
  610. return func(args ...tengo.Object) (tengo.Object, error) {
  611. if len(args) != 2 {
  612. return nil, tengo.ErrWrongNumArguments
  613. }
  614. s1, ok := tengo.ToString(args[0])
  615. if !ok {
  616. return nil, tengo.ErrInvalidArgumentType{
  617. Name: "first",
  618. Expected: "string(compatible)",
  619. Found: args[0].TypeName(),
  620. }
  621. }
  622. s2, ok := tengo.ToString(args[1])
  623. if !ok {
  624. return nil, tengo.ErrInvalidArgumentType{
  625. Name: "second",
  626. Expected: "string(compatible)",
  627. Found: args[0].TypeName(),
  628. }
  629. }
  630. return &tengo.Int{Value: int64(fn(s1, s2))}, nil
  631. }
  632. }
  633. // FuncASSRS transform a function of 'func(string, string) string' signature
  634. // into CallableFunc type.
  635. func FuncASSRS(fn func(string, string) string) tengo.CallableFunc {
  636. return func(args ...tengo.Object) (tengo.Object, error) {
  637. if len(args) != 2 {
  638. return nil, tengo.ErrWrongNumArguments
  639. }
  640. s1, ok := tengo.ToString(args[0])
  641. if !ok {
  642. return nil, tengo.ErrInvalidArgumentType{
  643. Name: "first",
  644. Expected: "string(compatible)",
  645. Found: args[0].TypeName(),
  646. }
  647. }
  648. s2, ok := tengo.ToString(args[1])
  649. if !ok {
  650. return nil, tengo.ErrInvalidArgumentType{
  651. Name: "second",
  652. Expected: "string(compatible)",
  653. Found: args[1].TypeName(),
  654. }
  655. }
  656. s := fn(s1, s2)
  657. if len(s) > tengo.MaxStringLen {
  658. return nil, tengo.ErrStringLimit
  659. }
  660. return &tengo.String{Value: s}, nil
  661. }
  662. }
  663. // FuncASSRB transform a function of 'func(string, string) bool' signature
  664. // into CallableFunc type.
  665. func FuncASSRB(fn func(string, string) bool) tengo.CallableFunc {
  666. return func(args ...tengo.Object) (tengo.Object, error) {
  667. if len(args) != 2 {
  668. return nil, tengo.ErrWrongNumArguments
  669. }
  670. s1, ok := tengo.ToString(args[0])
  671. if !ok {
  672. return nil, tengo.ErrInvalidArgumentType{
  673. Name: "first",
  674. Expected: "string(compatible)",
  675. Found: args[0].TypeName(),
  676. }
  677. }
  678. s2, ok := tengo.ToString(args[1])
  679. if !ok {
  680. return nil, tengo.ErrInvalidArgumentType{
  681. Name: "second",
  682. Expected: "string(compatible)",
  683. Found: args[1].TypeName(),
  684. }
  685. }
  686. if fn(s1, s2) {
  687. return tengo.TrueValue, nil
  688. }
  689. return tengo.FalseValue, nil
  690. }
  691. }
  692. // FuncASsSRS transform a function of 'func([]string, string) string' signature
  693. // into CallableFunc type.
  694. func FuncASsSRS(fn func([]string, string) string) tengo.CallableFunc {
  695. return func(args ...tengo.Object) (tengo.Object, error) {
  696. if len(args) != 2 {
  697. return nil, tengo.ErrWrongNumArguments
  698. }
  699. var ss1 []string
  700. switch arg0 := args[0].(type) {
  701. case *tengo.Array:
  702. for idx, a := range arg0.Value {
  703. as, ok := tengo.ToString(a)
  704. if !ok {
  705. return nil, tengo.ErrInvalidArgumentType{
  706. Name: fmt.Sprintf("first[%d]", idx),
  707. Expected: "string(compatible)",
  708. Found: a.TypeName(),
  709. }
  710. }
  711. ss1 = append(ss1, as)
  712. }
  713. case *tengo.ImmutableArray:
  714. for idx, a := range arg0.Value {
  715. as, ok := tengo.ToString(a)
  716. if !ok {
  717. return nil, tengo.ErrInvalidArgumentType{
  718. Name: fmt.Sprintf("first[%d]", idx),
  719. Expected: "string(compatible)",
  720. Found: a.TypeName(),
  721. }
  722. }
  723. ss1 = append(ss1, as)
  724. }
  725. default:
  726. return nil, tengo.ErrInvalidArgumentType{
  727. Name: "first",
  728. Expected: "array",
  729. Found: args[0].TypeName(),
  730. }
  731. }
  732. s2, ok := tengo.ToString(args[1])
  733. if !ok {
  734. return nil, tengo.ErrInvalidArgumentType{
  735. Name: "second",
  736. Expected: "string(compatible)",
  737. Found: args[1].TypeName(),
  738. }
  739. }
  740. s := fn(ss1, s2)
  741. if len(s) > tengo.MaxStringLen {
  742. return nil, tengo.ErrStringLimit
  743. }
  744. return &tengo.String{Value: s}, nil
  745. }
  746. }
  747. // FuncASI64RE transform a function of 'func(string, int64) error' signature
  748. // into CallableFunc type.
  749. func FuncASI64RE(fn func(string, int64) error) tengo.CallableFunc {
  750. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  751. if len(args) != 2 {
  752. return nil, tengo.ErrWrongNumArguments
  753. }
  754. s1, ok := tengo.ToString(args[0])
  755. if !ok {
  756. return nil, tengo.ErrInvalidArgumentType{
  757. Name: "first",
  758. Expected: "string(compatible)",
  759. Found: args[0].TypeName(),
  760. }
  761. }
  762. i2, ok := tengo.ToInt64(args[1])
  763. if !ok {
  764. return nil, tengo.ErrInvalidArgumentType{
  765. Name: "second",
  766. Expected: "int(compatible)",
  767. Found: args[1].TypeName(),
  768. }
  769. }
  770. return wrapError(fn(s1, i2)), nil
  771. }
  772. }
  773. // FuncAIIRE transform a function of 'func(int, int) error' signature
  774. // into CallableFunc type.
  775. func FuncAIIRE(fn func(int, int) error) tengo.CallableFunc {
  776. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  777. if len(args) != 2 {
  778. return nil, tengo.ErrWrongNumArguments
  779. }
  780. i1, ok := tengo.ToInt(args[0])
  781. if !ok {
  782. return nil, tengo.ErrInvalidArgumentType{
  783. Name: "first",
  784. Expected: "int(compatible)",
  785. Found: args[0].TypeName(),
  786. }
  787. }
  788. i2, ok := tengo.ToInt(args[1])
  789. if !ok {
  790. return nil, tengo.ErrInvalidArgumentType{
  791. Name: "second",
  792. Expected: "int(compatible)",
  793. Found: args[1].TypeName(),
  794. }
  795. }
  796. return wrapError(fn(i1, i2)), nil
  797. }
  798. }
  799. // FuncASIRS transform a function of 'func(string, int) string' signature
  800. // into CallableFunc type.
  801. func FuncASIRS(fn func(string, int) string) tengo.CallableFunc {
  802. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  803. if len(args) != 2 {
  804. return nil, tengo.ErrWrongNumArguments
  805. }
  806. s1, ok := tengo.ToString(args[0])
  807. if !ok {
  808. return nil, tengo.ErrInvalidArgumentType{
  809. Name: "first",
  810. Expected: "string(compatible)",
  811. Found: args[0].TypeName(),
  812. }
  813. }
  814. i2, ok := tengo.ToInt(args[1])
  815. if !ok {
  816. return nil, tengo.ErrInvalidArgumentType{
  817. Name: "second",
  818. Expected: "int(compatible)",
  819. Found: args[1].TypeName(),
  820. }
  821. }
  822. s := fn(s1, i2)
  823. if len(s) > tengo.MaxStringLen {
  824. return nil, tengo.ErrStringLimit
  825. }
  826. return &tengo.String{Value: s}, nil
  827. }
  828. }
  829. // FuncASIIRE transform a function of 'func(string, int, int) error' signature
  830. // into CallableFunc type.
  831. func FuncASIIRE(fn func(string, int, int) error) tengo.CallableFunc {
  832. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  833. if len(args) != 3 {
  834. return nil, tengo.ErrWrongNumArguments
  835. }
  836. s1, ok := tengo.ToString(args[0])
  837. if !ok {
  838. return nil, tengo.ErrInvalidArgumentType{
  839. Name: "first",
  840. Expected: "string(compatible)",
  841. Found: args[0].TypeName(),
  842. }
  843. }
  844. i2, ok := tengo.ToInt(args[1])
  845. if !ok {
  846. return nil, tengo.ErrInvalidArgumentType{
  847. Name: "second",
  848. Expected: "int(compatible)",
  849. Found: args[1].TypeName(),
  850. }
  851. }
  852. i3, ok := tengo.ToInt(args[2])
  853. if !ok {
  854. return nil, tengo.ErrInvalidArgumentType{
  855. Name: "third",
  856. Expected: "int(compatible)",
  857. Found: args[2].TypeName(),
  858. }
  859. }
  860. return wrapError(fn(s1, i2, i3)), nil
  861. }
  862. }
  863. // FuncAYRIE transform a function of 'func([]byte) (int, error)' signature
  864. // into CallableFunc type.
  865. func FuncAYRIE(fn func([]byte) (int, error)) tengo.CallableFunc {
  866. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  867. if len(args) != 1 {
  868. return nil, tengo.ErrWrongNumArguments
  869. }
  870. y1, ok := tengo.ToByteSlice(args[0])
  871. if !ok {
  872. return nil, tengo.ErrInvalidArgumentType{
  873. Name: "first",
  874. Expected: "bytes(compatible)",
  875. Found: args[0].TypeName(),
  876. }
  877. }
  878. res, err := fn(y1)
  879. if err != nil {
  880. return wrapError(err), nil
  881. }
  882. return &tengo.Int{Value: int64(res)}, nil
  883. }
  884. }
  885. // FuncAYRS transform a function of 'func([]byte) string' signature into
  886. // CallableFunc type.
  887. func FuncAYRS(fn func([]byte) string) tengo.CallableFunc {
  888. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  889. if len(args) != 1 {
  890. return nil, tengo.ErrWrongNumArguments
  891. }
  892. y1, ok := tengo.ToByteSlice(args[0])
  893. if !ok {
  894. return nil, tengo.ErrInvalidArgumentType{
  895. Name: "first",
  896. Expected: "bytes(compatible)",
  897. Found: args[0].TypeName(),
  898. }
  899. }
  900. res := fn(y1)
  901. return &tengo.String{Value: res}, nil
  902. }
  903. }
  904. // FuncASRIE transform a function of 'func(string) (int, error)' signature
  905. // into CallableFunc type.
  906. func FuncASRIE(fn func(string) (int, error)) tengo.CallableFunc {
  907. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  908. if len(args) != 1 {
  909. return nil, tengo.ErrWrongNumArguments
  910. }
  911. s1, ok := tengo.ToString(args[0])
  912. if !ok {
  913. return nil, tengo.ErrInvalidArgumentType{
  914. Name: "first",
  915. Expected: "string(compatible)",
  916. Found: args[0].TypeName(),
  917. }
  918. }
  919. res, err := fn(s1)
  920. if err != nil {
  921. return wrapError(err), nil
  922. }
  923. return &tengo.Int{Value: int64(res)}, nil
  924. }
  925. }
  926. // FuncASRYE transform a function of 'func(string) ([]byte, error)' signature
  927. // into CallableFunc type.
  928. func FuncASRYE(fn func(string) ([]byte, error)) tengo.CallableFunc {
  929. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  930. if len(args) != 1 {
  931. return nil, tengo.ErrWrongNumArguments
  932. }
  933. s1, ok := tengo.ToString(args[0])
  934. if !ok {
  935. return nil, tengo.ErrInvalidArgumentType{
  936. Name: "first",
  937. Expected: "string(compatible)",
  938. Found: args[0].TypeName(),
  939. }
  940. }
  941. res, err := fn(s1)
  942. if err != nil {
  943. return wrapError(err), nil
  944. }
  945. if len(res) > tengo.MaxBytesLen {
  946. return nil, tengo.ErrBytesLimit
  947. }
  948. return &tengo.Bytes{Value: res}, nil
  949. }
  950. }
  951. // FuncAIRSsE transform a function of 'func(int) ([]string, error)' signature
  952. // into CallableFunc type.
  953. func FuncAIRSsE(fn func(int) ([]string, error)) tengo.CallableFunc {
  954. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  955. if len(args) != 1 {
  956. return nil, tengo.ErrWrongNumArguments
  957. }
  958. i1, ok := tengo.ToInt(args[0])
  959. if !ok {
  960. return nil, tengo.ErrInvalidArgumentType{
  961. Name: "first",
  962. Expected: "int(compatible)",
  963. Found: args[0].TypeName(),
  964. }
  965. }
  966. res, err := fn(i1)
  967. if err != nil {
  968. return wrapError(err), nil
  969. }
  970. arr := &tengo.Array{}
  971. for _, r := range res {
  972. if len(r) > tengo.MaxStringLen {
  973. return nil, tengo.ErrStringLimit
  974. }
  975. arr.Value = append(arr.Value, &tengo.String{Value: r})
  976. }
  977. return arr, nil
  978. }
  979. }
  980. // FuncAIRS transform a function of 'func(int) string' signature into
  981. // CallableFunc type.
  982. func FuncAIRS(fn func(int) string) tengo.CallableFunc {
  983. return func(args ...tengo.Object) (ret tengo.Object, err error) {
  984. if len(args) != 1 {
  985. return nil, tengo.ErrWrongNumArguments
  986. }
  987. i1, ok := tengo.ToInt(args[0])
  988. if !ok {
  989. return nil, tengo.ErrInvalidArgumentType{
  990. Name: "first",
  991. Expected: "int(compatible)",
  992. Found: args[0].TypeName(),
  993. }
  994. }
  995. s := fn(i1)
  996. if len(s) > tengo.MaxStringLen {
  997. return nil, tengo.ErrStringLimit
  998. }
  999. return &tengo.String{Value: s}, nil
  1000. }
  1001. }