object_goarray_reflect.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.getArrayPrototype()
  52. o.baseObject._put("length", &o.lengthProp)
  53. }
  54. func (o *objectGoArrayReflect) init() {
  55. o._init()
  56. o.updateLen()
  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. if o.fieldsValue.Kind() == reflect.Slice {
  97. o.updateLen()
  98. }
  99. ownProp = &o.lengthProp
  100. } else {
  101. ownProp = o.objectGoReflect.getOwnPropStr(name)
  102. }
  103. return o.getStrWithOwnProp(ownProp, name, receiver)
  104. }
  105. func (o *objectGoArrayReflect) getOwnPropStr(name unistring.String) Value {
  106. if idx := strToGoIdx(name); idx >= 0 {
  107. if idx < o.fieldsValue.Len() {
  108. return &valueProperty{
  109. value: o._getIdx(idx),
  110. writable: true,
  111. enumerable: true,
  112. }
  113. }
  114. return nil
  115. }
  116. if name == "length" {
  117. if o.fieldsValue.Kind() == reflect.Slice {
  118. o.updateLen()
  119. }
  120. return &o.lengthProp
  121. }
  122. return o.objectGoReflect.getOwnPropStr(name)
  123. }
  124. func (o *objectGoArrayReflect) getOwnPropIdx(idx valueInt) Value {
  125. if idx := toIntStrict(int64(idx)); idx >= 0 && idx < o.fieldsValue.Len() {
  126. return &valueProperty{
  127. value: o._getIdx(idx),
  128. writable: true,
  129. enumerable: true,
  130. }
  131. }
  132. return nil
  133. }
  134. func (o *objectGoArrayReflect) _putIdx(idx int, v Value, throw bool) bool {
  135. cached := o.valueCache.get(idx)
  136. if cached != nil {
  137. copyReflectValueWrapper(cached)
  138. }
  139. rv := o.fieldsValue.Index(idx)
  140. err := o.val.runtime.toReflectValue(v, rv, &objectExportCtx{})
  141. if err != nil {
  142. if cached != nil {
  143. cached.setReflectValue(rv)
  144. }
  145. o.val.runtime.typeErrorResult(throw, "Go type conversion error: %v", err)
  146. return false
  147. }
  148. if cached != nil {
  149. o.valueCache[idx] = nil
  150. }
  151. return true
  152. }
  153. func (o *objectGoArrayReflect) setOwnIdx(idx valueInt, val Value, throw bool) bool {
  154. if i := toIntStrict(int64(idx)); i >= 0 {
  155. if i >= o.fieldsValue.Len() {
  156. if res, ok := o._setForeignIdx(idx, nil, val, o.val, throw); ok {
  157. return res
  158. }
  159. }
  160. return o.putIdx(i, val, throw)
  161. } else {
  162. name := idx.string()
  163. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
  164. o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
  165. return false
  166. } else {
  167. return res
  168. }
  169. }
  170. }
  171. func (o *objectGoArrayReflect) setOwnStr(name unistring.String, val Value, throw bool) bool {
  172. if idx := strToGoIdx(name); idx >= 0 {
  173. if idx >= o.fieldsValue.Len() {
  174. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); ok {
  175. return res
  176. }
  177. }
  178. return o.putIdx(idx, val, throw)
  179. } else {
  180. if res, ok := o._setForeignStr(name, nil, val, o.val, throw); !ok {
  181. o.val.runtime.typeErrorResult(throw, "Can't set property '%s' on Go slice", name)
  182. return false
  183. } else {
  184. return res
  185. }
  186. }
  187. }
  188. func (o *objectGoArrayReflect) setForeignIdx(idx valueInt, val, receiver Value, throw bool) (bool, bool) {
  189. return o._setForeignIdx(idx, trueValIfPresent(o._hasIdx(idx)), val, receiver, throw)
  190. }
  191. func (o *objectGoArrayReflect) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
  192. return o._setForeignStr(name, trueValIfPresent(o.hasOwnPropertyStr(name)), val, receiver, throw)
  193. }
  194. func (o *objectGoArrayReflect) hasOwnPropertyIdx(idx valueInt) bool {
  195. return o._hasIdx(idx)
  196. }
  197. func (o *objectGoArrayReflect) hasOwnPropertyStr(name unistring.String) bool {
  198. if o._hasStr(name) || name == "length" {
  199. return true
  200. }
  201. return o.objectGoReflect.hasOwnPropertyStr(name)
  202. }
  203. func (o *objectGoArrayReflect) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
  204. if i := toIntStrict(int64(idx)); i >= 0 {
  205. if !o.val.runtime.checkHostObjectPropertyDescr(idx.string(), descr, throw) {
  206. return false
  207. }
  208. val := descr.Value
  209. if val == nil {
  210. val = _undefined
  211. }
  212. return o.putIdx(i, val, throw)
  213. }
  214. o.val.runtime.typeErrorResult(throw, "Cannot define property '%d' on a Go slice", idx)
  215. return false
  216. }
  217. func (o *objectGoArrayReflect) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  218. if idx := strToGoIdx(name); idx >= 0 {
  219. if !o.val.runtime.checkHostObjectPropertyDescr(name, descr, throw) {
  220. return false
  221. }
  222. val := descr.Value
  223. if val == nil {
  224. val = _undefined
  225. }
  226. return o.putIdx(idx, val, throw)
  227. }
  228. o.val.runtime.typeErrorResult(throw, "Cannot define property '%s' on a Go slice", name)
  229. return false
  230. }
  231. func (o *objectGoArrayReflect) _deleteIdx(idx int) {
  232. if idx < o.fieldsValue.Len() {
  233. if cv := o.valueCache.get(idx); cv != nil {
  234. copyReflectValueWrapper(cv)
  235. o.valueCache[idx] = nil
  236. }
  237. o.fieldsValue.Index(idx).Set(reflect.Zero(o.fieldsValue.Type().Elem()))
  238. }
  239. }
  240. func (o *objectGoArrayReflect) deleteStr(name unistring.String, throw bool) bool {
  241. if idx := strToGoIdx(name); idx >= 0 {
  242. o._deleteIdx(idx)
  243. return true
  244. }
  245. return o.objectGoReflect.deleteStr(name, throw)
  246. }
  247. func (o *objectGoArrayReflect) deleteIdx(i valueInt, throw bool) bool {
  248. idx := toIntStrict(int64(i))
  249. if idx >= 0 {
  250. o._deleteIdx(idx)
  251. }
  252. return true
  253. }
  254. type goArrayReflectPropIter struct {
  255. o *objectGoArrayReflect
  256. idx, limit int
  257. }
  258. func (i *goArrayReflectPropIter) next() (propIterItem, iterNextFunc) {
  259. if i.idx < i.limit && i.idx < i.o.fieldsValue.Len() {
  260. name := strconv.Itoa(i.idx)
  261. i.idx++
  262. return propIterItem{name: asciiString(name), enumerable: _ENUM_TRUE}, i.next
  263. }
  264. return i.o.objectGoReflect.iterateStringKeys()()
  265. }
  266. func (o *objectGoArrayReflect) stringKeys(all bool, accum []Value) []Value {
  267. for i := 0; i < o.fieldsValue.Len(); i++ {
  268. accum = append(accum, asciiString(strconv.Itoa(i)))
  269. }
  270. return o.objectGoReflect.stringKeys(all, accum)
  271. }
  272. func (o *objectGoArrayReflect) iterateStringKeys() iterNextFunc {
  273. return (&goArrayReflectPropIter{
  274. o: o,
  275. limit: o.fieldsValue.Len(),
  276. }).next
  277. }
  278. func (o *objectGoArrayReflect) sortLen() int {
  279. return o.fieldsValue.Len()
  280. }
  281. func (o *objectGoArrayReflect) sortGet(i int) Value {
  282. return o.getIdx(valueInt(i), nil)
  283. }
  284. func (o *objectGoArrayReflect) swap(i int, j int) {
  285. vi := o.fieldsValue.Index(i)
  286. vj := o.fieldsValue.Index(j)
  287. tmp := reflect.New(o.fieldsValue.Type().Elem()).Elem()
  288. tmp.Set(vi)
  289. vi.Set(vj)
  290. vj.Set(tmp)
  291. cachedI := o.valueCache.get(i)
  292. cachedJ := o.valueCache.get(j)
  293. if cachedI != nil {
  294. cachedI.setReflectValue(vj)
  295. o.valueCache.put(j, cachedI)
  296. } else {
  297. if j < len(o.valueCache) {
  298. o.valueCache[j] = nil
  299. }
  300. }
  301. if cachedJ != nil {
  302. cachedJ.setReflectValue(vi)
  303. o.valueCache.put(i, cachedJ)
  304. } else {
  305. if i < len(o.valueCache) {
  306. o.valueCache[i] = nil
  307. }
  308. }
  309. }