object_gomap.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package goja
  2. import (
  3. "reflect"
  4. "strconv"
  5. )
  6. type objectGoMapSimple struct {
  7. baseObject
  8. data map[string]interface{}
  9. }
  10. func (o *objectGoMapSimple) init() {
  11. o.baseObject.init()
  12. o.prototype = o.val.runtime.global.ObjectPrototype
  13. o.class = classObject
  14. o.extensible = true
  15. }
  16. func (o *objectGoMapSimple) _get(n Value) Value {
  17. return o._getStr(n.String())
  18. }
  19. func (o *objectGoMapSimple) _getStr(name string) Value {
  20. v, exists := o.data[name]
  21. if !exists {
  22. return nil
  23. }
  24. return o.val.runtime.ToValue(v)
  25. }
  26. func (o *objectGoMapSimple) get(n Value) Value {
  27. return o.getStr(n.String())
  28. }
  29. func (o *objectGoMapSimple) getProp(n Value) Value {
  30. return o.getPropStr(n.String())
  31. }
  32. func (o *objectGoMapSimple) getPropStr(name string) Value {
  33. if v := o._getStr(name); v != nil {
  34. return v
  35. }
  36. return o.baseObject.getPropStr(name)
  37. }
  38. func (o *objectGoMapSimple) getStr(name string) Value {
  39. if v := o._getStr(name); v != nil {
  40. return v
  41. }
  42. return o.baseObject._getStr(name)
  43. }
  44. func (o *objectGoMapSimple) getOwnProp(name string) Value {
  45. if v := o._getStr(name); v != nil {
  46. return v
  47. }
  48. return o.baseObject.getOwnProp(name)
  49. }
  50. func (o *objectGoMapSimple) put(n Value, val Value, throw bool) {
  51. o.putStr(n.String(), val, throw)
  52. }
  53. func (o *objectGoMapSimple) _hasStr(name string) bool {
  54. _, exists := o.data[name]
  55. return exists
  56. }
  57. func (o *objectGoMapSimple) _has(n Value) bool {
  58. return o._hasStr(n.String())
  59. }
  60. func (o *objectGoMapSimple) putStr(name string, val Value, throw bool) {
  61. if o.extensible || o._hasStr(name) {
  62. o.data[name] = val.Export()
  63. } else {
  64. o.val.runtime.typeErrorResult(throw, "Host object is not extensible")
  65. }
  66. }
  67. func (o *objectGoMapSimple) hasProperty(n Value) bool {
  68. if o._has(n) {
  69. return true
  70. }
  71. return o.baseObject.hasProperty(n)
  72. }
  73. func (o *objectGoMapSimple) hasPropertyStr(name string) bool {
  74. if o._hasStr(name) {
  75. return true
  76. }
  77. return o.baseObject.hasOwnPropertyStr(name)
  78. }
  79. func (o *objectGoMapSimple) hasOwnProperty(n Value) bool {
  80. return o._has(n)
  81. }
  82. func (o *objectGoMapSimple) hasOwnPropertyStr(name string) bool {
  83. return o._hasStr(name)
  84. }
  85. func (o *objectGoMapSimple) _putProp(name string, value Value, writable, enumerable, configurable bool) Value {
  86. o.putStr(name, value, false)
  87. return value
  88. }
  89. func (o *objectGoMapSimple) defineOwnProperty(name Value, descr propertyDescr, throw bool) bool {
  90. if descr.Getter != nil || descr.Setter != nil {
  91. o.val.runtime.typeErrorResult(throw, "Host objects do not support accessor properties")
  92. return false
  93. }
  94. o.put(name, descr.Value, throw)
  95. return true
  96. }
  97. /*
  98. func (o *objectGoMapSimple) toPrimitiveNumber() Value {
  99. return o.toPrimitiveString()
  100. }
  101. func (o *objectGoMapSimple) toPrimitiveString() Value {
  102. return stringObjectObject
  103. }
  104. func (o *objectGoMapSimple) toPrimitive() Value {
  105. return o.toPrimitiveString()
  106. }
  107. func (o *objectGoMapSimple) assertCallable() (call func(FunctionCall) Value, ok bool) {
  108. return nil, false
  109. }
  110. */
  111. func (o *objectGoMapSimple) deleteStr(name string, throw bool) bool {
  112. delete(o.data, name)
  113. return true
  114. }
  115. func (o *objectGoMapSimple) delete(name Value, throw bool) bool {
  116. return o.deleteStr(name.String(), throw)
  117. }
  118. type gomapPropIter struct {
  119. o *objectGoMapSimple
  120. propNames []string
  121. recursive bool
  122. idx int
  123. }
  124. func (i *gomapPropIter) next() (propIterItem, iterNextFunc) {
  125. for i.idx < len(i.propNames) {
  126. name := i.propNames[i.idx]
  127. i.idx++
  128. if _, exists := i.o.data[name]; exists {
  129. return propIterItem{name: name, enumerable: _ENUM_TRUE}, i.next
  130. }
  131. }
  132. if i.recursive {
  133. return i.o.prototype.self._enumerate(true)()
  134. }
  135. return propIterItem{}, nil
  136. }
  137. func (o *objectGoMapSimple) enumerate(all, recursive bool) iterNextFunc {
  138. return (&propFilterIter{
  139. wrapped: o._enumerate(recursive),
  140. all: all,
  141. seen: make(map[string]bool),
  142. }).next
  143. }
  144. func (o *objectGoMapSimple) _enumerate(recursive bool) iterNextFunc {
  145. propNames := make([]string, len(o.data))
  146. i := 0
  147. for key, _ := range o.data {
  148. propNames[i] = key
  149. i++
  150. }
  151. return (&gomapPropIter{
  152. o: o,
  153. propNames: propNames,
  154. recursive: recursive,
  155. }).next
  156. }
  157. func (o *objectGoMapSimple) export() interface{} {
  158. return o.data
  159. }
  160. func (o *objectGoMapSimple) exportType() reflect.Type {
  161. return reflectTypeMap
  162. }
  163. func (o *objectGoMapSimple) equal(other objectImpl) bool {
  164. if other, ok := other.(*objectGoMapSimple); ok {
  165. return o == other
  166. }
  167. return false
  168. }
  169. func (o *objectGoMapSimple) sortLen() int64 {
  170. return int64(len(o.data))
  171. }
  172. func (o *objectGoMapSimple) sortGet(i int64) Value {
  173. return o.getStr(strconv.FormatInt(i, 10))
  174. }
  175. func (o *objectGoMapSimple) swap(i, j int64) {
  176. ii := strconv.FormatInt(i, 10)
  177. jj := strconv.FormatInt(j, 10)
  178. x := o.getStr(ii)
  179. y := o.getStr(jj)
  180. o.putStr(ii, y, false)
  181. o.putStr(jj, x, false)
  182. }