value.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. package goja
  2. import (
  3. "hash/maphash"
  4. "math"
  5. "math/big"
  6. "reflect"
  7. "strconv"
  8. "unsafe"
  9. "github.com/dop251/goja/ftoa"
  10. "github.com/dop251/goja/unistring"
  11. )
  12. var (
  13. // Not goroutine-safe, do not use for anything other than package level init
  14. pkgHasher maphash.Hash
  15. hashFalse = randomHash()
  16. hashTrue = randomHash()
  17. hashNull = randomHash()
  18. hashUndef = randomHash()
  19. )
  20. // Not goroutine-safe, do not use for anything other than package level init
  21. func randomHash() uint64 {
  22. pkgHasher.WriteByte(0)
  23. return pkgHasher.Sum64()
  24. }
  25. var (
  26. valueFalse Value = valueBool(false)
  27. valueTrue Value = valueBool(true)
  28. _null Value = valueNull{}
  29. _NaN Value = valueFloat(math.NaN())
  30. _positiveInf Value = valueFloat(math.Inf(+1))
  31. _negativeInf Value = valueFloat(math.Inf(-1))
  32. _positiveZero Value = valueInt(0)
  33. negativeZero = math.Float64frombits(0 | (1 << 63))
  34. _negativeZero Value = valueFloat(negativeZero)
  35. _epsilon = valueFloat(2.2204460492503130808472633361816e-16)
  36. _undefined Value = valueUndefined{}
  37. )
  38. var (
  39. reflectTypeInt = reflect.TypeOf(int64(0))
  40. reflectTypeBool = reflect.TypeOf(false)
  41. reflectTypeNil = reflect.TypeOf(nil)
  42. reflectTypeFloat = reflect.TypeOf(float64(0))
  43. reflectTypeBigInt = reflect.TypeOf(big.Int{})
  44. reflectTypeMap = reflect.TypeOf(map[string]interface{}{})
  45. reflectTypeArray = reflect.TypeOf([]interface{}{})
  46. reflectTypeString = reflect.TypeOf("")
  47. )
  48. var intCache [256]Value
  49. type Value interface {
  50. ToInteger() int64
  51. toString() valueString
  52. string() unistring.String
  53. ToString() Value
  54. String() string
  55. ToFloat() float64
  56. ToNumber() Value
  57. ToBigInt() Value
  58. ToBoolean() bool
  59. ToObject(*Runtime) *Object
  60. SameAs(Value) bool
  61. Equals(Value) bool
  62. StrictEquals(Value) bool
  63. Export() interface{}
  64. ExportType() reflect.Type
  65. baseObject(r *Runtime) *Object
  66. hash(hasher *maphash.Hash) uint64
  67. }
  68. type valueContainer interface {
  69. toValue(*Runtime) Value
  70. }
  71. type typeError string
  72. type rangeError string
  73. type referenceError string
  74. type valueInt int64
  75. type valueFloat float64
  76. type valueBigInt struct {
  77. *big.Int
  78. }
  79. type valueBool bool
  80. type valueNull struct{}
  81. type valueUndefined struct {
  82. valueNull
  83. }
  84. // *Symbol is a Value containing ECMAScript Symbol primitive. Symbols must only be created
  85. // using NewSymbol(). Zero values and copying of values (i.e. *s1 = *s2) are not permitted.
  86. // Well-known Symbols can be accessed using Sym* package variables (SymIterator, etc...)
  87. // Symbols can be shared by multiple Runtimes.
  88. type Symbol struct {
  89. h uintptr
  90. desc valueString
  91. }
  92. type valueUnresolved struct {
  93. r *Runtime
  94. ref unistring.String
  95. }
  96. type memberUnresolved struct {
  97. valueUnresolved
  98. }
  99. type valueProperty struct {
  100. value Value
  101. writable bool
  102. configurable bool
  103. enumerable bool
  104. accessor bool
  105. getterFunc *Object
  106. setterFunc *Object
  107. }
  108. var (
  109. errAccessBeforeInit = referenceError("Cannot access a variable before initialization")
  110. errAssignToConst = typeError("Assignment to constant variable.")
  111. )
  112. func propGetter(o Value, v Value, r *Runtime) *Object {
  113. if v == _undefined {
  114. return nil
  115. }
  116. if obj, ok := v.(*Object); ok {
  117. if _, ok := obj.self.assertCallable(); ok {
  118. return obj
  119. }
  120. }
  121. r.typeErrorResult(true, "Getter must be a function: %s", v.toString())
  122. return nil
  123. }
  124. func propSetter(o Value, v Value, r *Runtime) *Object {
  125. if v == _undefined {
  126. return nil
  127. }
  128. if obj, ok := v.(*Object); ok {
  129. if _, ok := obj.self.assertCallable(); ok {
  130. return obj
  131. }
  132. }
  133. r.typeErrorResult(true, "Setter must be a function: %s", v.toString())
  134. return nil
  135. }
  136. func fToStr(num float64, mode ftoa.FToStrMode, prec int) string {
  137. var buf1 [128]byte
  138. return string(ftoa.FToStr(num, mode, prec, buf1[:0]))
  139. }
  140. func (i valueInt) ToInteger() int64 {
  141. return int64(i)
  142. }
  143. func (i valueInt) toString() valueString {
  144. return asciiString(i.String())
  145. }
  146. func (i valueInt) string() unistring.String {
  147. return unistring.String(i.String())
  148. }
  149. func (i valueInt) ToString() Value {
  150. return i
  151. }
  152. func (i valueInt) String() string {
  153. return strconv.FormatInt(int64(i), 10)
  154. }
  155. func (i valueInt) ToFloat() float64 {
  156. return float64(i)
  157. }
  158. func (i valueInt) ToBoolean() bool {
  159. return i != 0
  160. }
  161. func (i valueInt) ToObject(r *Runtime) *Object {
  162. return r.newPrimitiveObject(i, r.global.NumberPrototype, classNumber)
  163. }
  164. func (i valueInt) ToNumber() Value {
  165. return i
  166. }
  167. func (i valueInt) ToBigInt() Value {
  168. return valueBigInt{big.NewInt(int64(i))}
  169. }
  170. func (i valueInt) SameAs(other Value) bool {
  171. return i == other
  172. }
  173. func (i valueInt) Equals(other Value) bool {
  174. switch o := other.(type) {
  175. case valueInt:
  176. return i == o
  177. case valueFloat:
  178. return float64(i) == float64(o)
  179. case valueString:
  180. return o.ToNumber().Equals(i)
  181. case valueBool:
  182. return int64(i) == o.ToInteger()
  183. case *Object:
  184. return i.Equals(o.toPrimitive())
  185. }
  186. return false
  187. }
  188. func (i valueInt) StrictEquals(other Value) bool {
  189. switch o := other.(type) {
  190. case valueInt:
  191. return i == o
  192. case valueFloat:
  193. return float64(i) == float64(o)
  194. }
  195. return false
  196. }
  197. func (i valueInt) baseObject(r *Runtime) *Object {
  198. return r.global.NumberPrototype
  199. }
  200. func (i valueInt) Export() interface{} {
  201. return int64(i)
  202. }
  203. func (i valueInt) ExportType() reflect.Type {
  204. return reflectTypeInt
  205. }
  206. func (i valueInt) hash(*maphash.Hash) uint64 {
  207. return uint64(i)
  208. }
  209. func (b valueBool) ToInteger() int64 {
  210. if b {
  211. return 1
  212. }
  213. return 0
  214. }
  215. func (b valueBool) toString() valueString {
  216. if b {
  217. return stringTrue
  218. }
  219. return stringFalse
  220. }
  221. func (b valueBool) ToString() Value {
  222. return b
  223. }
  224. func (b valueBool) String() string {
  225. if b {
  226. return "true"
  227. }
  228. return "false"
  229. }
  230. func (b valueBool) string() unistring.String {
  231. return unistring.String(b.String())
  232. }
  233. func (b valueBool) ToFloat() float64 {
  234. if b {
  235. return 1.0
  236. }
  237. return 0
  238. }
  239. func (b valueBool) ToBoolean() bool {
  240. return bool(b)
  241. }
  242. func (b valueBool) ToObject(r *Runtime) *Object {
  243. return r.newPrimitiveObject(b, r.global.BooleanPrototype, "Boolean")
  244. }
  245. func (b valueBool) ToNumber() Value {
  246. if b {
  247. return valueInt(1)
  248. }
  249. return valueInt(0)
  250. }
  251. func (b valueBool) ToBigInt() Value {
  252. if b {
  253. return valueBigInt{big.NewInt(1)}
  254. }
  255. return valueBigInt{big.NewInt(0)}
  256. }
  257. func (b valueBool) SameAs(other Value) bool {
  258. if other, ok := other.(valueBool); ok {
  259. return b == other
  260. }
  261. return false
  262. }
  263. func (b valueBool) Equals(other Value) bool {
  264. if o, ok := other.(valueBool); ok {
  265. return b == o
  266. }
  267. if b {
  268. return other.Equals(intToValue(1))
  269. } else {
  270. return other.Equals(intToValue(0))
  271. }
  272. }
  273. func (b valueBool) StrictEquals(other Value) bool {
  274. if other, ok := other.(valueBool); ok {
  275. return b == other
  276. }
  277. return false
  278. }
  279. func (b valueBool) baseObject(r *Runtime) *Object {
  280. return r.global.BooleanPrototype
  281. }
  282. func (b valueBool) Export() interface{} {
  283. return bool(b)
  284. }
  285. func (b valueBool) ExportType() reflect.Type {
  286. return reflectTypeBool
  287. }
  288. func (b valueBool) hash(*maphash.Hash) uint64 {
  289. if b {
  290. return hashTrue
  291. }
  292. return hashFalse
  293. }
  294. func (n valueNull) ToInteger() int64 {
  295. return 0
  296. }
  297. func (n valueNull) toString() valueString {
  298. return stringNull
  299. }
  300. func (n valueNull) string() unistring.String {
  301. return stringNull.string()
  302. }
  303. func (n valueNull) ToString() Value {
  304. return n
  305. }
  306. func (n valueNull) String() string {
  307. return "null"
  308. }
  309. func (u valueUndefined) toString() valueString {
  310. return stringUndefined
  311. }
  312. func (u valueUndefined) ToString() Value {
  313. return u
  314. }
  315. func (u valueUndefined) String() string {
  316. return "undefined"
  317. }
  318. func (u valueUndefined) string() unistring.String {
  319. return "undefined"
  320. }
  321. func (u valueUndefined) ToNumber() Value {
  322. return _NaN
  323. }
  324. func (u valueUndefined) ToBigInt() Value {
  325. panic("RangeError: not a bigint")
  326. }
  327. func (u valueUndefined) SameAs(other Value) bool {
  328. _, same := other.(valueUndefined)
  329. return same
  330. }
  331. func (u valueUndefined) StrictEquals(other Value) bool {
  332. _, same := other.(valueUndefined)
  333. return same
  334. }
  335. func (u valueUndefined) ToFloat() float64 {
  336. return math.NaN()
  337. }
  338. func (u valueUndefined) hash(*maphash.Hash) uint64 {
  339. return hashUndef
  340. }
  341. func (n valueNull) ToFloat() float64 {
  342. return 0
  343. }
  344. func (n valueNull) ToBoolean() bool {
  345. return false
  346. }
  347. func (n valueNull) ToObject(r *Runtime) *Object {
  348. r.typeErrorResult(true, "Cannot convert undefined or null to object")
  349. return nil
  350. //return r.newObject()
  351. }
  352. func (n valueNull) ToNumber() Value {
  353. return intToValue(0)
  354. }
  355. func (n valueNull) ToBigInt() Value {
  356. panic(typeError("Cannot convert null to BigInt"))
  357. }
  358. func (n valueNull) SameAs(other Value) bool {
  359. _, same := other.(valueNull)
  360. return same
  361. }
  362. func (n valueNull) Equals(other Value) bool {
  363. switch other.(type) {
  364. case valueUndefined, valueNull:
  365. return true
  366. }
  367. return false
  368. }
  369. func (n valueNull) StrictEquals(other Value) bool {
  370. _, same := other.(valueNull)
  371. return same
  372. }
  373. func (n valueNull) baseObject(*Runtime) *Object {
  374. return nil
  375. }
  376. func (n valueNull) Export() interface{} {
  377. return nil
  378. }
  379. func (n valueNull) ExportType() reflect.Type {
  380. return reflectTypeNil
  381. }
  382. func (n valueNull) hash(*maphash.Hash) uint64 {
  383. return hashNull
  384. }
  385. func (p *valueProperty) ToInteger() int64 {
  386. return 0
  387. }
  388. func (p *valueProperty) toString() valueString {
  389. return stringEmpty
  390. }
  391. func (p *valueProperty) string() unistring.String {
  392. return ""
  393. }
  394. func (p *valueProperty) ToString() Value {
  395. return _undefined
  396. }
  397. func (p *valueProperty) String() string {
  398. return ""
  399. }
  400. func (p *valueProperty) ToFloat() float64 {
  401. return math.NaN()
  402. }
  403. func (p *valueProperty) ToBoolean() bool {
  404. return false
  405. }
  406. func (p *valueProperty) ToObject(*Runtime) *Object {
  407. return nil
  408. }
  409. func (p *valueProperty) ToNumber() Value {
  410. return nil
  411. }
  412. func (p *valueProperty) ToBigInt() Value {
  413. return nil
  414. }
  415. func (p *valueProperty) isWritable() bool {
  416. return p.writable || p.setterFunc != nil
  417. }
  418. func (p *valueProperty) get(this Value) Value {
  419. if p.getterFunc == nil {
  420. if p.value != nil {
  421. return p.value
  422. }
  423. return _undefined
  424. }
  425. call, _ := p.getterFunc.self.assertCallable()
  426. return call(FunctionCall{
  427. This: this,
  428. })
  429. }
  430. func (p *valueProperty) set(this, v Value) {
  431. if p.setterFunc == nil {
  432. p.value = v
  433. return
  434. }
  435. call, _ := p.setterFunc.self.assertCallable()
  436. call(FunctionCall{
  437. This: this,
  438. Arguments: []Value{v},
  439. })
  440. }
  441. func (p *valueProperty) SameAs(other Value) bool {
  442. if otherProp, ok := other.(*valueProperty); ok {
  443. return p == otherProp
  444. }
  445. return false
  446. }
  447. func (p *valueProperty) Equals(Value) bool {
  448. return false
  449. }
  450. func (p *valueProperty) StrictEquals(Value) bool {
  451. return false
  452. }
  453. func (p *valueProperty) baseObject(r *Runtime) *Object {
  454. r.typeErrorResult(true, "BUG: baseObject() is called on valueProperty") // TODO error message
  455. return nil
  456. }
  457. func (p *valueProperty) Export() interface{} {
  458. panic("Cannot export valueProperty")
  459. }
  460. func (p *valueProperty) ExportType() reflect.Type {
  461. panic("Cannot export valueProperty")
  462. }
  463. func (p *valueProperty) hash(*maphash.Hash) uint64 {
  464. panic("valueProperty should never be used in maps or sets")
  465. }
  466. func floatToIntClip(n float64) int64 {
  467. switch {
  468. case math.IsNaN(n):
  469. return 0
  470. case n >= math.MaxInt64:
  471. return math.MaxInt64
  472. case n <= math.MinInt64:
  473. return math.MinInt64
  474. }
  475. return int64(n)
  476. }
  477. func (f valueFloat) ToInteger() int64 {
  478. return floatToIntClip(float64(f))
  479. }
  480. func (f valueFloat) toString() valueString {
  481. return asciiString(f.String())
  482. }
  483. func (f valueFloat) string() unistring.String {
  484. return unistring.String(f.String())
  485. }
  486. func (f valueFloat) ToString() Value {
  487. return f
  488. }
  489. func (f valueFloat) String() string {
  490. return fToStr(float64(f), ftoa.ModeStandard, 0)
  491. }
  492. func (f valueFloat) ToFloat() float64 {
  493. return float64(f)
  494. }
  495. func (f valueFloat) ToBoolean() bool {
  496. return float64(f) != 0.0 && !math.IsNaN(float64(f))
  497. }
  498. func (f valueFloat) ToObject(r *Runtime) *Object {
  499. return r.newPrimitiveObject(f, r.global.NumberPrototype, "Number")
  500. }
  501. func (f valueFloat) ToNumber() Value {
  502. return f
  503. }
  504. func (f valueFloat) ToBigInt() Value {
  505. i := floatToIntClip(float64(f))
  506. return valueBigInt{big.NewInt(i)}
  507. }
  508. func (f valueFloat) SameAs(other Value) bool {
  509. switch o := other.(type) {
  510. case valueFloat:
  511. this := float64(f)
  512. o1 := float64(o)
  513. if math.IsNaN(this) && math.IsNaN(o1) {
  514. return true
  515. } else {
  516. ret := this == o1
  517. if ret && this == 0 {
  518. ret = math.Signbit(this) == math.Signbit(o1)
  519. }
  520. return ret
  521. }
  522. case valueInt:
  523. this := float64(f)
  524. ret := this == float64(o)
  525. if ret && this == 0 {
  526. ret = !math.Signbit(this)
  527. }
  528. return ret
  529. }
  530. return false
  531. }
  532. func (f valueFloat) Equals(other Value) bool {
  533. switch o := other.(type) {
  534. case valueFloat:
  535. return f == o
  536. case valueInt:
  537. return float64(f) == float64(o)
  538. case valueString, valueBool:
  539. return float64(f) == o.ToFloat()
  540. case *Object:
  541. return f.Equals(o.toPrimitive())
  542. }
  543. return false
  544. }
  545. func (f valueFloat) StrictEquals(other Value) bool {
  546. switch o := other.(type) {
  547. case valueFloat:
  548. return f == o
  549. case valueInt:
  550. return float64(f) == float64(o)
  551. }
  552. return false
  553. }
  554. func (f valueFloat) baseObject(r *Runtime) *Object {
  555. return r.global.NumberPrototype
  556. }
  557. func (f valueFloat) Export() interface{} {
  558. return float64(f)
  559. }
  560. func (f valueFloat) ExportType() reflect.Type {
  561. return reflectTypeFloat
  562. }
  563. func (f valueFloat) hash(*maphash.Hash) uint64 {
  564. if f == _negativeZero {
  565. return 0
  566. }
  567. return math.Float64bits(float64(f))
  568. }
  569. func (b valueBigInt) ToInteger() int64 {
  570. return b.Int64()
  571. }
  572. func (b valueBigInt) toString() valueString {
  573. return asciiString(b.String())
  574. }
  575. func (b valueBigInt) toPrimitiveNumber() Value {
  576. return b
  577. }
  578. func (b valueBigInt) string() unistring.String {
  579. return unistring.String(b.String())
  580. }
  581. func (b valueBigInt) ToString() Value {
  582. return b
  583. }
  584. func (b valueBigInt) String() string {
  585. return b.Int.String()
  586. }
  587. func (b valueBigInt) ToFloat() float64 {
  588. return float64(b.Int64())
  589. }
  590. func (b valueBigInt) ToBoolean() bool {
  591. return b.Int64() != 0
  592. }
  593. func (b valueBigInt) ToObject(r *Runtime) *Object {
  594. return r.newPrimitiveObject(b, r.global.BigIntPrototype, "BigInt")
  595. }
  596. func (b valueBigInt) ToNumber() Value {
  597. return b
  598. }
  599. func (b valueBigInt) ToBigInt() Value {
  600. return b
  601. }
  602. func (b valueBigInt) SameAs(other Value) bool {
  603. v, ok := other.(valueBigInt)
  604. return ok && b.Int.Cmp(v.Int) == 0
  605. return false
  606. }
  607. func (b valueBigInt) Equals(other Value) bool {
  608. switch o := other.(type) {
  609. case valueBigInt:
  610. return b.Cmp(o.Int) == 0
  611. case valueFloat:
  612. return float64(b.Int64()) == float64(o)
  613. case valueInt:
  614. return b.Int64() == int64(o)
  615. case valueString, valueBool:
  616. return b.Int64() == o.ToInteger()
  617. case *Object:
  618. return b.Equals(o.toPrimitive())
  619. }
  620. return false
  621. }
  622. func (b valueBigInt) StrictEquals(other Value) bool {
  623. switch o := other.(type) {
  624. case valueBigInt:
  625. return b.Cmp(o.Int) == 0
  626. }
  627. return false
  628. }
  629. func (b valueBigInt) baseObject(r *Runtime) *Object {
  630. return r.global.BigIntPrototype
  631. }
  632. func (b valueBigInt) Export() interface{} {
  633. return (*big.Int)(b.Int)
  634. }
  635. func (b valueBigInt) ExportType() reflect.Type {
  636. return reflectTypeBigInt
  637. }
  638. func (b valueBigInt) hash(*maphash.Hash) uint64 {
  639. return uint64(b.Int64())
  640. }
  641. func (o *Object) ToInteger() int64 {
  642. return o.toPrimitiveNumber().ToNumber().ToInteger()
  643. }
  644. func (o *Object) toString() valueString {
  645. return o.toPrimitiveString().toString()
  646. }
  647. func (o *Object) string() unistring.String {
  648. return o.toPrimitiveString().string()
  649. }
  650. func (o *Object) ToString() Value {
  651. return o.toPrimitiveString().ToString()
  652. }
  653. func (o *Object) String() string {
  654. return o.toPrimitiveString().String()
  655. }
  656. func (o *Object) ToFloat() float64 {
  657. return o.toPrimitiveNumber().ToFloat()
  658. }
  659. func (o *Object) ToBoolean() bool {
  660. return true
  661. }
  662. func (o *Object) ToObject(*Runtime) *Object {
  663. return o
  664. }
  665. func (o *Object) ToNumber() Value {
  666. return o.toPrimitiveNumber().ToNumber()
  667. }
  668. func (o *Object) ToBigInt() Value {
  669. return o.toPrimitiveBigInt().ToBigInt()
  670. }
  671. func (o *Object) SameAs(other Value) bool {
  672. if other, ok := other.(*Object); ok {
  673. return o == other
  674. }
  675. return false
  676. }
  677. func (o *Object) Equals(other Value) bool {
  678. if other, ok := other.(*Object); ok {
  679. return o == other || o.self.equal(other.self)
  680. }
  681. switch o1 := other.(type) {
  682. case valueInt, valueFloat, valueString, *Symbol:
  683. return o.toPrimitive().Equals(other)
  684. case valueBool:
  685. return o.Equals(o1.ToNumber())
  686. }
  687. return false
  688. }
  689. func (o *Object) StrictEquals(other Value) bool {
  690. if other, ok := other.(*Object); ok {
  691. return o == other || o.self.equal(other.self)
  692. }
  693. return false
  694. }
  695. func (o *Object) baseObject(*Runtime) *Object {
  696. return o
  697. }
  698. // Export the Object to a plain Go type. The returned value will be map[string]interface{} unless
  699. // the Object is a wrapped Go value (created using ToValue()).
  700. // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
  701. func (o *Object) Export() (ret interface{}) {
  702. o.runtime.tryPanic(func() {
  703. ret = o.self.export(&objectExportCtx{})
  704. })
  705. return
  706. }
  707. func (o *Object) ExportType() reflect.Type {
  708. return o.self.exportType()
  709. }
  710. func (o *Object) hash(*maphash.Hash) uint64 {
  711. return o.getId()
  712. }
  713. // Get an object's property by name.
  714. // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
  715. func (o *Object) Get(name string) Value {
  716. return o.self.getStr(unistring.NewFromString(name), nil)
  717. }
  718. // GetSymbol returns the value of a symbol property. Use one of the Sym* values for well-known
  719. // symbols (such as SymIterator, SymToStringTag, etc...).
  720. // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
  721. func (o *Object) GetSymbol(sym *Symbol) Value {
  722. return o.self.getSym(sym, nil)
  723. }
  724. // Keys returns a list of Object's enumerable keys.
  725. // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
  726. func (o *Object) Keys() (keys []string) {
  727. iter := &enumerableIter{
  728. o: o,
  729. wrapped: o.self.iterateStringKeys(),
  730. }
  731. for item, next := iter.next(); next != nil; item, next = next() {
  732. keys = append(keys, item.name.String())
  733. }
  734. return
  735. }
  736. // Symbols returns a list of Object's enumerable symbol properties.
  737. // This method will panic with an *Exception if a JavaScript exception is thrown in the process.
  738. func (o *Object) Symbols() []*Symbol {
  739. symbols := o.self.symbols(false, nil)
  740. ret := make([]*Symbol, len(symbols))
  741. for i, sym := range symbols {
  742. ret[i], _ = sym.(*Symbol)
  743. }
  744. return ret
  745. }
  746. // DefineDataProperty is a Go equivalent of Object.defineProperty(o, name, {value: value, writable: writable,
  747. // configurable: configurable, enumerable: enumerable})
  748. func (o *Object) DefineDataProperty(name string, value Value, writable, configurable, enumerable Flag) error {
  749. return o.runtime.try(func() {
  750. o.self.defineOwnPropertyStr(unistring.NewFromString(name), PropertyDescriptor{
  751. Value: value,
  752. Writable: writable,
  753. Configurable: configurable,
  754. Enumerable: enumerable,
  755. }, true)
  756. })
  757. }
  758. // DefineAccessorProperty is a Go equivalent of Object.defineProperty(o, name, {get: getter, set: setter,
  759. // configurable: configurable, enumerable: enumerable})
  760. func (o *Object) DefineAccessorProperty(name string, getter, setter Value, configurable, enumerable Flag) error {
  761. return o.runtime.try(func() {
  762. o.self.defineOwnPropertyStr(unistring.NewFromString(name), PropertyDescriptor{
  763. Getter: getter,
  764. Setter: setter,
  765. Configurable: configurable,
  766. Enumerable: enumerable,
  767. }, true)
  768. })
  769. }
  770. // DefineDataPropertySymbol is a Go equivalent of Object.defineProperty(o, name, {value: value, writable: writable,
  771. // configurable: configurable, enumerable: enumerable})
  772. func (o *Object) DefineDataPropertySymbol(name *Symbol, value Value, writable, configurable, enumerable Flag) error {
  773. return o.runtime.try(func() {
  774. o.self.defineOwnPropertySym(name, PropertyDescriptor{
  775. Value: value,
  776. Writable: writable,
  777. Configurable: configurable,
  778. Enumerable: enumerable,
  779. }, true)
  780. })
  781. }
  782. // DefineAccessorPropertySymbol is a Go equivalent of Object.defineProperty(o, name, {get: getter, set: setter,
  783. // configurable: configurable, enumerable: enumerable})
  784. func (o *Object) DefineAccessorPropertySymbol(name *Symbol, getter, setter Value, configurable, enumerable Flag) error {
  785. return o.runtime.try(func() {
  786. o.self.defineOwnPropertySym(name, PropertyDescriptor{
  787. Getter: getter,
  788. Setter: setter,
  789. Configurable: configurable,
  790. Enumerable: enumerable,
  791. }, true)
  792. })
  793. }
  794. func (o *Object) Set(name string, value interface{}) error {
  795. return o.runtime.try(func() {
  796. o.self.setOwnStr(unistring.NewFromString(name), o.runtime.ToValue(value), true)
  797. })
  798. }
  799. func (o *Object) SetSymbol(name *Symbol, value interface{}) error {
  800. return o.runtime.try(func() {
  801. o.self.setOwnSym(name, o.runtime.ToValue(value), true)
  802. })
  803. }
  804. func (o *Object) Delete(name string) error {
  805. return o.runtime.try(func() {
  806. o.self.deleteStr(unistring.NewFromString(name), true)
  807. })
  808. }
  809. func (o *Object) DeleteSymbol(name *Symbol) error {
  810. return o.runtime.try(func() {
  811. o.self.deleteSym(name, true)
  812. })
  813. }
  814. // Prototype returns the Object's prototype, same as Object.getPrototypeOf(). If the prototype is null
  815. // returns nil.
  816. func (o *Object) Prototype() *Object {
  817. return o.self.proto()
  818. }
  819. // SetPrototype sets the Object's prototype, same as Object.setPrototypeOf(). Setting proto to nil
  820. // is an equivalent of Object.setPrototypeOf(null).
  821. func (o *Object) SetPrototype(proto *Object) error {
  822. return o.runtime.try(func() {
  823. o.self.setProto(proto, true)
  824. })
  825. }
  826. // MarshalJSON returns JSON representation of the Object. It is equivalent to JSON.stringify(o).
  827. // Note, this implements json.Marshaler so that json.Marshal() can be used without the need to Export().
  828. func (o *Object) MarshalJSON() ([]byte, error) {
  829. ctx := _builtinJSON_stringifyContext{
  830. r: o.runtime,
  831. }
  832. ex := o.runtime.vm.try(func() {
  833. if !ctx.do(o) {
  834. ctx.buf.WriteString("null")
  835. }
  836. })
  837. if ex != nil {
  838. return nil, ex
  839. }
  840. return ctx.buf.Bytes(), nil
  841. }
  842. // ClassName returns the class name
  843. func (o *Object) ClassName() string {
  844. return o.self.className()
  845. }
  846. func (o valueUnresolved) throw() {
  847. o.r.throwReferenceError(o.ref)
  848. }
  849. func (o valueUnresolved) ToInteger() int64 {
  850. o.throw()
  851. return 0
  852. }
  853. func (o valueUnresolved) toString() valueString {
  854. o.throw()
  855. return nil
  856. }
  857. func (o valueUnresolved) string() unistring.String {
  858. o.throw()
  859. return ""
  860. }
  861. func (o valueUnresolved) ToString() Value {
  862. o.throw()
  863. return nil
  864. }
  865. func (o valueUnresolved) String() string {
  866. o.throw()
  867. return ""
  868. }
  869. func (o valueUnresolved) ToFloat() float64 {
  870. o.throw()
  871. return 0
  872. }
  873. func (o valueUnresolved) ToBoolean() bool {
  874. o.throw()
  875. return false
  876. }
  877. func (o valueUnresolved) ToObject(*Runtime) *Object {
  878. o.throw()
  879. return nil
  880. }
  881. func (o valueUnresolved) ToNumber() Value {
  882. o.throw()
  883. return nil
  884. }
  885. func (o valueUnresolved) ToBigInt() Value {
  886. o.throw()
  887. return nil
  888. }
  889. func (o valueUnresolved) SameAs(Value) bool {
  890. o.throw()
  891. return false
  892. }
  893. func (o valueUnresolved) Equals(Value) bool {
  894. o.throw()
  895. return false
  896. }
  897. func (o valueUnresolved) StrictEquals(Value) bool {
  898. o.throw()
  899. return false
  900. }
  901. func (o valueUnresolved) baseObject(*Runtime) *Object {
  902. o.throw()
  903. return nil
  904. }
  905. func (o valueUnresolved) Export() interface{} {
  906. o.throw()
  907. return nil
  908. }
  909. func (o valueUnresolved) ExportType() reflect.Type {
  910. o.throw()
  911. return nil
  912. }
  913. func (o valueUnresolved) hash(*maphash.Hash) uint64 {
  914. o.throw()
  915. return 0
  916. }
  917. func (s *Symbol) ToInteger() int64 {
  918. panic(typeError("Cannot convert a Symbol value to a number"))
  919. }
  920. func (s *Symbol) toString() valueString {
  921. panic(typeError("Cannot convert a Symbol value to a string"))
  922. }
  923. func (s *Symbol) ToString() Value {
  924. return s
  925. }
  926. func (s *Symbol) String() string {
  927. if s.desc != nil {
  928. return s.desc.String()
  929. }
  930. return ""
  931. }
  932. func (s *Symbol) string() unistring.String {
  933. if s.desc != nil {
  934. return s.desc.string()
  935. }
  936. return ""
  937. }
  938. func (s *Symbol) ToFloat() float64 {
  939. panic(typeError("Cannot convert a Symbol value to a number"))
  940. }
  941. func (s *Symbol) ToNumber() Value {
  942. panic(typeError("Cannot convert a Symbol value to a number"))
  943. }
  944. func (s *Symbol) ToBigInt() Value {
  945. panic(typeError("Cannot convert a Symbol value to a bigint"))
  946. }
  947. func (s *Symbol) ToBoolean() bool {
  948. return true
  949. }
  950. func (s *Symbol) ToObject(r *Runtime) *Object {
  951. return s.baseObject(r)
  952. }
  953. func (s *Symbol) SameAs(other Value) bool {
  954. if s1, ok := other.(*Symbol); ok {
  955. return s == s1
  956. }
  957. return false
  958. }
  959. func (s *Symbol) Equals(o Value) bool {
  960. switch o := o.(type) {
  961. case *Object:
  962. return s.Equals(o.toPrimitive())
  963. }
  964. return s.SameAs(o)
  965. }
  966. func (s *Symbol) StrictEquals(o Value) bool {
  967. return s.SameAs(o)
  968. }
  969. func (s *Symbol) Export() interface{} {
  970. return s.String()
  971. }
  972. func (s *Symbol) ExportType() reflect.Type {
  973. return reflectTypeString
  974. }
  975. func (s *Symbol) baseObject(r *Runtime) *Object {
  976. return r.newPrimitiveObject(s, r.global.SymbolPrototype, "Symbol")
  977. }
  978. func (s *Symbol) hash(*maphash.Hash) uint64 {
  979. return uint64(s.h)
  980. }
  981. func exportValue(v Value, ctx *objectExportCtx) interface{} {
  982. if obj, ok := v.(*Object); ok {
  983. return obj.self.export(ctx)
  984. }
  985. return v.Export()
  986. }
  987. func newSymbol(s valueString) *Symbol {
  988. r := &Symbol{
  989. desc: s,
  990. }
  991. // This may need to be reconsidered in the future.
  992. // Depending on changes in Go's allocation policy and/or introduction of a compacting GC
  993. // this may no longer provide sufficient dispersion. The alternative, however, is a globally
  994. // synchronised random generator/hasher/sequencer and I don't want to go down that route just yet.
  995. r.h = uintptr(unsafe.Pointer(r))
  996. return r
  997. }
  998. func NewSymbol(s string) *Symbol {
  999. return newSymbol(newStringValue(s))
  1000. }
  1001. func (s *Symbol) descriptiveString() valueString {
  1002. desc := s.desc
  1003. if desc == nil {
  1004. desc = stringEmpty
  1005. }
  1006. return asciiString("Symbol(").concat(desc).concat(asciiString(")"))
  1007. }
  1008. func funcName(prefix string, n Value) valueString {
  1009. var b valueStringBuilder
  1010. b.WriteString(asciiString(prefix))
  1011. if sym, ok := n.(*Symbol); ok {
  1012. if sym.desc != nil {
  1013. b.WriteRune('[')
  1014. b.WriteString(sym.desc)
  1015. b.WriteRune(']')
  1016. }
  1017. } else {
  1018. b.WriteString(n.toString())
  1019. }
  1020. return b.String()
  1021. }
  1022. func init() {
  1023. for i := 0; i < 256; i++ {
  1024. intCache[i] = valueInt(i - 128)
  1025. }
  1026. _positiveZero = intToValue(0)
  1027. }