object_goarray_reflect.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package goja
  2. import (
  3. "reflect"
  4. "strconv"
  5. "github.com/dop251/goja/unistring"
  6. )
  7. type objectGoArrayReflect struct {
  8. objectGoReflect
  9. lengthProp valueProperty
  10. valueCache valueArrayCache
  11. putIdx func(idx int, v Value, throw bool) bool
  12. }
  13. type valueArrayCache []reflectValueWrapper
  14. func (c *valueArrayCache) get(idx int) reflectValueWrapper {
  15. if idx < len(*c) {
  16. return (*c)[idx]
  17. }
  18. return nil
  19. }
  20. func (c *valueArrayCache) grow(newlen int) {
  21. oldcap := cap(*c)
  22. if oldcap < newlen {
  23. a := make([]reflectValueWrapper, newlen, growCap(newlen, len(*c), oldcap))
  24. copy(a, *c)
  25. *c = a
  26. } else {
  27. *c = (*c)[:newlen]
  28. }
  29. }
  30. func (c *valueArrayCache) put(idx int, w reflectValueWrapper) {
  31. if len(*c) <= idx {
  32. c.grow(idx + 1)
  33. }
  34. (*c)[idx] = w
  35. }
  36. func (c *valueArrayCache) shrink(newlen int) {
  37. if len(*c) > newlen {
  38. tail := (*c)[newlen:]
  39. for i, item := range tail {
  40. if item != nil {
  41. copyReflectValueWrapper(item)
  42. tail[i] = nil
  43. }
  44. }
  45. *c = (*c)[:newlen]
  46. }
  47. }
  48. func (o *objectGoArrayReflect) _init() {
  49. o.objectGoReflect.init()
  50. o.class = classArray
  51. o.prototype = o.val.runtime.global.ArrayPrototype
  52. o.updateLen()
  53. o.baseObject._put("length", &o.lengthProp)
  54. }
  55. func (o *objectGoArrayReflect) init() {
  56. o._init()
  57. o.putIdx = o._putIdx
  58. }
  59. func (o *objectGoArrayReflect) updateLen() {
  60. o.lengthProp.value = intToValue(int64(o.fieldsValue.Len()))
  61. }
  62. func (o *objectGoArrayReflect) _hasIdx(idx valueInt) bool {
  63. if idx := int64(idx); idx >= 0 && idx < int64(o.fieldsValue.Len()) {
  64. return true
  65. }
  66. return false
  67. }
  68. func (o *objectGoArrayReflect) _hasStr(name unistring.String) bool {
  69. if idx := strToIdx64(name); idx >= 0 && idx < int64(o.fieldsValue.Len()) {
  70. return true
  71. }
  72. return false
  73. }
  74. func (o *objectGoArrayReflect) _getIdx(idx int) Value {
  75. if v := o.valueCache.get(idx); v != nil {
  76. return v.esValue()
  77. }
  78. v := o.fieldsValue.Index(idx)
  79. res, w := o.elemToValue(v)
  80. if w != nil {
  81. o.valueCache.put(idx, w)
  82. }
  83. return res
  84. }
  85. func (o *objectGoArrayReflect) getIdx(idx valueInt, receiver Value) Value {
  86. if idx := toIntStrict(int64(idx)); idx >= 0 && idx < o.fieldsValue.Len() {
  87. return o._getIdx(idx)
  88. }
  89. return o.objectGoReflect.getStr(idx.string(), receiver)
  90. }
  91. func (o *objectGoArrayReflect) getStr(name unistring.String, receiver Value) Value {
  92. var ownProp Value
  93. if idx := strToGoIdx(name); idx >= 0 && idx < o.fieldsValue.Len() {
  94. ownProp = o._getIdx(idx)
  95. } else if name == "length" {
  96. ownProp = &o.lengthProp
  97. } else {
  98. ownProp = o.objectGoReflect.getOwnPropStr(name)
  99. }
  100. return o.getStrWithOwnProp(ownProp, name, receiver)
  101. }
  102. func (o *objectGoArrayReflect) getOwnPropStr(name unistring.String) Value {
  103. if idx := strToGoIdx(name); idx >= 0 {
  104. if idx < o.fieldsValue.Len() {
  105. return &valueProperty{
  106. value: o._getIdx(idx),
  107. writable: true,
  108. enumerable: true,
  109. }
  110. }
  111. return nil
  112. }
  113. if name == "length" {
  114. return &o.lengthProp
  115. }
  116. return o.objectGoReflect.getOwnPropStr(name)
  117. }
  118. func (o *objectGoArrayReflect) getOwnPropIdx(idx valueInt) Value {
  119. if idx := toIntStrict(int64(idx)); idx >= 0 && idx < o.fieldsValue.Len() {
  120. return &valueProperty{
  121. value: o._getIdx(idx),
  122. writable: true,
  123. enumerable: true,
  124. }
  125. }
  126. return nil
  127. }
  128. func (o *objectGoArrayReflect) _putIdx(idx int, v Value, throw bool) bool {
  129. cached := o.valueCache.get(idx)
  130. if cached != nil {
  131. copyReflectValueWrapper(cached)
  132. }
  133. rv := o.fieldsValue.Index(idx)
  134. err := o.val.runtime.toReflectValue(v, rv, &objectExportCtx{})
  135. if err != nil {
  136. if cached != nil {
  137. cached.setReflectValue(rv)
  138. }
  139. o.val.runtime.typeErrorResult(throw, "Go type conversion error: %v", err)
  140. return false
  141. }
  142. if cached != nil {
  143. o.valueCache[idx] = nil
  144. }
  145. return true
  146. }
  147. func (o *objectGoArrayReflect) setOwnIdx(idx valueInt, val Value, throw bool) bool {
  148. if i := toIntStrict(int64(idx)); i >= 0 {
  149. if i >= o.fieldsValue.Len() {
  150. if res, ok := o._setForeignIdx(idx, nil, val, o.val, throw); ok {
  151. return res
  152. }
  153. }
  154. return o.putIdx(i, val, throw)
  155. } else {
  156. name := idx.string()
  157. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
  158. o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
  159. return false
  160. } else {
  161. return res
  162. }
  163. }
  164. }
  165. func (o *objectGoArrayReflect) setOwnStr(name unistring.String, val Value, throw bool) bool {
  166. if idx := strToGoIdx(name); idx >= 0 {
  167. if idx >= o.fieldsValue.Len() {
  168. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); ok {
  169. return res
  170. }
  171. }
  172. return o.putIdx(idx, val, throw)
  173. } else {
  174. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
  175. o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
  176. return false
  177. } else {
  178. return res
  179. }
  180. }
  181. }
  182. func (o *objectGoArrayReflect) setForeignIdx(idx valueInt, val, receiver Value, throw bool) (bool, bool) {
  183. return o._setForeignIdx(idx, trueValIfPresent(o._hasIdx(idx)), val, receiver, throw)
  184. }
  185. func (o *objectGoArrayReflect) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
  186. return o._setForeignStr(name, trueValIfPresent(o.hasOwnPropertyStr(name)), val, receiver, throw)
  187. }
  188. func (o *objectGoArrayReflect) hasOwnPropertyIdx(idx valueInt) bool {
  189. return o._hasIdx(idx)
  190. }
  191. func (o *objectGoArrayReflect) hasOwnPropertyStr(name unistring.String) bool {
  192. if o._hasStr(name) || name == "length" {
  193. return true
  194. }
  195. return o.objectGoReflect._has(name.String())
  196. }
  197. func (o *objectGoArrayReflect) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
  198. if i := toIntStrict(int64(idx)); i >= 0 {
  199. if !o.val.runtime.checkHostObjectPropertyDescr(idx.string(), descr, throw) {
  200. return false
  201. }
  202. val := descr.Value
  203. if val == nil {
  204. val = _undefined
  205. }
  206. return o.putIdx(i, val, throw)
  207. }
  208. o.val.runtime.typeErrorResult(throw, "Cannot define property '%d' on a Go slice", idx)
  209. return false
  210. }
  211. func (o *objectGoArrayReflect) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  212. if idx := strToGoIdx(name); idx >= 0 {
  213. if !o.val.runtime.checkHostObjectPropertyDescr(name, descr, throw) {
  214. return false
  215. }
  216. val := descr.Value
  217. if val == nil {
  218. val = _undefined
  219. }
  220. return o.putIdx(idx, val, throw)
  221. }
  222. o.val.runtime.typeErrorResult(throw, "Cannot define property '%s' on a Go slice", name)
  223. return false
  224. }
  225. func (o *objectGoArrayReflect) toPrimitive() Value {
  226. return o.toPrimitiveString()
  227. }
  228. func (o *objectGoArrayReflect) _deleteIdx(idx int) {
  229. if idx < o.fieldsValue.Len() {
  230. if cv := o.valueCache.get(idx); cv != nil {
  231. copyReflectValueWrapper(cv)
  232. o.valueCache[idx] = nil
  233. }
  234. o.fieldsValue.Index(idx).Set(reflect.Zero(o.fieldsValue.Type().Elem()))
  235. }
  236. }
  237. func (o *objectGoArrayReflect) deleteStr(name unistring.String, throw bool) bool {
  238. if idx := strToGoIdx(name); idx >= 0 {
  239. o._deleteIdx(idx)
  240. return true
  241. }
  242. return o.objectGoReflect.deleteStr(name, throw)
  243. }
  244. func (o *objectGoArrayReflect) deleteIdx(i valueInt, throw bool) bool {
  245. idx := toIntStrict(int64(i))
  246. if idx >= 0 {
  247. o._deleteIdx(idx)
  248. }
  249. return true
  250. }
  251. type goArrayReflectPropIter struct {
  252. o *objectGoArrayReflect
  253. idx, limit int
  254. }
  255. func (i *goArrayReflectPropIter) next() (propIterItem, iterNextFunc) {
  256. if i.idx < i.limit && i.idx < i.o.fieldsValue.Len() {
  257. name := strconv.Itoa(i.idx)
  258. i.idx++
  259. return propIterItem{name: asciiString(name), enumerable: _ENUM_TRUE}, i.next
  260. }
  261. return i.o.objectGoReflect.iterateStringKeys()()
  262. }
  263. func (o *objectGoArrayReflect) stringKeys(all bool, accum []Value) []Value {
  264. for i := 0; i < o.fieldsValue.Len(); i++ {
  265. accum = append(accum, asciiString(strconv.Itoa(i)))
  266. }
  267. return o.objectGoReflect.stringKeys(all, accum)
  268. }
  269. func (o *objectGoArrayReflect) iterateStringKeys() iterNextFunc {
  270. return (&goArrayReflectPropIter{
  271. o: o,
  272. limit: o.fieldsValue.Len(),
  273. }).next
  274. }
  275. func (o *objectGoArrayReflect) sortLen() int {
  276. return o.fieldsValue.Len()
  277. }
  278. func (o *objectGoArrayReflect) sortGet(i int) Value {
  279. return o.getIdx(valueInt(i), nil)
  280. }
  281. func (o *objectGoArrayReflect) swap(i int, j int) {
  282. vi := o.fieldsValue.Index(i)
  283. vj := o.fieldsValue.Index(j)
  284. tmp := reflect.New(o.fieldsValue.Type().Elem()).Elem()
  285. tmp.Set(vi)
  286. vi.Set(vj)
  287. vj.Set(tmp)
  288. cachedI := o.valueCache.get(i)
  289. cachedJ := o.valueCache.get(j)
  290. if cachedI != nil {
  291. cachedI.setReflectValue(vj)
  292. o.valueCache.put(j, cachedI)
  293. } else {
  294. if j < len(o.valueCache) {
  295. o.valueCache[j] = nil
  296. }
  297. }
  298. if cachedJ != nil {
  299. cachedJ.setReflectValue(vi)
  300. o.valueCache.put(i, cachedJ)
  301. } else {
  302. if i < len(o.valueCache) {
  303. o.valueCache[i] = nil
  304. }
  305. }
  306. }