object_args.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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) getStr(name string, receiver Value) Value {
  11. return a.getStrWithOwnProp(a.getOwnPropStr(name), name, receiver)
  12. }
  13. func (a *argumentsObject) getOwnPropStr(name string) Value {
  14. if mapped, ok := a.values[name].(*mappedProperty); ok {
  15. return *mapped.v
  16. }
  17. return a.baseObject.getOwnPropStr(name)
  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) setOwnStr(name string, val Value, throw bool) bool {
  24. if prop, ok := a.values[name].(*mappedProperty); ok {
  25. if !prop.writable {
  26. a.val.runtime.typeErrorResult(throw, "Property is not writable: %s", name)
  27. return false
  28. }
  29. *prop.v = val
  30. return true
  31. }
  32. return a.baseObject.setOwnStr(name, val, throw)
  33. }
  34. func (a *argumentsObject) setForeignStr(name string, val, receiver Value, throw bool) (bool, bool) {
  35. return a._setForeignStr(name, a.getOwnPropStr(name), val, receiver, throw)
  36. }
  37. /*func (a *argumentsObject) putStr(name string, val Value, throw bool) {
  38. if prop, ok := a.values[name].(*mappedProperty); ok {
  39. if !prop.writable {
  40. a.val.runtime.typeErrorResult(throw, "Property is not writable: %s", name)
  41. return
  42. }
  43. *prop.v = val
  44. return
  45. }
  46. a.baseObject.putStr(name, val, throw)
  47. }*/
  48. func (a *argumentsObject) deleteStr(name string, throw bool) bool {
  49. if prop, ok := a.values[name].(*mappedProperty); ok {
  50. if !a.checkDeleteProp(name, &prop.valueProperty, throw) {
  51. return false
  52. }
  53. a._delete(name)
  54. return true
  55. }
  56. return a.baseObject.deleteStr(name, throw)
  57. }
  58. type argumentsPropIter struct {
  59. wrapped iterNextFunc
  60. }
  61. func (i *argumentsPropIter) next() (propIterItem, iterNextFunc) {
  62. var item propIterItem
  63. item, i.wrapped = i.wrapped()
  64. if i.wrapped == nil {
  65. return propIterItem{}, nil
  66. }
  67. if prop, ok := item.value.(*mappedProperty); ok {
  68. item.value = *prop.v
  69. }
  70. return item, i.next
  71. }
  72. func (a *argumentsObject) enumerateUnfiltered() iterNextFunc {
  73. return a.recursiveIter((&argumentsPropIter{
  74. wrapped: a.ownIter(),
  75. }).next)
  76. }
  77. func (a *argumentsObject) defineOwnPropertyStr(name string, descr PropertyDescriptor, throw bool) bool {
  78. if mapped, ok := a.values[name].(*mappedProperty); ok {
  79. existing := &valueProperty{
  80. configurable: mapped.configurable,
  81. writable: true,
  82. enumerable: mapped.enumerable,
  83. value: mapped.get(a.val),
  84. }
  85. val, ok := a.baseObject._defineOwnProperty(name, existing, descr, throw)
  86. if !ok {
  87. return false
  88. }
  89. if prop, ok := val.(*valueProperty); ok {
  90. if !prop.accessor {
  91. *mapped.v = prop.value
  92. }
  93. if prop.accessor || !prop.writable {
  94. a._put(name, prop)
  95. return true
  96. }
  97. mapped.configurable = prop.configurable
  98. mapped.enumerable = prop.enumerable
  99. } else {
  100. *mapped.v = val
  101. mapped.configurable = true
  102. mapped.enumerable = true
  103. }
  104. return true
  105. }
  106. return a.baseObject.defineOwnPropertyStr(name, descr, throw)
  107. }
  108. func (a *argumentsObject) export() interface{} {
  109. arr := make([]interface{}, a.length)
  110. for i := range arr {
  111. v := a.getIdx(valueInt(int64(i)), nil)
  112. if v != nil {
  113. arr[i] = v.Export()
  114. }
  115. }
  116. return arr
  117. }