array_sparse.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. package goja
  2. import (
  3. "fmt"
  4. "math"
  5. "math/bits"
  6. "reflect"
  7. "sort"
  8. "strconv"
  9. "github.com/dop251/goja/unistring"
  10. )
  11. type sparseArrayItem struct {
  12. idx uint32
  13. value Value
  14. }
  15. type sparseArrayObject struct {
  16. baseObject
  17. items []sparseArrayItem
  18. length uint32
  19. propValueCount int
  20. lengthProp valueProperty
  21. }
  22. func (a *sparseArrayObject) findIdx(idx uint32) int {
  23. return sort.Search(len(a.items), func(i int) bool {
  24. return a.items[i].idx >= idx
  25. })
  26. }
  27. func (a *sparseArrayObject) _setLengthInt(l uint32, throw bool) bool {
  28. ret := true
  29. if l <= a.length {
  30. if a.propValueCount > 0 {
  31. // Slow path
  32. for i := len(a.items) - 1; i >= 0; i-- {
  33. item := a.items[i]
  34. if item.idx <= l {
  35. break
  36. }
  37. if prop, ok := item.value.(*valueProperty); ok {
  38. if !prop.configurable {
  39. l = item.idx + 1
  40. ret = false
  41. break
  42. }
  43. a.propValueCount--
  44. }
  45. }
  46. }
  47. }
  48. idx := a.findIdx(l)
  49. aa := a.items[idx:]
  50. for i := range aa {
  51. aa[i].value = nil
  52. }
  53. a.items = a.items[:idx]
  54. a.length = l
  55. if !ret {
  56. a.val.runtime.typeErrorResult(throw, "Cannot redefine property: length")
  57. }
  58. return ret
  59. }
  60. func (a *sparseArrayObject) setLengthInt(l uint32, throw bool) bool {
  61. if l == a.length {
  62. return true
  63. }
  64. if !a.lengthProp.writable {
  65. a.val.runtime.typeErrorResult(throw, "length is not writable")
  66. return false
  67. }
  68. return a._setLengthInt(l, throw)
  69. }
  70. func (a *sparseArrayObject) setLength(v uint32, throw bool) bool {
  71. if v == a.length {
  72. return true
  73. }
  74. if !a.lengthProp.writable {
  75. a.val.runtime.typeErrorResult(throw, "length is not writable")
  76. return false
  77. }
  78. return a._setLengthInt(v, throw)
  79. }
  80. func (a *sparseArrayObject) _getIdx(idx uint32) Value {
  81. i := a.findIdx(idx)
  82. if i < len(a.items) && a.items[i].idx == idx {
  83. return a.items[i].value
  84. }
  85. return nil
  86. }
  87. func (a *sparseArrayObject) getStr(name unistring.String, receiver Value) Value {
  88. return a.getStrWithOwnProp(a.getOwnPropStr(name), name, receiver)
  89. }
  90. func (a *sparseArrayObject) getIdx(idx valueInt, receiver Value) Value {
  91. prop := a.getOwnPropIdx(idx)
  92. if prop == nil {
  93. if a.prototype != nil {
  94. if receiver == nil {
  95. return a.prototype.self.getIdx(idx, a.val)
  96. }
  97. return a.prototype.self.getIdx(idx, receiver)
  98. }
  99. }
  100. if prop, ok := prop.(*valueProperty); ok {
  101. if receiver == nil {
  102. return prop.get(a.val)
  103. }
  104. return prop.get(receiver)
  105. }
  106. return prop
  107. }
  108. func (a *sparseArrayObject) getLengthProp() Value {
  109. a.lengthProp.value = intToValue(int64(a.length))
  110. return &a.lengthProp
  111. }
  112. func (a *sparseArrayObject) getOwnPropStr(name unistring.String) Value {
  113. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  114. return a._getIdx(idx)
  115. }
  116. if name == "length" {
  117. return a.getLengthProp()
  118. }
  119. return a.baseObject.getOwnPropStr(name)
  120. }
  121. func (a *sparseArrayObject) getOwnPropIdx(idx valueInt) Value {
  122. if idx := toIdx(idx); idx != math.MaxUint32 {
  123. return a._getIdx(idx)
  124. }
  125. return a.baseObject.getOwnPropStr(idx.string())
  126. }
  127. func (a *sparseArrayObject) add(idx uint32, val Value) {
  128. i := a.findIdx(idx)
  129. a.items = append(a.items, sparseArrayItem{})
  130. copy(a.items[i+1:], a.items[i:])
  131. a.items[i] = sparseArrayItem{
  132. idx: idx,
  133. value: val,
  134. }
  135. }
  136. func (a *sparseArrayObject) _setOwnIdx(idx uint32, val Value, throw bool) bool {
  137. var prop Value
  138. i := a.findIdx(idx)
  139. if i < len(a.items) && a.items[i].idx == idx {
  140. prop = a.items[i].value
  141. }
  142. if prop == nil {
  143. if proto := a.prototype; proto != nil {
  144. // we know it's foreign because prototype loops are not allowed
  145. if res, ok := proto.self.setForeignIdx(valueInt(idx), val, a.val, throw); ok {
  146. return res
  147. }
  148. }
  149. // new property
  150. if !a.extensible {
  151. a.val.runtime.typeErrorResult(throw, "Cannot add property %d, object is not extensible", idx)
  152. return false
  153. }
  154. if idx >= a.length {
  155. if !a.setLengthInt(idx+1, throw) {
  156. return false
  157. }
  158. }
  159. if a.expand(idx) {
  160. a.items = append(a.items, sparseArrayItem{})
  161. copy(a.items[i+1:], a.items[i:])
  162. a.items[i] = sparseArrayItem{
  163. idx: idx,
  164. value: val,
  165. }
  166. } else {
  167. ar := a.val.self.(*arrayObject)
  168. ar.values[idx] = val
  169. ar.objCount++
  170. return true
  171. }
  172. } else {
  173. if prop, ok := prop.(*valueProperty); ok {
  174. if !prop.isWritable() {
  175. a.val.runtime.typeErrorResult(throw)
  176. return false
  177. }
  178. prop.set(a.val, val)
  179. } else {
  180. a.items[i].value = val
  181. }
  182. }
  183. return true
  184. }
  185. func (a *sparseArrayObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
  186. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  187. return a._setOwnIdx(idx, val, throw)
  188. } else {
  189. if name == "length" {
  190. return a.setLength(a.val.runtime.toLengthUint32(val), throw)
  191. } else {
  192. return a.baseObject.setOwnStr(name, val, throw)
  193. }
  194. }
  195. }
  196. func (a *sparseArrayObject) setOwnIdx(idx valueInt, val Value, throw bool) bool {
  197. if idx := toIdx(idx); idx != math.MaxUint32 {
  198. return a._setOwnIdx(idx, val, throw)
  199. }
  200. return a.baseObject.setOwnStr(idx.string(), val, throw)
  201. }
  202. func (a *sparseArrayObject) setForeignStr(name unistring.String, val, receiver Value, throw bool) (bool, bool) {
  203. return a._setForeignStr(name, a.getOwnPropStr(name), val, receiver, throw)
  204. }
  205. func (a *sparseArrayObject) setForeignIdx(name valueInt, val, receiver Value, throw bool) (bool, bool) {
  206. return a._setForeignIdx(name, a.getOwnPropIdx(name), val, receiver, throw)
  207. }
  208. type sparseArrayPropIter struct {
  209. a *sparseArrayObject
  210. idx int
  211. }
  212. func (i *sparseArrayPropIter) next() (propIterItem, iterNextFunc) {
  213. for i.idx < len(i.a.items) {
  214. name := asciiString(strconv.Itoa(int(i.a.items[i.idx].idx)))
  215. prop := i.a.items[i.idx].value
  216. i.idx++
  217. if prop != nil {
  218. return propIterItem{name: name, value: prop}, i.next
  219. }
  220. }
  221. return i.a.baseObject.iterateStringKeys()()
  222. }
  223. func (a *sparseArrayObject) iterateStringKeys() iterNextFunc {
  224. return (&sparseArrayPropIter{
  225. a: a,
  226. }).next
  227. }
  228. func (a *sparseArrayObject) stringKeys(all bool, accum []Value) []Value {
  229. if all {
  230. for _, item := range a.items {
  231. accum = append(accum, asciiString(strconv.FormatUint(uint64(item.idx), 10)))
  232. }
  233. } else {
  234. for _, item := range a.items {
  235. if prop, ok := item.value.(*valueProperty); ok && !prop.enumerable {
  236. continue
  237. }
  238. accum = append(accum, asciiString(strconv.FormatUint(uint64(item.idx), 10)))
  239. }
  240. }
  241. return a.baseObject.stringKeys(all, accum)
  242. }
  243. func (a *sparseArrayObject) setValues(values []Value, objCount int) {
  244. a.items = make([]sparseArrayItem, 0, objCount)
  245. for i, val := range values {
  246. if val != nil {
  247. a.items = append(a.items, sparseArrayItem{
  248. idx: uint32(i),
  249. value: val,
  250. })
  251. }
  252. }
  253. }
  254. func (a *sparseArrayObject) hasOwnPropertyStr(name unistring.String) bool {
  255. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  256. i := a.findIdx(idx)
  257. return i < len(a.items) && a.items[i].idx == idx
  258. } else {
  259. return a.baseObject.hasOwnPropertyStr(name)
  260. }
  261. }
  262. func (a *sparseArrayObject) hasOwnPropertyIdx(idx valueInt) bool {
  263. if idx := toIdx(idx); idx != math.MaxUint32 {
  264. i := a.findIdx(idx)
  265. return i < len(a.items) && a.items[i].idx == idx
  266. }
  267. return a.baseObject.hasOwnPropertyStr(idx.string())
  268. }
  269. func (a *sparseArrayObject) expand(idx uint32) bool {
  270. if l := len(a.items); l >= 1024 {
  271. if ii := a.items[l-1].idx; ii > idx {
  272. idx = ii
  273. }
  274. if (bits.UintSize == 64 || idx < math.MaxInt32) && int(idx)>>3 < l {
  275. //log.Println("Switching sparse->standard")
  276. ar := &arrayObject{
  277. baseObject: a.baseObject,
  278. length: a.length,
  279. propValueCount: a.propValueCount,
  280. }
  281. ar.setValuesFromSparse(a.items, int(idx))
  282. ar.val.self = ar
  283. ar.lengthProp.writable = a.lengthProp.writable
  284. a._put("length", &ar.lengthProp)
  285. return false
  286. }
  287. }
  288. return true
  289. }
  290. func (a *sparseArrayObject) _defineIdxProperty(idx uint32, desc PropertyDescriptor, throw bool) bool {
  291. var existing Value
  292. i := a.findIdx(idx)
  293. if i < len(a.items) && a.items[i].idx == idx {
  294. existing = a.items[i].value
  295. }
  296. prop, ok := a.baseObject._defineOwnProperty(unistring.String(strconv.FormatUint(uint64(idx), 10)), existing, desc, throw)
  297. if ok {
  298. if idx >= a.length {
  299. if !a.setLengthInt(idx+1, throw) {
  300. return false
  301. }
  302. }
  303. if i >= len(a.items) || a.items[i].idx != idx {
  304. if a.expand(idx) {
  305. a.items = append(a.items, sparseArrayItem{})
  306. copy(a.items[i+1:], a.items[i:])
  307. a.items[i] = sparseArrayItem{
  308. idx: idx,
  309. value: prop,
  310. }
  311. if idx >= a.length {
  312. a.length = idx + 1
  313. }
  314. } else {
  315. a.val.self.(*arrayObject).values[idx] = prop
  316. }
  317. } else {
  318. a.items[i].value = prop
  319. }
  320. if _, ok := prop.(*valueProperty); ok {
  321. a.propValueCount++
  322. }
  323. }
  324. return ok
  325. }
  326. func (a *sparseArrayObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  327. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  328. return a._defineIdxProperty(idx, descr, throw)
  329. }
  330. if name == "length" {
  331. return a.val.runtime.defineArrayLength(&a.lengthProp, descr, a.setLength, throw)
  332. }
  333. return a.baseObject.defineOwnPropertyStr(name, descr, throw)
  334. }
  335. func (a *sparseArrayObject) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
  336. if idx := toIdx(idx); idx != math.MaxUint32 {
  337. return a._defineIdxProperty(idx, descr, throw)
  338. }
  339. return a.baseObject.defineOwnPropertyStr(idx.string(), descr, throw)
  340. }
  341. func (a *sparseArrayObject) _deleteIdxProp(idx uint32, throw bool) bool {
  342. i := a.findIdx(idx)
  343. if i < len(a.items) && a.items[i].idx == idx {
  344. if p, ok := a.items[i].value.(*valueProperty); ok {
  345. if !p.configurable {
  346. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.toString())
  347. return false
  348. }
  349. a.propValueCount--
  350. }
  351. copy(a.items[i:], a.items[i+1:])
  352. a.items[len(a.items)-1].value = nil
  353. a.items = a.items[:len(a.items)-1]
  354. }
  355. return true
  356. }
  357. func (a *sparseArrayObject) deleteStr(name unistring.String, throw bool) bool {
  358. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  359. return a._deleteIdxProp(idx, throw)
  360. }
  361. return a.baseObject.deleteStr(name, throw)
  362. }
  363. func (a *sparseArrayObject) deleteIdx(idx valueInt, throw bool) bool {
  364. if idx := toIdx(idx); idx != math.MaxUint32 {
  365. return a._deleteIdxProp(idx, throw)
  366. }
  367. return a.baseObject.deleteStr(idx.string(), throw)
  368. }
  369. func (a *sparseArrayObject) sortLen() int64 {
  370. if len(a.items) > 0 {
  371. return int64(a.items[len(a.items)-1].idx) + 1
  372. }
  373. return 0
  374. }
  375. func (a *sparseArrayObject) export(ctx *objectExportCtx) interface{} {
  376. if v, exists := ctx.get(a.val); exists {
  377. return v
  378. }
  379. arr := make([]interface{}, a.length)
  380. ctx.put(a.val, arr)
  381. var prevIdx uint32
  382. for _, item := range a.items {
  383. idx := item.idx
  384. for i := prevIdx; i < idx; i++ {
  385. if a.prototype != nil {
  386. if v := a.prototype.self.getIdx(valueInt(i), nil); v != nil {
  387. arr[i] = exportValue(v, ctx)
  388. }
  389. }
  390. }
  391. v := item.value
  392. if v != nil {
  393. if prop, ok := v.(*valueProperty); ok {
  394. v = prop.get(a.val)
  395. }
  396. arr[idx] = exportValue(v, ctx)
  397. }
  398. prevIdx = idx + 1
  399. }
  400. for i := prevIdx; i < a.length; i++ {
  401. if a.prototype != nil {
  402. if v := a.prototype.self.getIdx(valueInt(i), nil); v != nil {
  403. arr[i] = exportValue(v, ctx)
  404. }
  405. }
  406. }
  407. return arr
  408. }
  409. func (a *sparseArrayObject) exportType() reflect.Type {
  410. return reflectTypeArray
  411. }
  412. func (a *sparseArrayObject) exportToArrayOrSlice(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
  413. r := a.val.runtime
  414. if iter := a.getSym(SymIterator, nil); iter == r.global.arrayValues || iter == nil {
  415. l := toIntStrict(int64(a.length))
  416. if dst.Len() != l {
  417. if typ.Kind() == reflect.Array {
  418. return fmt.Errorf("cannot convert an Array into an array, lengths mismatch (have %d, need %d)", l, dst.Len())
  419. } else {
  420. dst.Set(reflect.MakeSlice(typ, l, l))
  421. }
  422. }
  423. ctx.putTyped(a.val, typ, dst.Interface())
  424. for _, item := range a.items {
  425. val := item.value
  426. if p, ok := val.(*valueProperty); ok {
  427. val = p.get(a.val)
  428. }
  429. idx := toIntStrict(int64(item.idx))
  430. if idx >= l {
  431. break
  432. }
  433. err := r.toReflectValue(val, dst.Index(idx), ctx)
  434. if err != nil {
  435. return fmt.Errorf("could not convert array element %v to %v at %d: %w", item.value, typ, idx, err)
  436. }
  437. }
  438. return nil
  439. }
  440. return a.baseObject.exportToArrayOrSlice(dst, typ, ctx)
  441. }