object_goslice_reflect.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package goja
  2. import (
  3. "math"
  4. "math/bits"
  5. "reflect"
  6. "github.com/dop251/goja/unistring"
  7. )
  8. type objectGoSliceReflect struct {
  9. objectGoArrayReflect
  10. }
  11. func (o *objectGoSliceReflect) init() {
  12. o.objectGoArrayReflect._init()
  13. o.lengthProp.writable = true
  14. o.putIdx = o._putIdx
  15. }
  16. func (o *objectGoSliceReflect) _putIdx(idx int, v Value, throw bool) bool {
  17. if idx >= o.fieldsValue.Len() {
  18. o.grow(idx + 1)
  19. }
  20. return o.objectGoArrayReflect._putIdx(idx, v, throw)
  21. }
  22. func (o *objectGoSliceReflect) grow(size int) {
  23. oldcap := o.fieldsValue.Cap()
  24. if oldcap < size {
  25. n := reflect.MakeSlice(o.fieldsValue.Type(), size, growCap(size, o.fieldsValue.Len(), oldcap))
  26. reflect.Copy(n, o.fieldsValue)
  27. o.fieldsValue.Set(n)
  28. l := len(o.valueCache)
  29. if l > size {
  30. l = size
  31. }
  32. for i, w := range o.valueCache[:l] {
  33. if w != nil {
  34. w.setReflectValue(o.fieldsValue.Index(i))
  35. }
  36. }
  37. } else {
  38. tail := o.fieldsValue.Slice(o.fieldsValue.Len(), size)
  39. zero := reflect.Zero(o.fieldsValue.Type().Elem())
  40. for i := 0; i < tail.Len(); i++ {
  41. tail.Index(i).Set(zero)
  42. }
  43. o.fieldsValue.SetLen(size)
  44. }
  45. }
  46. func (o *objectGoSliceReflect) shrink(size int) {
  47. o.valueCache.shrink(size)
  48. tail := o.fieldsValue.Slice(size, o.fieldsValue.Len())
  49. zero := reflect.Zero(o.fieldsValue.Type().Elem())
  50. for i := 0; i < tail.Len(); i++ {
  51. tail.Index(i).Set(zero)
  52. }
  53. o.fieldsValue.SetLen(size)
  54. }
  55. func (o *objectGoSliceReflect) putLength(v uint32, throw bool) bool {
  56. if bits.UintSize == 32 && v > math.MaxInt32 {
  57. panic(rangeError("Integer value overflows 32-bit int"))
  58. }
  59. newLen := int(v)
  60. curLen := o.fieldsValue.Len()
  61. if newLen > curLen {
  62. o.grow(newLen)
  63. } else if newLen < curLen {
  64. o.shrink(newLen)
  65. }
  66. return true
  67. }
  68. func (o *objectGoSliceReflect) setOwnStr(name unistring.String, val Value, throw bool) bool {
  69. if name == "length" {
  70. return o.putLength(o.val.runtime.toLengthUint32(val), throw)
  71. }
  72. return o.objectGoArrayReflect.setOwnStr(name, val, throw)
  73. }
  74. func (o *objectGoSliceReflect) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  75. if name == "length" {
  76. return o.val.runtime.defineArrayLength(&o.lengthProp, descr, o.putLength, throw)
  77. }
  78. return o.objectGoArrayReflect.defineOwnPropertyStr(name, descr, throw)
  79. }