object_args.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package goja
  2. type argumentsObject struct {
  3. baseObject
  4. length int
  5. }
  6. type mappedProperty struct {
  7. valueProperty
  8. v *Value
  9. }
  10. func (a *argumentsObject) getPropStr(name string) Value {
  11. if prop, ok := a.values[name].(*mappedProperty); ok {
  12. return *prop.v
  13. }
  14. return a.baseObject.getPropStr(name)
  15. }
  16. func (a *argumentsObject) getProp(n Value) Value {
  17. return a.getPropStr(n.String())
  18. }
  19. func (a *argumentsObject) init() {
  20. a.baseObject.init()
  21. a._putProp("length", intToValue(int64(a.length)), true, false, true)
  22. }
  23. func (a *argumentsObject) put(n Value, val Value, throw bool) {
  24. a.putStr(n.String(), val, throw)
  25. }
  26. func (a *argumentsObject) putStr(name string, val Value, throw bool) {
  27. if prop, ok := a.values[name].(*mappedProperty); ok {
  28. if !prop.writable {
  29. a.val.runtime.typeErrorResult(throw, "Property is not writable: %s", name)
  30. return
  31. }
  32. *prop.v = val
  33. return
  34. }
  35. a.baseObject.putStr(name, val, throw)
  36. }
  37. func (a *argumentsObject) deleteStr(name string, throw bool) bool {
  38. if prop, ok := a.values[name].(*mappedProperty); ok {
  39. if !a.checkDeleteProp(name, &prop.valueProperty, throw) {
  40. return false
  41. }
  42. a._delete(name)
  43. return true
  44. }
  45. return a.baseObject.deleteStr(name, throw)
  46. }
  47. func (a *argumentsObject) delete(n Value, throw bool) bool {
  48. return a.deleteStr(n.String(), throw)
  49. }
  50. type argumentsPropIter1 struct {
  51. a *argumentsObject
  52. idx int
  53. recursive bool
  54. }
  55. type argumentsPropIter struct {
  56. wrapped iterNextFunc
  57. }
  58. func (i *argumentsPropIter) next() (propIterItem, iterNextFunc) {
  59. var item propIterItem
  60. item, i.wrapped = i.wrapped()
  61. if i.wrapped == nil {
  62. return propIterItem{}, nil
  63. }
  64. if prop, ok := item.value.(*mappedProperty); ok {
  65. item.value = *prop.v
  66. }
  67. return item, i.next
  68. }
  69. func (a *argumentsObject) _enumerate(recursive bool) iterNextFunc {
  70. return (&argumentsPropIter{
  71. wrapped: a.baseObject._enumerate(recursive),
  72. }).next
  73. }
  74. func (a *argumentsObject) enumerate(all, recursive bool) iterNextFunc {
  75. return (&argumentsPropIter{
  76. wrapped: a.baseObject.enumerate(all, recursive),
  77. }).next
  78. }
  79. func (a *argumentsObject) defineOwnProperty(n Value, descr propertyDescr, throw bool) bool {
  80. name := n.String()
  81. if mapped, ok := a.values[name].(*mappedProperty); ok {
  82. existing := &valueProperty{
  83. configurable: mapped.configurable,
  84. writable: true,
  85. enumerable: mapped.enumerable,
  86. value: mapped.get(a.val),
  87. }
  88. val, ok := a.baseObject._defineOwnProperty(n, existing, descr, throw)
  89. if !ok {
  90. return false
  91. }
  92. if prop, ok := val.(*valueProperty); ok {
  93. if !prop.accessor {
  94. *mapped.v = prop.value
  95. }
  96. if prop.accessor || !prop.writable {
  97. a._put(name, prop)
  98. return true
  99. }
  100. mapped.configurable = prop.configurable
  101. mapped.enumerable = prop.enumerable
  102. } else {
  103. *mapped.v = val
  104. mapped.configurable = true
  105. mapped.enumerable = true
  106. }
  107. return true
  108. }
  109. return a.baseObject.defineOwnProperty(n, descr, throw)
  110. }
  111. func (a *argumentsObject) getOwnProp(name string) Value {
  112. if mapped, ok := a.values[name].(*mappedProperty); ok {
  113. return *mapped.v
  114. }
  115. return a.baseObject.getOwnProp(name)
  116. }
  117. func (a *argumentsObject) export() interface{} {
  118. arr := make([]interface{}, a.length)
  119. for i, _ := range arr {
  120. v := a.get(intToValue(int64(i)))
  121. if v != nil {
  122. arr[i] = v.Export()
  123. }
  124. }
  125. return arr
  126. }