objects.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. package tengo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/d5/tengo/v2/parser"
  10. "github.com/d5/tengo/v2/token"
  11. )
  12. var (
  13. // TrueValue represents a true value.
  14. TrueValue Object = &Bool{value: true}
  15. // FalseValue represents a false value.
  16. FalseValue Object = &Bool{value: false}
  17. // UndefinedValue represents an undefined value.
  18. UndefinedValue Object = &Undefined{}
  19. )
  20. // Object represents an object in the VM.
  21. type Object interface {
  22. // TypeName should return the name of the type.
  23. TypeName() string
  24. // String should return a string representation of the type's value.
  25. String() string
  26. // BinaryOp should return another object that is the result of a given
  27. // binary operator and a right-hand side object. If BinaryOp returns an
  28. // error, the VM will treat it as a run-time error.
  29. BinaryOp(op token.Token, rhs Object) (Object, error)
  30. // IsFalsy should return true if the value of the type should be considered
  31. // as falsy.
  32. IsFalsy() bool
  33. // Equals should return true if the value of the type should be considered
  34. // as equal to the value of another object.
  35. Equals(another Object) bool
  36. // Copy should return a copy of the type (and its value). Copy function
  37. // will be used for copy() builtin function which is expected to deep-copy
  38. // the values generally.
  39. Copy() Object
  40. // IndexGet should take an index Object and return a result Object or an
  41. // error for indexable objects. Indexable is an object that can take an
  42. // index and return an object. If error is returned, the runtime will treat
  43. // it as a run-time error and ignore returned value. If Object is not
  44. // indexable, ErrNotIndexable should be returned as error. If nil is
  45. // returned as value, it will be converted to UndefinedToken value by the
  46. // runtime.
  47. IndexGet(index Object) (value Object, err error)
  48. // IndexSet should take an index Object and a value Object for index
  49. // assignable objects. Index assignable is an object that can take an index
  50. // and a value on the left-hand side of the assignment statement. If Object
  51. // is not index assignable, ErrNotIndexAssignable should be returned as
  52. // error. If an error is returned, it will be treated as a run-time error.
  53. IndexSet(index, value Object) error
  54. // Iterate should return an Iterator for the type.
  55. Iterate() Iterator
  56. // CanIterate should return whether the Object can be Iterated.
  57. CanIterate() bool
  58. // Call should take an arbitrary number of arguments and returns a return
  59. // value and/or an error, which the VM will consider as a run-time error.
  60. Call(args ...Object) (ret Object, err error)
  61. // CanCall should return whether the Object can be Called.
  62. CanCall() bool
  63. }
  64. // ObjectImpl represents a default Object Implementation. To defined a new
  65. // value type, one can embed ObjectImpl in their type declarations to avoid
  66. // implementing all non-significant methods. TypeName() and String() methods
  67. // still need to be implemented.
  68. type ObjectImpl struct {
  69. }
  70. // TypeName returns the name of the type.
  71. func (o *ObjectImpl) TypeName() string {
  72. panic(ErrNotImplemented)
  73. }
  74. func (o *ObjectImpl) String() string {
  75. panic(ErrNotImplemented)
  76. }
  77. // BinaryOp returns another object that is the result of a given binary
  78. // operator and a right-hand side object.
  79. func (o *ObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error) {
  80. return nil, ErrInvalidOperator
  81. }
  82. // Copy returns a copy of the type.
  83. func (o *ObjectImpl) Copy() Object {
  84. return nil
  85. }
  86. // IsFalsy returns true if the value of the type is falsy.
  87. func (o *ObjectImpl) IsFalsy() bool {
  88. return false
  89. }
  90. // Equals returns true if the value of the type is equal to the value of
  91. // another object.
  92. func (o *ObjectImpl) Equals(x Object) bool {
  93. return o == x
  94. }
  95. // IndexGet returns an element at a given index.
  96. func (o *ObjectImpl) IndexGet(_ Object) (res Object, err error) {
  97. return nil, ErrNotIndexable
  98. }
  99. // IndexSet sets an element at a given index.
  100. func (o *ObjectImpl) IndexSet(_, _ Object) (err error) {
  101. return ErrNotIndexAssignable
  102. }
  103. // Iterate returns an iterator.
  104. func (o *ObjectImpl) Iterate() Iterator {
  105. return nil
  106. }
  107. // CanIterate returns whether the Object can be Iterated.
  108. func (o *ObjectImpl) CanIterate() bool {
  109. return false
  110. }
  111. // Call takes an arbitrary number of arguments and returns a return value
  112. // and/or an error.
  113. func (o *ObjectImpl) Call(_ ...Object) (ret Object, err error) {
  114. return nil, nil
  115. }
  116. // CanCall returns whether the Object can be Called.
  117. func (o *ObjectImpl) CanCall() bool {
  118. return false
  119. }
  120. // Array represents an array of objects.
  121. type Array struct {
  122. ObjectImpl
  123. Value []Object
  124. }
  125. // TypeName returns the name of the type.
  126. func (o *Array) TypeName() string {
  127. return "array"
  128. }
  129. func (o *Array) String() string {
  130. var elements []string
  131. for _, e := range o.Value {
  132. elements = append(elements, e.String())
  133. }
  134. return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
  135. }
  136. // BinaryOp returns another object that is the result of a given binary
  137. // operator and a right-hand side object.
  138. func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error) {
  139. if rhs, ok := rhs.(*Array); ok {
  140. switch op {
  141. case token.Add:
  142. if len(rhs.Value) == 0 {
  143. return o, nil
  144. }
  145. return &Array{Value: append(o.Value, rhs.Value...)}, nil
  146. }
  147. }
  148. return nil, ErrInvalidOperator
  149. }
  150. // Copy returns a copy of the type.
  151. func (o *Array) Copy() Object {
  152. var c []Object
  153. for _, elem := range o.Value {
  154. c = append(c, elem.Copy())
  155. }
  156. return &Array{Value: c}
  157. }
  158. // IsFalsy returns true if the value of the type is falsy.
  159. func (o *Array) IsFalsy() bool {
  160. return len(o.Value) == 0
  161. }
  162. // Equals returns true if the value of the type is equal to the value of
  163. // another object.
  164. func (o *Array) Equals(x Object) bool {
  165. var xVal []Object
  166. switch x := x.(type) {
  167. case *Array:
  168. xVal = x.Value
  169. case *ImmutableArray:
  170. xVal = x.Value
  171. default:
  172. return false
  173. }
  174. if len(o.Value) != len(xVal) {
  175. return false
  176. }
  177. for i, e := range o.Value {
  178. if !e.Equals(xVal[i]) {
  179. return false
  180. }
  181. }
  182. return true
  183. }
  184. // IndexGet returns an element at a given index.
  185. func (o *Array) IndexGet(index Object) (res Object, err error) {
  186. intIdx, ok := index.(*Int)
  187. if !ok {
  188. err = ErrInvalidIndexType
  189. return
  190. }
  191. idxVal := int(intIdx.Value)
  192. if idxVal < 0 || idxVal >= len(o.Value) {
  193. res = UndefinedValue
  194. return
  195. }
  196. res = o.Value[idxVal]
  197. return
  198. }
  199. // IndexSet sets an element at a given index.
  200. func (o *Array) IndexSet(index, value Object) (err error) {
  201. intIdx, ok := ToInt(index)
  202. if !ok {
  203. err = ErrInvalidIndexType
  204. return
  205. }
  206. if intIdx < 0 || intIdx >= len(o.Value) {
  207. err = ErrIndexOutOfBounds
  208. return
  209. }
  210. o.Value[intIdx] = value
  211. return nil
  212. }
  213. // Iterate creates an array iterator.
  214. func (o *Array) Iterate() Iterator {
  215. return &ArrayIterator{
  216. v: o.Value,
  217. l: len(o.Value),
  218. }
  219. }
  220. // CanIterate returns whether the Object can be Iterated.
  221. func (o *Array) CanIterate() bool {
  222. return true
  223. }
  224. // Bool represents a boolean value.
  225. type Bool struct {
  226. ObjectImpl
  227. // this is intentionally non-public to force using objects.TrueValue and
  228. // FalseValue always
  229. value bool
  230. }
  231. func (o *Bool) String() string {
  232. if o.value {
  233. return "true"
  234. }
  235. return "false"
  236. }
  237. // TypeName returns the name of the type.
  238. func (o *Bool) TypeName() string {
  239. return "bool"
  240. }
  241. // Copy returns a copy of the type.
  242. func (o *Bool) Copy() Object {
  243. return o
  244. }
  245. // IsFalsy returns true if the value of the type is falsy.
  246. func (o *Bool) IsFalsy() bool {
  247. return !o.value
  248. }
  249. // Equals returns true if the value of the type is equal to the value of
  250. // another object.
  251. func (o *Bool) Equals(x Object) bool {
  252. return o == x
  253. }
  254. // GobDecode decodes bool value from input bytes.
  255. func (o *Bool) GobDecode(b []byte) (err error) {
  256. o.value = b[0] == 1
  257. return
  258. }
  259. // GobEncode encodes bool values into bytes.
  260. func (o *Bool) GobEncode() (b []byte, err error) {
  261. if o.value {
  262. b = []byte{1}
  263. } else {
  264. b = []byte{0}
  265. }
  266. return
  267. }
  268. // BuiltinFunction represents a builtin function.
  269. type BuiltinFunction struct {
  270. ObjectImpl
  271. Name string
  272. Value CallableFunc
  273. }
  274. // TypeName returns the name of the type.
  275. func (o *BuiltinFunction) TypeName() string {
  276. return "builtin-function:" + o.Name
  277. }
  278. func (o *BuiltinFunction) String() string {
  279. return "<builtin-function>"
  280. }
  281. // Copy returns a copy of the type.
  282. func (o *BuiltinFunction) Copy() Object {
  283. return &BuiltinFunction{Value: o.Value}
  284. }
  285. // Equals returns true if the value of the type is equal to the value of
  286. // another object.
  287. func (o *BuiltinFunction) Equals(_ Object) bool {
  288. return false
  289. }
  290. // Call executes a builtin function.
  291. func (o *BuiltinFunction) Call(args ...Object) (Object, error) {
  292. return o.Value(args...)
  293. }
  294. // CanCall returns whether the Object can be Called.
  295. func (o *BuiltinFunction) CanCall() bool {
  296. return true
  297. }
  298. // BuiltinModule is an importable module that's written in Go.
  299. type BuiltinModule struct {
  300. Attrs map[string]Object
  301. }
  302. // Import returns an immutable map for the module.
  303. func (m *BuiltinModule) Import(moduleName string) (interface{}, error) {
  304. return m.AsImmutableMap(moduleName), nil
  305. }
  306. // AsImmutableMap converts builtin module into an immutable map.
  307. func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap {
  308. attrs := make(map[string]Object, len(m.Attrs))
  309. for k, v := range m.Attrs {
  310. attrs[k] = v.Copy()
  311. }
  312. attrs["__module_name__"] = &String{Value: moduleName}
  313. return &ImmutableMap{Value: attrs}
  314. }
  315. // Bytes represents a byte array.
  316. type Bytes struct {
  317. ObjectImpl
  318. Value []byte
  319. }
  320. func (o *Bytes) String() string {
  321. return string(o.Value)
  322. }
  323. // TypeName returns the name of the type.
  324. func (o *Bytes) TypeName() string {
  325. return "bytes"
  326. }
  327. // BinaryOp returns another object that is the result of a given binary
  328. // operator and a right-hand side object.
  329. func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) {
  330. switch op {
  331. case token.Add:
  332. switch rhs := rhs.(type) {
  333. case *Bytes:
  334. if len(o.Value)+len(rhs.Value) > MaxBytesLen {
  335. return nil, ErrBytesLimit
  336. }
  337. return &Bytes{Value: append(o.Value, rhs.Value...)}, nil
  338. }
  339. }
  340. return nil, ErrInvalidOperator
  341. }
  342. // Copy returns a copy of the type.
  343. func (o *Bytes) Copy() Object {
  344. return &Bytes{Value: append([]byte{}, o.Value...)}
  345. }
  346. // IsFalsy returns true if the value of the type is falsy.
  347. func (o *Bytes) IsFalsy() bool {
  348. return len(o.Value) == 0
  349. }
  350. // Equals returns true if the value of the type is equal to the value of
  351. // another object.
  352. func (o *Bytes) Equals(x Object) bool {
  353. t, ok := x.(*Bytes)
  354. if !ok {
  355. return false
  356. }
  357. return bytes.Equal(o.Value, t.Value)
  358. }
  359. // IndexGet returns an element (as Int) at a given index.
  360. func (o *Bytes) IndexGet(index Object) (res Object, err error) {
  361. intIdx, ok := index.(*Int)
  362. if !ok {
  363. err = ErrInvalidIndexType
  364. return
  365. }
  366. idxVal := int(intIdx.Value)
  367. if idxVal < 0 || idxVal >= len(o.Value) {
  368. res = UndefinedValue
  369. return
  370. }
  371. res = &Int{Value: int64(o.Value[idxVal])}
  372. return
  373. }
  374. // Iterate creates a bytes iterator.
  375. func (o *Bytes) Iterate() Iterator {
  376. return &BytesIterator{
  377. v: o.Value,
  378. l: len(o.Value),
  379. }
  380. }
  381. // CanIterate returns whether the Object can be Iterated.
  382. func (o *Bytes) CanIterate() bool {
  383. return true
  384. }
  385. // Char represents a character value.
  386. type Char struct {
  387. ObjectImpl
  388. Value rune
  389. }
  390. func (o *Char) String() string {
  391. return string(o.Value)
  392. }
  393. // TypeName returns the name of the type.
  394. func (o *Char) TypeName() string {
  395. return "char"
  396. }
  397. // BinaryOp returns another object that is the result of a given binary
  398. // operator and a right-hand side object.
  399. func (o *Char) BinaryOp(op token.Token, rhs Object) (Object, error) {
  400. switch rhs := rhs.(type) {
  401. case *Char:
  402. switch op {
  403. case token.Add:
  404. r := o.Value + rhs.Value
  405. if r == o.Value {
  406. return o, nil
  407. }
  408. return &Char{Value: r}, nil
  409. case token.Sub:
  410. r := o.Value - rhs.Value
  411. if r == o.Value {
  412. return o, nil
  413. }
  414. return &Char{Value: r}, nil
  415. case token.Less:
  416. if o.Value < rhs.Value {
  417. return TrueValue, nil
  418. }
  419. return FalseValue, nil
  420. case token.Greater:
  421. if o.Value > rhs.Value {
  422. return TrueValue, nil
  423. }
  424. return FalseValue, nil
  425. case token.LessEq:
  426. if o.Value <= rhs.Value {
  427. return TrueValue, nil
  428. }
  429. return FalseValue, nil
  430. case token.GreaterEq:
  431. if o.Value >= rhs.Value {
  432. return TrueValue, nil
  433. }
  434. return FalseValue, nil
  435. }
  436. case *Int:
  437. switch op {
  438. case token.Add:
  439. r := o.Value + rune(rhs.Value)
  440. if r == o.Value {
  441. return o, nil
  442. }
  443. return &Char{Value: r}, nil
  444. case token.Sub:
  445. r := o.Value - rune(rhs.Value)
  446. if r == o.Value {
  447. return o, nil
  448. }
  449. return &Char{Value: r}, nil
  450. case token.Less:
  451. if int64(o.Value) < rhs.Value {
  452. return TrueValue, nil
  453. }
  454. return FalseValue, nil
  455. case token.Greater:
  456. if int64(o.Value) > rhs.Value {
  457. return TrueValue, nil
  458. }
  459. return FalseValue, nil
  460. case token.LessEq:
  461. if int64(o.Value) <= rhs.Value {
  462. return TrueValue, nil
  463. }
  464. return FalseValue, nil
  465. case token.GreaterEq:
  466. if int64(o.Value) >= rhs.Value {
  467. return TrueValue, nil
  468. }
  469. return FalseValue, nil
  470. }
  471. }
  472. return nil, ErrInvalidOperator
  473. }
  474. // Copy returns a copy of the type.
  475. func (o *Char) Copy() Object {
  476. return &Char{Value: o.Value}
  477. }
  478. // IsFalsy returns true if the value of the type is falsy.
  479. func (o *Char) IsFalsy() bool {
  480. return o.Value == 0
  481. }
  482. // Equals returns true if the value of the type is equal to the value of
  483. // another object.
  484. func (o *Char) Equals(x Object) bool {
  485. t, ok := x.(*Char)
  486. if !ok {
  487. return false
  488. }
  489. return o.Value == t.Value
  490. }
  491. // CompiledFunction represents a compiled function.
  492. type CompiledFunction struct {
  493. ObjectImpl
  494. Instructions []byte
  495. NumLocals int // number of local variables (including function parameters)
  496. NumParameters int
  497. VarArgs bool
  498. SourceMap map[int]parser.Pos
  499. Free []*ObjectPtr
  500. }
  501. // TypeName returns the name of the type.
  502. func (o *CompiledFunction) TypeName() string {
  503. return "compiled-function"
  504. }
  505. func (o *CompiledFunction) String() string {
  506. return "<compiled-function>"
  507. }
  508. // Copy returns a copy of the type.
  509. func (o *CompiledFunction) Copy() Object {
  510. return &CompiledFunction{
  511. Instructions: append([]byte{}, o.Instructions...),
  512. NumLocals: o.NumLocals,
  513. NumParameters: o.NumParameters,
  514. VarArgs: o.VarArgs,
  515. Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers
  516. }
  517. }
  518. // Equals returns true if the value of the type is equal to the value of
  519. // another object.
  520. func (o *CompiledFunction) Equals(_ Object) bool {
  521. return false
  522. }
  523. // SourcePos returns the source position of the instruction at ip.
  524. func (o *CompiledFunction) SourcePos(ip int) parser.Pos {
  525. for ip >= 0 {
  526. if p, ok := o.SourceMap[ip]; ok {
  527. return p
  528. }
  529. ip--
  530. }
  531. return parser.NoPos
  532. }
  533. // CanCall returns whether the Object can be Called.
  534. func (o *CompiledFunction) CanCall() bool {
  535. return true
  536. }
  537. // Error represents an error value.
  538. type Error struct {
  539. ObjectImpl
  540. Value Object
  541. }
  542. // TypeName returns the name of the type.
  543. func (o *Error) TypeName() string {
  544. return "error"
  545. }
  546. func (o *Error) String() string {
  547. if o.Value != nil {
  548. return fmt.Sprintf("error: %s", o.Value.String())
  549. }
  550. return "error"
  551. }
  552. // IsFalsy returns true if the value of the type is falsy.
  553. func (o *Error) IsFalsy() bool {
  554. return true // error is always false.
  555. }
  556. // Copy returns a copy of the type.
  557. func (o *Error) Copy() Object {
  558. return &Error{Value: o.Value.Copy()}
  559. }
  560. // Equals returns true if the value of the type is equal to the value of
  561. // another object.
  562. func (o *Error) Equals(x Object) bool {
  563. return o == x // pointer equality
  564. }
  565. // IndexGet returns an element at a given index.
  566. func (o *Error) IndexGet(index Object) (res Object, err error) {
  567. if strIdx, _ := ToString(index); strIdx != "value" {
  568. err = ErrInvalidIndexOnError
  569. return
  570. }
  571. res = o.Value
  572. return
  573. }
  574. // Float represents a floating point number value.
  575. type Float struct {
  576. ObjectImpl
  577. Value float64
  578. }
  579. func (o *Float) String() string {
  580. return strconv.FormatFloat(o.Value, 'f', -1, 64)
  581. }
  582. // TypeName returns the name of the type.
  583. func (o *Float) TypeName() string {
  584. return "float"
  585. }
  586. // BinaryOp returns another object that is the result of a given binary
  587. // operator and a right-hand side object.
  588. func (o *Float) BinaryOp(op token.Token, rhs Object) (Object, error) {
  589. switch rhs := rhs.(type) {
  590. case *Float:
  591. switch op {
  592. case token.Add:
  593. r := o.Value + rhs.Value
  594. if r == o.Value {
  595. return o, nil
  596. }
  597. return &Float{Value: r}, nil
  598. case token.Sub:
  599. r := o.Value - rhs.Value
  600. if r == o.Value {
  601. return o, nil
  602. }
  603. return &Float{Value: r}, nil
  604. case token.Mul:
  605. r := o.Value * rhs.Value
  606. if r == o.Value {
  607. return o, nil
  608. }
  609. return &Float{Value: r}, nil
  610. case token.Quo:
  611. r := o.Value / rhs.Value
  612. if r == o.Value {
  613. return o, nil
  614. }
  615. return &Float{Value: r}, nil
  616. case token.Less:
  617. if o.Value < rhs.Value {
  618. return TrueValue, nil
  619. }
  620. return FalseValue, nil
  621. case token.Greater:
  622. if o.Value > rhs.Value {
  623. return TrueValue, nil
  624. }
  625. return FalseValue, nil
  626. case token.LessEq:
  627. if o.Value <= rhs.Value {
  628. return TrueValue, nil
  629. }
  630. return FalseValue, nil
  631. case token.GreaterEq:
  632. if o.Value >= rhs.Value {
  633. return TrueValue, nil
  634. }
  635. return FalseValue, nil
  636. }
  637. case *Int:
  638. switch op {
  639. case token.Add:
  640. r := o.Value + float64(rhs.Value)
  641. if r == o.Value {
  642. return o, nil
  643. }
  644. return &Float{Value: r}, nil
  645. case token.Sub:
  646. r := o.Value - float64(rhs.Value)
  647. if r == o.Value {
  648. return o, nil
  649. }
  650. return &Float{Value: r}, nil
  651. case token.Mul:
  652. r := o.Value * float64(rhs.Value)
  653. if r == o.Value {
  654. return o, nil
  655. }
  656. return &Float{Value: r}, nil
  657. case token.Quo:
  658. r := o.Value / float64(rhs.Value)
  659. if r == o.Value {
  660. return o, nil
  661. }
  662. return &Float{Value: r}, nil
  663. case token.Less:
  664. if o.Value < float64(rhs.Value) {
  665. return TrueValue, nil
  666. }
  667. return FalseValue, nil
  668. case token.Greater:
  669. if o.Value > float64(rhs.Value) {
  670. return TrueValue, nil
  671. }
  672. return FalseValue, nil
  673. case token.LessEq:
  674. if o.Value <= float64(rhs.Value) {
  675. return TrueValue, nil
  676. }
  677. return FalseValue, nil
  678. case token.GreaterEq:
  679. if o.Value >= float64(rhs.Value) {
  680. return TrueValue, nil
  681. }
  682. return FalseValue, nil
  683. }
  684. }
  685. return nil, ErrInvalidOperator
  686. }
  687. // Copy returns a copy of the type.
  688. func (o *Float) Copy() Object {
  689. return &Float{Value: o.Value}
  690. }
  691. // IsFalsy returns true if the value of the type is falsy.
  692. func (o *Float) IsFalsy() bool {
  693. return math.IsNaN(o.Value)
  694. }
  695. // Equals returns true if the value of the type is equal to the value of
  696. // another object.
  697. func (o *Float) Equals(x Object) bool {
  698. t, ok := x.(*Float)
  699. if !ok {
  700. return false
  701. }
  702. return o.Value == t.Value
  703. }
  704. // ImmutableArray represents an immutable array of objects.
  705. type ImmutableArray struct {
  706. ObjectImpl
  707. Value []Object
  708. }
  709. // TypeName returns the name of the type.
  710. func (o *ImmutableArray) TypeName() string {
  711. return "immutable-array"
  712. }
  713. func (o *ImmutableArray) String() string {
  714. var elements []string
  715. for _, e := range o.Value {
  716. elements = append(elements, e.String())
  717. }
  718. return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
  719. }
  720. // BinaryOp returns another object that is the result of a given binary
  721. // operator and a right-hand side object.
  722. func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error) {
  723. if rhs, ok := rhs.(*ImmutableArray); ok {
  724. switch op {
  725. case token.Add:
  726. return &Array{Value: append(o.Value, rhs.Value...)}, nil
  727. }
  728. }
  729. return nil, ErrInvalidOperator
  730. }
  731. // Copy returns a copy of the type.
  732. func (o *ImmutableArray) Copy() Object {
  733. var c []Object
  734. for _, elem := range o.Value {
  735. c = append(c, elem.Copy())
  736. }
  737. return &Array{Value: c}
  738. }
  739. // IsFalsy returns true if the value of the type is falsy.
  740. func (o *ImmutableArray) IsFalsy() bool {
  741. return len(o.Value) == 0
  742. }
  743. // Equals returns true if the value of the type is equal to the value of
  744. // another object.
  745. func (o *ImmutableArray) Equals(x Object) bool {
  746. var xVal []Object
  747. switch x := x.(type) {
  748. case *Array:
  749. xVal = x.Value
  750. case *ImmutableArray:
  751. xVal = x.Value
  752. default:
  753. return false
  754. }
  755. if len(o.Value) != len(xVal) {
  756. return false
  757. }
  758. for i, e := range o.Value {
  759. if !e.Equals(xVal[i]) {
  760. return false
  761. }
  762. }
  763. return true
  764. }
  765. // IndexGet returns an element at a given index.
  766. func (o *ImmutableArray) IndexGet(index Object) (res Object, err error) {
  767. intIdx, ok := index.(*Int)
  768. if !ok {
  769. err = ErrInvalidIndexType
  770. return
  771. }
  772. idxVal := int(intIdx.Value)
  773. if idxVal < 0 || idxVal >= len(o.Value) {
  774. res = UndefinedValue
  775. return
  776. }
  777. res = o.Value[idxVal]
  778. return
  779. }
  780. // Iterate creates an array iterator.
  781. func (o *ImmutableArray) Iterate() Iterator {
  782. return &ArrayIterator{
  783. v: o.Value,
  784. l: len(o.Value),
  785. }
  786. }
  787. // CanIterate returns whether the Object can be Iterated.
  788. func (o *ImmutableArray) CanIterate() bool {
  789. return true
  790. }
  791. // ImmutableMap represents an immutable map object.
  792. type ImmutableMap struct {
  793. ObjectImpl
  794. Value map[string]Object
  795. }
  796. // TypeName returns the name of the type.
  797. func (o *ImmutableMap) TypeName() string {
  798. return "immutable-map"
  799. }
  800. func (o *ImmutableMap) String() string {
  801. var pairs []string
  802. for k, v := range o.Value {
  803. pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
  804. }
  805. return fmt.Sprintf("{%s}", strings.Join(pairs, ", "))
  806. }
  807. // Copy returns a copy of the type.
  808. func (o *ImmutableMap) Copy() Object {
  809. c := make(map[string]Object)
  810. for k, v := range o.Value {
  811. c[k] = v.Copy()
  812. }
  813. return &Map{Value: c}
  814. }
  815. // IsFalsy returns true if the value of the type is falsy.
  816. func (o *ImmutableMap) IsFalsy() bool {
  817. return len(o.Value) == 0
  818. }
  819. // IndexGet returns the value for the given key.
  820. func (o *ImmutableMap) IndexGet(index Object) (res Object, err error) {
  821. strIdx, ok := ToString(index)
  822. if !ok {
  823. err = ErrInvalidIndexType
  824. return
  825. }
  826. res, ok = o.Value[strIdx]
  827. if !ok {
  828. res = UndefinedValue
  829. }
  830. return
  831. }
  832. // Equals returns true if the value of the type is equal to the value of
  833. // another object.
  834. func (o *ImmutableMap) Equals(x Object) bool {
  835. var xVal map[string]Object
  836. switch x := x.(type) {
  837. case *Map:
  838. xVal = x.Value
  839. case *ImmutableMap:
  840. xVal = x.Value
  841. default:
  842. return false
  843. }
  844. if len(o.Value) != len(xVal) {
  845. return false
  846. }
  847. for k, v := range o.Value {
  848. tv := xVal[k]
  849. if !v.Equals(tv) {
  850. return false
  851. }
  852. }
  853. return true
  854. }
  855. // Iterate creates an immutable map iterator.
  856. func (o *ImmutableMap) Iterate() Iterator {
  857. var keys []string
  858. for k := range o.Value {
  859. keys = append(keys, k)
  860. }
  861. return &MapIterator{
  862. v: o.Value,
  863. k: keys,
  864. l: len(keys),
  865. }
  866. }
  867. // CanIterate returns whether the Object can be Iterated.
  868. func (o *ImmutableMap) CanIterate() bool {
  869. return true
  870. }
  871. // Int represents an integer value.
  872. type Int struct {
  873. ObjectImpl
  874. Value int64
  875. }
  876. func (o *Int) String() string {
  877. return strconv.FormatInt(o.Value, 10)
  878. }
  879. // TypeName returns the name of the type.
  880. func (o *Int) TypeName() string {
  881. return "int"
  882. }
  883. // BinaryOp returns another object that is the result of a given binary
  884. // operator and a right-hand side object.
  885. func (o *Int) BinaryOp(op token.Token, rhs Object) (Object, error) {
  886. switch rhs := rhs.(type) {
  887. case *Int:
  888. switch op {
  889. case token.Add:
  890. r := o.Value + rhs.Value
  891. if r == o.Value {
  892. return o, nil
  893. }
  894. return &Int{Value: r}, nil
  895. case token.Sub:
  896. r := o.Value - rhs.Value
  897. if r == o.Value {
  898. return o, nil
  899. }
  900. return &Int{Value: r}, nil
  901. case token.Mul:
  902. r := o.Value * rhs.Value
  903. if r == o.Value {
  904. return o, nil
  905. }
  906. return &Int{Value: r}, nil
  907. case token.Quo:
  908. r := o.Value / rhs.Value
  909. if r == o.Value {
  910. return o, nil
  911. }
  912. return &Int{Value: r}, nil
  913. case token.Rem:
  914. r := o.Value % rhs.Value
  915. if r == o.Value {
  916. return o, nil
  917. }
  918. return &Int{Value: r}, nil
  919. case token.And:
  920. r := o.Value & rhs.Value
  921. if r == o.Value {
  922. return o, nil
  923. }
  924. return &Int{Value: r}, nil
  925. case token.Or:
  926. r := o.Value | rhs.Value
  927. if r == o.Value {
  928. return o, nil
  929. }
  930. return &Int{Value: r}, nil
  931. case token.Xor:
  932. r := o.Value ^ rhs.Value
  933. if r == o.Value {
  934. return o, nil
  935. }
  936. return &Int{Value: r}, nil
  937. case token.AndNot:
  938. r := o.Value &^ rhs.Value
  939. if r == o.Value {
  940. return o, nil
  941. }
  942. return &Int{Value: r}, nil
  943. case token.Shl:
  944. r := o.Value << uint64(rhs.Value)
  945. if r == o.Value {
  946. return o, nil
  947. }
  948. return &Int{Value: r}, nil
  949. case token.Shr:
  950. r := o.Value >> uint64(rhs.Value)
  951. if r == o.Value {
  952. return o, nil
  953. }
  954. return &Int{Value: r}, nil
  955. case token.Less:
  956. if o.Value < rhs.Value {
  957. return TrueValue, nil
  958. }
  959. return FalseValue, nil
  960. case token.Greater:
  961. if o.Value > rhs.Value {
  962. return TrueValue, nil
  963. }
  964. return FalseValue, nil
  965. case token.LessEq:
  966. if o.Value <= rhs.Value {
  967. return TrueValue, nil
  968. }
  969. return FalseValue, nil
  970. case token.GreaterEq:
  971. if o.Value >= rhs.Value {
  972. return TrueValue, nil
  973. }
  974. return FalseValue, nil
  975. }
  976. case *Float:
  977. switch op {
  978. case token.Add:
  979. return &Float{Value: float64(o.Value) + rhs.Value}, nil
  980. case token.Sub:
  981. return &Float{Value: float64(o.Value) - rhs.Value}, nil
  982. case token.Mul:
  983. return &Float{Value: float64(o.Value) * rhs.Value}, nil
  984. case token.Quo:
  985. return &Float{Value: float64(o.Value) / rhs.Value}, nil
  986. case token.Less:
  987. if float64(o.Value) < rhs.Value {
  988. return TrueValue, nil
  989. }
  990. return FalseValue, nil
  991. case token.Greater:
  992. if float64(o.Value) > rhs.Value {
  993. return TrueValue, nil
  994. }
  995. return FalseValue, nil
  996. case token.LessEq:
  997. if float64(o.Value) <= rhs.Value {
  998. return TrueValue, nil
  999. }
  1000. return FalseValue, nil
  1001. case token.GreaterEq:
  1002. if float64(o.Value) >= rhs.Value {
  1003. return TrueValue, nil
  1004. }
  1005. return FalseValue, nil
  1006. }
  1007. case *Char:
  1008. switch op {
  1009. case token.Add:
  1010. return &Char{Value: rune(o.Value) + rhs.Value}, nil
  1011. case token.Sub:
  1012. return &Char{Value: rune(o.Value) - rhs.Value}, nil
  1013. case token.Less:
  1014. if o.Value < int64(rhs.Value) {
  1015. return TrueValue, nil
  1016. }
  1017. return FalseValue, nil
  1018. case token.Greater:
  1019. if o.Value > int64(rhs.Value) {
  1020. return TrueValue, nil
  1021. }
  1022. return FalseValue, nil
  1023. case token.LessEq:
  1024. if o.Value <= int64(rhs.Value) {
  1025. return TrueValue, nil
  1026. }
  1027. return FalseValue, nil
  1028. case token.GreaterEq:
  1029. if o.Value >= int64(rhs.Value) {
  1030. return TrueValue, nil
  1031. }
  1032. return FalseValue, nil
  1033. }
  1034. }
  1035. return nil, ErrInvalidOperator
  1036. }
  1037. // Copy returns a copy of the type.
  1038. func (o *Int) Copy() Object {
  1039. return &Int{Value: o.Value}
  1040. }
  1041. // IsFalsy returns true if the value of the type is falsy.
  1042. func (o *Int) IsFalsy() bool {
  1043. return o.Value == 0
  1044. }
  1045. // Equals returns true if the value of the type is equal to the value of
  1046. // another object.
  1047. func (o *Int) Equals(x Object) bool {
  1048. t, ok := x.(*Int)
  1049. if !ok {
  1050. return false
  1051. }
  1052. return o.Value == t.Value
  1053. }
  1054. // Map represents a map of objects.
  1055. type Map struct {
  1056. ObjectImpl
  1057. Value map[string]Object
  1058. }
  1059. // TypeName returns the name of the type.
  1060. func (o *Map) TypeName() string {
  1061. return "map"
  1062. }
  1063. func (o *Map) String() string {
  1064. var pairs []string
  1065. for k, v := range o.Value {
  1066. pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String()))
  1067. }
  1068. return fmt.Sprintf("{%s}", strings.Join(pairs, ", "))
  1069. }
  1070. // Copy returns a copy of the type.
  1071. func (o *Map) Copy() Object {
  1072. c := make(map[string]Object)
  1073. for k, v := range o.Value {
  1074. c[k] = v.Copy()
  1075. }
  1076. return &Map{Value: c}
  1077. }
  1078. // IsFalsy returns true if the value of the type is falsy.
  1079. func (o *Map) IsFalsy() bool {
  1080. return len(o.Value) == 0
  1081. }
  1082. // Equals returns true if the value of the type is equal to the value of
  1083. // another object.
  1084. func (o *Map) Equals(x Object) bool {
  1085. var xVal map[string]Object
  1086. switch x := x.(type) {
  1087. case *Map:
  1088. xVal = x.Value
  1089. case *ImmutableMap:
  1090. xVal = x.Value
  1091. default:
  1092. return false
  1093. }
  1094. if len(o.Value) != len(xVal) {
  1095. return false
  1096. }
  1097. for k, v := range o.Value {
  1098. tv := xVal[k]
  1099. if !v.Equals(tv) {
  1100. return false
  1101. }
  1102. }
  1103. return true
  1104. }
  1105. // IndexGet returns the value for the given key.
  1106. func (o *Map) IndexGet(index Object) (res Object, err error) {
  1107. strIdx, ok := ToString(index)
  1108. if !ok {
  1109. err = ErrInvalidIndexType
  1110. return
  1111. }
  1112. res, ok = o.Value[strIdx]
  1113. if !ok {
  1114. res = UndefinedValue
  1115. }
  1116. return
  1117. }
  1118. // IndexSet sets the value for the given key.
  1119. func (o *Map) IndexSet(index, value Object) (err error) {
  1120. strIdx, ok := ToString(index)
  1121. if !ok {
  1122. err = ErrInvalidIndexType
  1123. return
  1124. }
  1125. o.Value[strIdx] = value
  1126. return nil
  1127. }
  1128. // Iterate creates a map iterator.
  1129. func (o *Map) Iterate() Iterator {
  1130. var keys []string
  1131. for k := range o.Value {
  1132. keys = append(keys, k)
  1133. }
  1134. return &MapIterator{
  1135. v: o.Value,
  1136. k: keys,
  1137. l: len(keys),
  1138. }
  1139. }
  1140. // CanIterate returns whether the Object can be Iterated.
  1141. func (o *Map) CanIterate() bool {
  1142. return true
  1143. }
  1144. // ObjectPtr represents a free variable.
  1145. type ObjectPtr struct {
  1146. ObjectImpl
  1147. Value *Object
  1148. }
  1149. func (o *ObjectPtr) String() string {
  1150. return "free-var"
  1151. }
  1152. // TypeName returns the name of the type.
  1153. func (o *ObjectPtr) TypeName() string {
  1154. return "<free-var>"
  1155. }
  1156. // Copy returns a copy of the type.
  1157. func (o *ObjectPtr) Copy() Object {
  1158. return o
  1159. }
  1160. // IsFalsy returns true if the value of the type is falsy.
  1161. func (o *ObjectPtr) IsFalsy() bool {
  1162. return o.Value == nil
  1163. }
  1164. // Equals returns true if the value of the type is equal to the value of
  1165. // another object.
  1166. func (o *ObjectPtr) Equals(x Object) bool {
  1167. return o == x
  1168. }
  1169. // String represents a string value.
  1170. type String struct {
  1171. ObjectImpl
  1172. Value string
  1173. runeStr []rune
  1174. }
  1175. // TypeName returns the name of the type.
  1176. func (o *String) TypeName() string {
  1177. return "string"
  1178. }
  1179. func (o *String) String() string {
  1180. return strconv.Quote(o.Value)
  1181. }
  1182. // BinaryOp returns another object that is the result of a given binary
  1183. // operator and a right-hand side object.
  1184. func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) {
  1185. switch op {
  1186. case token.Add:
  1187. switch rhs := rhs.(type) {
  1188. case *String:
  1189. if len(o.Value)+len(rhs.Value) > MaxStringLen {
  1190. return nil, ErrStringLimit
  1191. }
  1192. return &String{Value: o.Value + rhs.Value}, nil
  1193. default:
  1194. rhsStr := rhs.String()
  1195. if len(o.Value)+len(rhsStr) > MaxStringLen {
  1196. return nil, ErrStringLimit
  1197. }
  1198. return &String{Value: o.Value + rhsStr}, nil
  1199. }
  1200. case token.Less:
  1201. switch rhs := rhs.(type) {
  1202. case *String:
  1203. if o.Value < rhs.Value {
  1204. return TrueValue, nil
  1205. }
  1206. return FalseValue, nil
  1207. }
  1208. case token.LessEq:
  1209. switch rhs := rhs.(type) {
  1210. case *String:
  1211. if o.Value <= rhs.Value {
  1212. return TrueValue, nil
  1213. }
  1214. return FalseValue, nil
  1215. }
  1216. case token.Greater:
  1217. switch rhs := rhs.(type) {
  1218. case *String:
  1219. if o.Value > rhs.Value {
  1220. return TrueValue, nil
  1221. }
  1222. return FalseValue, nil
  1223. }
  1224. case token.GreaterEq:
  1225. switch rhs := rhs.(type) {
  1226. case *String:
  1227. if o.Value >= rhs.Value {
  1228. return TrueValue, nil
  1229. }
  1230. return FalseValue, nil
  1231. }
  1232. }
  1233. return nil, ErrInvalidOperator
  1234. }
  1235. // IsFalsy returns true if the value of the type is falsy.
  1236. func (o *String) IsFalsy() bool {
  1237. return len(o.Value) == 0
  1238. }
  1239. // Copy returns a copy of the type.
  1240. func (o *String) Copy() Object {
  1241. return &String{Value: o.Value}
  1242. }
  1243. // Equals returns true if the value of the type is equal to the value of
  1244. // another object.
  1245. func (o *String) Equals(x Object) bool {
  1246. t, ok := x.(*String)
  1247. if !ok {
  1248. return false
  1249. }
  1250. return o.Value == t.Value
  1251. }
  1252. // IndexGet returns a character at a given index.
  1253. func (o *String) IndexGet(index Object) (res Object, err error) {
  1254. intIdx, ok := index.(*Int)
  1255. if !ok {
  1256. err = ErrInvalidIndexType
  1257. return
  1258. }
  1259. idxVal := int(intIdx.Value)
  1260. if o.runeStr == nil {
  1261. o.runeStr = []rune(o.Value)
  1262. }
  1263. if idxVal < 0 || idxVal >= len(o.runeStr) {
  1264. res = UndefinedValue
  1265. return
  1266. }
  1267. res = &Char{Value: o.runeStr[idxVal]}
  1268. return
  1269. }
  1270. // Iterate creates a string iterator.
  1271. func (o *String) Iterate() Iterator {
  1272. if o.runeStr == nil {
  1273. o.runeStr = []rune(o.Value)
  1274. }
  1275. return &StringIterator{
  1276. v: o.runeStr,
  1277. l: len(o.runeStr),
  1278. }
  1279. }
  1280. // CanIterate returns whether the Object can be Iterated.
  1281. func (o *String) CanIterate() bool {
  1282. return true
  1283. }
  1284. // Time represents a time value.
  1285. type Time struct {
  1286. ObjectImpl
  1287. Value time.Time
  1288. }
  1289. func (o *Time) String() string {
  1290. return o.Value.String()
  1291. }
  1292. // TypeName returns the name of the type.
  1293. func (o *Time) TypeName() string {
  1294. return "time"
  1295. }
  1296. // BinaryOp returns another object that is the result of a given binary
  1297. // operator and a right-hand side object.
  1298. func (o *Time) BinaryOp(op token.Token, rhs Object) (Object, error) {
  1299. switch rhs := rhs.(type) {
  1300. case *Int:
  1301. switch op {
  1302. case token.Add: // time + int => time
  1303. if rhs.Value == 0 {
  1304. return o, nil
  1305. }
  1306. return &Time{Value: o.Value.Add(time.Duration(rhs.Value))}, nil
  1307. case token.Sub: // time - int => time
  1308. if rhs.Value == 0 {
  1309. return o, nil
  1310. }
  1311. return &Time{Value: o.Value.Add(time.Duration(-rhs.Value))}, nil
  1312. }
  1313. case *Time:
  1314. switch op {
  1315. case token.Sub: // time - time => int (duration)
  1316. return &Int{Value: int64(o.Value.Sub(rhs.Value))}, nil
  1317. case token.Less: // time < time => bool
  1318. if o.Value.Before(rhs.Value) {
  1319. return TrueValue, nil
  1320. }
  1321. return FalseValue, nil
  1322. case token.Greater:
  1323. if o.Value.After(rhs.Value) {
  1324. return TrueValue, nil
  1325. }
  1326. return FalseValue, nil
  1327. case token.LessEq:
  1328. if o.Value.Equal(rhs.Value) || o.Value.Before(rhs.Value) {
  1329. return TrueValue, nil
  1330. }
  1331. return FalseValue, nil
  1332. case token.GreaterEq:
  1333. if o.Value.Equal(rhs.Value) || o.Value.After(rhs.Value) {
  1334. return TrueValue, nil
  1335. }
  1336. return FalseValue, nil
  1337. }
  1338. }
  1339. return nil, ErrInvalidOperator
  1340. }
  1341. // Copy returns a copy of the type.
  1342. func (o *Time) Copy() Object {
  1343. return &Time{Value: o.Value}
  1344. }
  1345. // IsFalsy returns true if the value of the type is falsy.
  1346. func (o *Time) IsFalsy() bool {
  1347. return o.Value.IsZero()
  1348. }
  1349. // Equals returns true if the value of the type is equal to the value of
  1350. // another object.
  1351. func (o *Time) Equals(x Object) bool {
  1352. t, ok := x.(*Time)
  1353. if !ok {
  1354. return false
  1355. }
  1356. return o.Value.Equal(t.Value)
  1357. }
  1358. // Undefined represents an undefined value.
  1359. type Undefined struct {
  1360. ObjectImpl
  1361. }
  1362. // TypeName returns the name of the type.
  1363. func (o *Undefined) TypeName() string {
  1364. return "undefined"
  1365. }
  1366. func (o *Undefined) String() string {
  1367. return "<undefined>"
  1368. }
  1369. // Copy returns a copy of the type.
  1370. func (o *Undefined) Copy() Object {
  1371. return o
  1372. }
  1373. // IsFalsy returns true if the value of the type is falsy.
  1374. func (o *Undefined) IsFalsy() bool {
  1375. return true
  1376. }
  1377. // Equals returns true if the value of the type is equal to the value of
  1378. // another object.
  1379. func (o *Undefined) Equals(x Object) bool {
  1380. return o == x
  1381. }
  1382. // IndexGet returns an element at a given index.
  1383. func (o *Undefined) IndexGet(_ Object) (Object, error) {
  1384. return UndefinedValue, nil
  1385. }
  1386. // Iterate creates a map iterator.
  1387. func (o *Undefined) Iterate() Iterator {
  1388. return o
  1389. }
  1390. // CanIterate returns whether the Object can be Iterated.
  1391. func (o *Undefined) CanIterate() bool {
  1392. return true
  1393. }
  1394. // Next returns true if there are more elements to iterate.
  1395. func (o *Undefined) Next() bool {
  1396. return false
  1397. }
  1398. // Key returns the key or index value of the current element.
  1399. func (o *Undefined) Key() Object {
  1400. return o
  1401. }
  1402. // Value returns the value of the current element.
  1403. func (o *Undefined) Value() Object {
  1404. return o
  1405. }
  1406. // UserFunction represents a user function.
  1407. type UserFunction struct {
  1408. ObjectImpl
  1409. Name string
  1410. Value CallableFunc
  1411. }
  1412. // TypeName returns the name of the type.
  1413. func (o *UserFunction) TypeName() string {
  1414. return "user-function:" + o.Name
  1415. }
  1416. func (o *UserFunction) String() string {
  1417. return "<user-function>"
  1418. }
  1419. // Copy returns a copy of the type.
  1420. func (o *UserFunction) Copy() Object {
  1421. return &UserFunction{Value: o.Value, Name: o.Name}
  1422. }
  1423. // Equals returns true if the value of the type is equal to the value of
  1424. // another object.
  1425. func (o *UserFunction) Equals(_ Object) bool {
  1426. return false
  1427. }
  1428. // Call invokes a user function.
  1429. func (o *UserFunction) Call(args ...Object) (Object, error) {
  1430. return o.Value(args...)
  1431. }
  1432. // CanCall returns whether the Object can be Called.
  1433. func (o *UserFunction) CanCall() bool {
  1434. return true
  1435. }