array_sparse.go 12 KB

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