value.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. package goja
  2. import (
  3. "fmt"
  4. "hash/maphash"
  5. "math"
  6. "reflect"
  7. "regexp"
  8. "strconv"
  9. "unsafe"
  10. )
  11. var (
  12. valueFalse Value = valueBool(false)
  13. valueTrue Value = valueBool(true)
  14. _null Value = valueNull{}
  15. _NaN Value = valueFloat(math.NaN())
  16. _positiveInf Value = valueFloat(math.Inf(+1))
  17. _negativeInf Value = valueFloat(math.Inf(-1))
  18. _positiveZero Value = valueInt(0)
  19. _negativeZero Value = valueFloat(math.Float64frombits(0 | (1 << 63)))
  20. _epsilon = valueFloat(2.2204460492503130808472633361816e-16)
  21. _undefined Value = valueUndefined{}
  22. )
  23. var (
  24. reflectTypeInt = reflect.TypeOf(int64(0))
  25. reflectTypeBool = reflect.TypeOf(false)
  26. reflectTypeNil = reflect.TypeOf(nil)
  27. reflectTypeFloat = reflect.TypeOf(float64(0))
  28. reflectTypeMap = reflect.TypeOf(map[string]interface{}{})
  29. reflectTypeArray = reflect.TypeOf([]interface{}{})
  30. reflectTypeString = reflect.TypeOf("")
  31. )
  32. var (
  33. mapHasher maphash.Hash
  34. )
  35. var intCache [256]Value
  36. type Value interface {
  37. ToInteger() int64
  38. toString() valueString
  39. ToPrimitiveString() Value
  40. String() string
  41. ToFloat() float64
  42. ToNumber() Value
  43. ToBoolean() bool
  44. ToObject(*Runtime) *Object
  45. SameAs(Value) bool
  46. Equals(Value) bool
  47. StrictEquals(Value) bool
  48. Export() interface{}
  49. ExportType() reflect.Type
  50. baseObject(r *Runtime) *Object
  51. hash() uint64
  52. }
  53. type typeError string
  54. type valueInt int64
  55. type valueFloat float64
  56. type valueBool bool
  57. type valueNull struct{}
  58. type valueUndefined struct {
  59. valueNull
  60. }
  61. type valueSymbol struct {
  62. desc string
  63. }
  64. type valueUnresolved struct {
  65. r *Runtime
  66. ref string
  67. }
  68. type memberUnresolved struct {
  69. valueUnresolved
  70. }
  71. type valueProperty struct {
  72. value Value
  73. writable bool
  74. configurable bool
  75. enumerable bool
  76. accessor bool
  77. getterFunc *Object
  78. setterFunc *Object
  79. }
  80. func propGetter(o Value, v Value, r *Runtime) *Object {
  81. if v == _undefined {
  82. return nil
  83. }
  84. if obj, ok := v.(*Object); ok {
  85. if _, ok := obj.self.assertCallable(); ok {
  86. return obj
  87. }
  88. }
  89. r.typeErrorResult(true, "Getter must be a function: %s", v.toString())
  90. return nil
  91. }
  92. func propSetter(o Value, v Value, r *Runtime) *Object {
  93. if v == _undefined {
  94. return nil
  95. }
  96. if obj, ok := v.(*Object); ok {
  97. if _, ok := obj.self.assertCallable(); ok {
  98. return obj
  99. }
  100. }
  101. r.typeErrorResult(true, "Setter must be a function: %s", v.toString())
  102. return nil
  103. }
  104. func (i valueInt) ToInteger() int64 {
  105. return int64(i)
  106. }
  107. func (i valueInt) toString() valueString {
  108. return asciiString(i.String())
  109. }
  110. func (i valueInt) ToPrimitiveString() Value {
  111. return i
  112. }
  113. func (i valueInt) String() string {
  114. return strconv.FormatInt(int64(i), 10)
  115. }
  116. func (i valueInt) ToFloat() float64 {
  117. return float64(int64(i))
  118. }
  119. func (i valueInt) ToBoolean() bool {
  120. return i != 0
  121. }
  122. func (i valueInt) ToObject(r *Runtime) *Object {
  123. return r.newPrimitiveObject(i, r.global.NumberPrototype, classNumber)
  124. }
  125. func (i valueInt) ToNumber() Value {
  126. return i
  127. }
  128. func (i valueInt) SameAs(other Value) bool {
  129. return i == other
  130. }
  131. func (i valueInt) Equals(other Value) bool {
  132. switch o := other.(type) {
  133. case valueInt:
  134. return i == o
  135. case valueFloat:
  136. return float64(i) == float64(o)
  137. case valueString:
  138. return o.ToNumber().Equals(i)
  139. case valueBool:
  140. return int64(i) == o.ToInteger()
  141. case *Object:
  142. return i.Equals(o.self.toPrimitiveNumber())
  143. }
  144. return false
  145. }
  146. func (i valueInt) StrictEquals(other Value) bool {
  147. switch o := other.(type) {
  148. case valueInt:
  149. return i == o
  150. case valueFloat:
  151. return float64(i) == float64(o)
  152. }
  153. return false
  154. }
  155. func (i valueInt) baseObject(r *Runtime) *Object {
  156. return r.global.NumberPrototype
  157. }
  158. func (i valueInt) Export() interface{} {
  159. return int64(i)
  160. }
  161. func (i valueInt) ExportType() reflect.Type {
  162. return reflectTypeInt
  163. }
  164. func (i valueInt) hash() uint64 {
  165. return uint64(i)
  166. }
  167. func (o valueBool) ToInteger() int64 {
  168. if o {
  169. return 1
  170. }
  171. return 0
  172. }
  173. func (o valueBool) toString() valueString {
  174. if o {
  175. return stringTrue
  176. }
  177. return stringFalse
  178. }
  179. func (o valueBool) ToPrimitiveString() Value {
  180. return o
  181. }
  182. func (o valueBool) String() string {
  183. if o {
  184. return "true"
  185. }
  186. return "false"
  187. }
  188. func (o valueBool) ToFloat() float64 {
  189. if o {
  190. return 1.0
  191. }
  192. return 0
  193. }
  194. func (o valueBool) ToBoolean() bool {
  195. return bool(o)
  196. }
  197. func (o valueBool) ToObject(r *Runtime) *Object {
  198. return r.newPrimitiveObject(o, r.global.BooleanPrototype, "Boolean")
  199. }
  200. func (o valueBool) ToNumber() Value {
  201. if o {
  202. return valueInt(1)
  203. }
  204. return valueInt(0)
  205. }
  206. func (o valueBool) SameAs(other Value) bool {
  207. if other, ok := other.(valueBool); ok {
  208. return o == other
  209. }
  210. return false
  211. }
  212. func (b valueBool) Equals(other Value) bool {
  213. if o, ok := other.(valueBool); ok {
  214. return b == o
  215. }
  216. if b {
  217. return other.Equals(intToValue(1))
  218. } else {
  219. return other.Equals(intToValue(0))
  220. }
  221. }
  222. func (o valueBool) StrictEquals(other Value) bool {
  223. if other, ok := other.(valueBool); ok {
  224. return o == other
  225. }
  226. return false
  227. }
  228. func (o valueBool) baseObject(r *Runtime) *Object {
  229. return r.global.BooleanPrototype
  230. }
  231. func (o valueBool) Export() interface{} {
  232. return bool(o)
  233. }
  234. func (o valueBool) ExportType() reflect.Type {
  235. return reflectTypeBool
  236. }
  237. func (b valueBool) hash() uint64 {
  238. if b {
  239. return uint64(uintptr(unsafe.Pointer(&valueTrue)))
  240. }
  241. return uint64(uintptr(unsafe.Pointer(&valueFalse)))
  242. }
  243. func (n valueNull) ToInteger() int64 {
  244. return 0
  245. }
  246. func (n valueNull) toString() valueString {
  247. return stringNull
  248. }
  249. func (n valueNull) ToPrimitiveString() Value {
  250. return n
  251. }
  252. func (n valueNull) String() string {
  253. return "null"
  254. }
  255. func (u valueUndefined) toString() valueString {
  256. return stringUndefined
  257. }
  258. func (u valueUndefined) ToPrimitiveString() Value {
  259. return u
  260. }
  261. func (u valueUndefined) String() string {
  262. return "undefined"
  263. }
  264. func (u valueUndefined) ToNumber() Value {
  265. return _NaN
  266. }
  267. func (u valueUndefined) SameAs(other Value) bool {
  268. _, same := other.(valueUndefined)
  269. return same
  270. }
  271. func (u valueUndefined) StrictEquals(other Value) bool {
  272. _, same := other.(valueUndefined)
  273. return same
  274. }
  275. func (u valueUndefined) ToFloat() float64 {
  276. return math.NaN()
  277. }
  278. func (u valueUndefined) hash() uint64 {
  279. return uint64(uintptr(unsafe.Pointer(&_undefined)))
  280. }
  281. func (n valueNull) ToFloat() float64 {
  282. return 0
  283. }
  284. func (n valueNull) ToBoolean() bool {
  285. return false
  286. }
  287. func (n valueNull) ToObject(r *Runtime) *Object {
  288. r.typeErrorResult(true, "Cannot convert undefined or null to object")
  289. return nil
  290. //return r.newObject()
  291. }
  292. func (n valueNull) ToNumber() Value {
  293. return intToValue(0)
  294. }
  295. func (n valueNull) SameAs(other Value) bool {
  296. _, same := other.(valueNull)
  297. return same
  298. }
  299. func (n valueNull) Equals(other Value) bool {
  300. switch other.(type) {
  301. case valueUndefined, valueNull:
  302. return true
  303. }
  304. return false
  305. }
  306. func (n valueNull) StrictEquals(other Value) bool {
  307. _, same := other.(valueNull)
  308. return same
  309. }
  310. func (n valueNull) baseObject(*Runtime) *Object {
  311. return nil
  312. }
  313. func (n valueNull) Export() interface{} {
  314. return nil
  315. }
  316. func (n valueNull) ExportType() reflect.Type {
  317. return reflectTypeNil
  318. }
  319. func (n valueNull) hash() uint64 {
  320. return uint64(uintptr(unsafe.Pointer(&_null)))
  321. }
  322. func (p *valueProperty) ToInteger() int64 {
  323. return 0
  324. }
  325. func (p *valueProperty) toString() valueString {
  326. return stringEmpty
  327. }
  328. func (p *valueProperty) ToPrimitiveString() Value {
  329. return _undefined
  330. }
  331. func (p *valueProperty) String() string {
  332. return ""
  333. }
  334. func (p *valueProperty) ToFloat() float64 {
  335. return math.NaN()
  336. }
  337. func (p *valueProperty) ToBoolean() bool {
  338. return false
  339. }
  340. func (p *valueProperty) ToObject(*Runtime) *Object {
  341. return nil
  342. }
  343. func (p *valueProperty) ToNumber() Value {
  344. return nil
  345. }
  346. func (p *valueProperty) isWritable() bool {
  347. return p.writable || p.setterFunc != nil
  348. }
  349. func (p *valueProperty) get(this Value) Value {
  350. if p.getterFunc == nil {
  351. if p.value != nil {
  352. return p.value
  353. }
  354. return _undefined
  355. }
  356. call, _ := p.getterFunc.self.assertCallable()
  357. return call(FunctionCall{
  358. This: this,
  359. })
  360. }
  361. func (p *valueProperty) set(this, v Value) {
  362. if p.setterFunc == nil {
  363. p.value = v
  364. return
  365. }
  366. call, _ := p.setterFunc.self.assertCallable()
  367. call(FunctionCall{
  368. This: this,
  369. Arguments: []Value{v},
  370. })
  371. }
  372. func (p *valueProperty) SameAs(other Value) bool {
  373. if otherProp, ok := other.(*valueProperty); ok {
  374. return p == otherProp
  375. }
  376. return false
  377. }
  378. func (p *valueProperty) Equals(Value) bool {
  379. return false
  380. }
  381. func (p *valueProperty) StrictEquals(Value) bool {
  382. return false
  383. }
  384. func (n *valueProperty) baseObject(r *Runtime) *Object {
  385. r.typeErrorResult(true, "BUG: baseObject() is called on valueProperty") // TODO error message
  386. return nil
  387. }
  388. func (n *valueProperty) Export() interface{} {
  389. panic("Cannot export valueProperty")
  390. }
  391. func (n *valueProperty) ExportType() reflect.Type {
  392. panic("Cannot export valueProperty")
  393. }
  394. func (n *valueProperty) hash() uint64 {
  395. panic("valueProperty should never be used in maps or sets")
  396. }
  397. func (f valueFloat) ToInteger() int64 {
  398. switch {
  399. case math.IsNaN(float64(f)):
  400. return 0
  401. case math.IsInf(float64(f), 1):
  402. return int64(math.MaxInt64)
  403. case math.IsInf(float64(f), -1):
  404. return int64(math.MinInt64)
  405. }
  406. return int64(f)
  407. }
  408. func (f valueFloat) toString() valueString {
  409. return asciiString(f.String())
  410. }
  411. func (f valueFloat) ToPrimitiveString() Value {
  412. return f
  413. }
  414. var matchLeading0Exponent = regexp.MustCompile(`([eE][+\-])0+([1-9])`) // 1e-07 => 1e-7
  415. func (f valueFloat) String() string {
  416. value := float64(f)
  417. if math.IsNaN(value) {
  418. return "NaN"
  419. } else if math.IsInf(value, 0) {
  420. if math.Signbit(value) {
  421. return "-Infinity"
  422. }
  423. return "Infinity"
  424. } else if f == _negativeZero {
  425. return "0"
  426. }
  427. exponent := math.Log10(math.Abs(value))
  428. if exponent >= 21 || exponent < -6 {
  429. return matchLeading0Exponent.ReplaceAllString(strconv.FormatFloat(value, 'g', -1, 64), "$1$2")
  430. }
  431. return strconv.FormatFloat(value, 'f', -1, 64)
  432. }
  433. func (f valueFloat) ToFloat() float64 {
  434. return float64(f)
  435. }
  436. func (f valueFloat) ToBoolean() bool {
  437. return float64(f) != 0.0 && !math.IsNaN(float64(f))
  438. }
  439. func (f valueFloat) ToObject(r *Runtime) *Object {
  440. return r.newPrimitiveObject(f, r.global.NumberPrototype, "Number")
  441. }
  442. func (f valueFloat) ToNumber() Value {
  443. return f
  444. }
  445. func (f valueFloat) SameAs(other Value) bool {
  446. switch o := other.(type) {
  447. case valueFloat:
  448. this := float64(f)
  449. o1 := float64(o)
  450. if math.IsNaN(this) && math.IsNaN(o1) {
  451. return true
  452. } else {
  453. ret := this == o1
  454. if ret && this == 0 {
  455. ret = math.Signbit(this) == math.Signbit(o1)
  456. }
  457. return ret
  458. }
  459. case valueInt:
  460. this := float64(f)
  461. ret := this == float64(o)
  462. if ret && this == 0 {
  463. ret = !math.Signbit(this)
  464. }
  465. return ret
  466. }
  467. return false
  468. }
  469. func (f valueFloat) Equals(other Value) bool {
  470. switch o := other.(type) {
  471. case valueFloat:
  472. return f == o
  473. case valueInt:
  474. return float64(f) == float64(o)
  475. case valueString, valueBool:
  476. return float64(f) == o.ToFloat()
  477. case *Object:
  478. return f.Equals(o.self.toPrimitiveNumber())
  479. }
  480. return false
  481. }
  482. func (f valueFloat) StrictEquals(other Value) bool {
  483. switch o := other.(type) {
  484. case valueFloat:
  485. return f == o
  486. case valueInt:
  487. return float64(f) == float64(o)
  488. }
  489. return false
  490. }
  491. func (f valueFloat) baseObject(r *Runtime) *Object {
  492. return r.global.NumberPrototype
  493. }
  494. func (f valueFloat) Export() interface{} {
  495. return float64(f)
  496. }
  497. func (f valueFloat) ExportType() reflect.Type {
  498. return reflectTypeFloat
  499. }
  500. func (f valueFloat) hash() uint64 {
  501. if f == _negativeZero {
  502. return 0
  503. }
  504. return math.Float64bits(float64(f))
  505. }
  506. func (o *Object) ToInteger() int64 {
  507. return o.self.toPrimitiveNumber().ToNumber().ToInteger()
  508. }
  509. func (o *Object) toString() valueString {
  510. return o.self.toPrimitiveString().toString()
  511. }
  512. func (o *Object) ToPrimitiveString() Value {
  513. return o.self.toPrimitiveString().ToPrimitiveString()
  514. }
  515. func (o *Object) String() string {
  516. return o.self.toPrimitiveString().String()
  517. }
  518. func (o *Object) ToFloat() float64 {
  519. return o.self.toPrimitiveNumber().ToFloat()
  520. }
  521. func (o *Object) ToBoolean() bool {
  522. return true
  523. }
  524. func (o *Object) ToObject(*Runtime) *Object {
  525. return o
  526. }
  527. func (o *Object) ToNumber() Value {
  528. return o.self.toPrimitiveNumber().ToNumber()
  529. }
  530. func (o *Object) SameAs(other Value) bool {
  531. if other, ok := other.(*Object); ok {
  532. return o == other
  533. }
  534. return false
  535. }
  536. func (o *Object) Equals(other Value) bool {
  537. if other, ok := other.(*Object); ok {
  538. return o == other || o.self.equal(other.self)
  539. }
  540. switch o1 := other.(type) {
  541. case valueInt, valueFloat, valueString:
  542. return o.self.toPrimitive().Equals(other)
  543. case valueBool:
  544. return o.Equals(o1.ToNumber())
  545. }
  546. return false
  547. }
  548. func (o *Object) StrictEquals(other Value) bool {
  549. if other, ok := other.(*Object); ok {
  550. return o == other || o.self.equal(other.self)
  551. }
  552. return false
  553. }
  554. func (o *Object) baseObject(*Runtime) *Object {
  555. return o
  556. }
  557. func (o *Object) Export() interface{} {
  558. return o.self.export()
  559. }
  560. func (o *Object) ExportType() reflect.Type {
  561. return o.self.exportType()
  562. }
  563. func (o *Object) hash() uint64 {
  564. return uint64(uintptr(unsafe.Pointer(o)))
  565. }
  566. func (o *Object) Get(name string) Value {
  567. return o.self.getStr(name, nil)
  568. }
  569. func (o *Object) Keys() (keys []string) {
  570. names := o.self.ownKeys(false, nil)
  571. keys = make([]string, 0, len(names))
  572. for _, name := range names {
  573. keys = append(keys, name.String())
  574. }
  575. return
  576. }
  577. // DefineDataProperty is a Go equivalent of Object.defineProperty(o, name, {value: value, writable: writable,
  578. // configurable: configurable, enumerable: enumerable})
  579. func (o *Object) DefineDataProperty(name string, value Value, writable, configurable, enumerable Flag) error {
  580. return tryFunc(func() {
  581. o.self.defineOwnPropertyStr(name, PropertyDescriptor{
  582. Value: value,
  583. Writable: writable,
  584. Configurable: configurable,
  585. Enumerable: enumerable,
  586. }, true)
  587. })
  588. }
  589. // DefineAccessorProperty is a Go equivalent of Object.defineProperty(o, name, {get: getter, set: setter,
  590. // configurable: configurable, enumerable: enumerable})
  591. func (o *Object) DefineAccessorProperty(name string, getter, setter Value, configurable, enumerable Flag) error {
  592. return tryFunc(func() {
  593. o.self.defineOwnPropertyStr(name, PropertyDescriptor{
  594. Getter: getter,
  595. Setter: setter,
  596. Configurable: configurable,
  597. Enumerable: enumerable,
  598. }, true)
  599. })
  600. }
  601. func (o *Object) Set(name string, value interface{}) error {
  602. return tryFunc(func() {
  603. o.self.setOwnStr(name, o.runtime.ToValue(value), true)
  604. })
  605. }
  606. // MarshalJSON returns JSON representation of the Object. It is equivalent to JSON.stringify(o).
  607. // Note, this implements json.Marshaler so that json.Marshal() can be used without the need to Export().
  608. func (o *Object) MarshalJSON() ([]byte, error) {
  609. ctx := _builtinJSON_stringifyContext{
  610. r: o.runtime,
  611. }
  612. ex := o.runtime.vm.try(func() {
  613. if !ctx.do(o) {
  614. ctx.buf.WriteString("null")
  615. }
  616. })
  617. if ex != nil {
  618. return nil, ex
  619. }
  620. return ctx.buf.Bytes(), nil
  621. }
  622. // ClassName returns the class name
  623. func (o *Object) ClassName() string {
  624. return o.self.className()
  625. }
  626. func (o valueUnresolved) throw() {
  627. o.r.throwReferenceError(o.ref)
  628. }
  629. func (o valueUnresolved) ToInteger() int64 {
  630. o.throw()
  631. return 0
  632. }
  633. func (o valueUnresolved) toString() valueString {
  634. o.throw()
  635. return nil
  636. }
  637. func (o valueUnresolved) ToPrimitiveString() Value {
  638. o.throw()
  639. return nil
  640. }
  641. func (o valueUnresolved) String() string {
  642. o.throw()
  643. return ""
  644. }
  645. func (o valueUnresolved) ToFloat() float64 {
  646. o.throw()
  647. return 0
  648. }
  649. func (o valueUnresolved) ToBoolean() bool {
  650. o.throw()
  651. return false
  652. }
  653. func (o valueUnresolved) ToObject(*Runtime) *Object {
  654. o.throw()
  655. return nil
  656. }
  657. func (o valueUnresolved) ToNumber() Value {
  658. o.throw()
  659. return nil
  660. }
  661. func (o valueUnresolved) SameAs(Value) bool {
  662. o.throw()
  663. return false
  664. }
  665. func (o valueUnresolved) Equals(Value) bool {
  666. o.throw()
  667. return false
  668. }
  669. func (o valueUnresolved) StrictEquals(Value) bool {
  670. o.throw()
  671. return false
  672. }
  673. func (o valueUnresolved) baseObject(*Runtime) *Object {
  674. o.throw()
  675. return nil
  676. }
  677. func (o valueUnresolved) Export() interface{} {
  678. o.throw()
  679. return nil
  680. }
  681. func (o valueUnresolved) ExportType() reflect.Type {
  682. o.throw()
  683. return nil
  684. }
  685. func (o valueUnresolved) hash() uint64 {
  686. o.throw()
  687. return 0
  688. }
  689. func (s *valueSymbol) ToInteger() int64 {
  690. panic(typeError("Cannot convert a Symbol value to a number"))
  691. }
  692. func (s *valueSymbol) toString() valueString {
  693. panic(typeError("Cannot convert a Symbol value to a string"))
  694. }
  695. func (s *valueSymbol) ToPrimitiveString() Value {
  696. return s
  697. }
  698. func (s *valueSymbol) String() string {
  699. return s.descString()
  700. }
  701. func (s *valueSymbol) ToFloat() float64 {
  702. panic(typeError("Cannot convert a Symbol value to a number"))
  703. }
  704. func (s *valueSymbol) ToNumber() Value {
  705. panic(typeError("Cannot convert a Symbol value to a number"))
  706. }
  707. func (s *valueSymbol) ToBoolean() bool {
  708. return true
  709. }
  710. func (s *valueSymbol) ToObject(r *Runtime) *Object {
  711. return s.baseObject(r)
  712. }
  713. func (s *valueSymbol) SameAs(other Value) bool {
  714. if s1, ok := other.(*valueSymbol); ok {
  715. return s == s1
  716. }
  717. return false
  718. }
  719. func (s *valueSymbol) Equals(o Value) bool {
  720. return s.SameAs(o)
  721. }
  722. func (s *valueSymbol) StrictEquals(o Value) bool {
  723. return s.SameAs(o)
  724. }
  725. func (s *valueSymbol) Export() interface{} {
  726. return s.String()
  727. }
  728. func (s *valueSymbol) ExportType() reflect.Type {
  729. return reflectTypeString
  730. }
  731. func (s *valueSymbol) baseObject(r *Runtime) *Object {
  732. return r.newPrimitiveObject(s, r.global.SymbolPrototype, "Symbol")
  733. }
  734. func (s *valueSymbol) hash() uint64 {
  735. return uint64(uintptr(unsafe.Pointer(s)))
  736. }
  737. func (s *valueSymbol) descString() string {
  738. return fmt.Sprintf("Symbol(%s)", s.desc)
  739. }
  740. func init() {
  741. for i := 0; i < 256; i++ {
  742. intCache[i] = valueInt(i - 128)
  743. }
  744. _positiveZero = intToValue(0)
  745. }