array_sparse.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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) hasPropertyIdx(idx valueInt) bool {
  267. if a.hasOwnPropertyIdx(idx) {
  268. return true
  269. }
  270. if a.prototype != nil {
  271. return a.prototype.self.hasPropertyIdx(idx)
  272. }
  273. return false
  274. }
  275. func (a *sparseArrayObject) expand(idx uint32) bool {
  276. if l := len(a.items); l >= 1024 {
  277. if ii := a.items[l-1].idx; ii > idx {
  278. idx = ii
  279. }
  280. if (bits.UintSize == 64 || idx < math.MaxInt32) && int(idx)>>3 < l {
  281. //log.Println("Switching sparse->standard")
  282. ar := &arrayObject{
  283. baseObject: a.baseObject,
  284. length: a.length,
  285. propValueCount: a.propValueCount,
  286. }
  287. ar.setValuesFromSparse(a.items, int(idx))
  288. ar.val.self = ar
  289. ar.lengthProp.writable = a.lengthProp.writable
  290. a._put("length", &ar.lengthProp)
  291. return false
  292. }
  293. }
  294. return true
  295. }
  296. func (a *sparseArrayObject) _defineIdxProperty(idx uint32, desc PropertyDescriptor, throw bool) bool {
  297. var existing Value
  298. i := a.findIdx(idx)
  299. if i < len(a.items) && a.items[i].idx == idx {
  300. existing = a.items[i].value
  301. }
  302. prop, ok := a.baseObject._defineOwnProperty(unistring.String(strconv.FormatUint(uint64(idx), 10)), existing, desc, throw)
  303. if ok {
  304. if idx >= a.length {
  305. if !a.setLengthInt(idx+1, throw) {
  306. return false
  307. }
  308. }
  309. if i >= len(a.items) || a.items[i].idx != idx {
  310. if a.expand(idx) {
  311. a.items = append(a.items, sparseArrayItem{})
  312. copy(a.items[i+1:], a.items[i:])
  313. a.items[i] = sparseArrayItem{
  314. idx: idx,
  315. value: prop,
  316. }
  317. if idx >= a.length {
  318. a.length = idx + 1
  319. }
  320. } else {
  321. a.val.self.(*arrayObject).values[idx] = prop
  322. }
  323. } else {
  324. a.items[i].value = prop
  325. }
  326. if _, ok := prop.(*valueProperty); ok {
  327. a.propValueCount++
  328. }
  329. }
  330. return ok
  331. }
  332. func (a *sparseArrayObject) defineOwnPropertyStr(name unistring.String, descr PropertyDescriptor, throw bool) bool {
  333. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  334. return a._defineIdxProperty(idx, descr, throw)
  335. }
  336. if name == "length" {
  337. return a.val.runtime.defineArrayLength(a.getLengthProp(), descr, a.setLength, throw)
  338. }
  339. return a.baseObject.defineOwnPropertyStr(name, descr, throw)
  340. }
  341. func (a *sparseArrayObject) defineOwnPropertyIdx(idx valueInt, descr PropertyDescriptor, throw bool) bool {
  342. if idx := toIdx(idx); idx != math.MaxUint32 {
  343. return a._defineIdxProperty(idx, descr, throw)
  344. }
  345. return a.baseObject.defineOwnPropertyStr(idx.string(), descr, throw)
  346. }
  347. func (a *sparseArrayObject) _deleteIdxProp(idx uint32, throw bool) bool {
  348. i := a.findIdx(idx)
  349. if i < len(a.items) && a.items[i].idx == idx {
  350. if p, ok := a.items[i].value.(*valueProperty); ok {
  351. if !p.configurable {
  352. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.toString())
  353. return false
  354. }
  355. a.propValueCount--
  356. }
  357. copy(a.items[i:], a.items[i+1:])
  358. a.items[len(a.items)-1].value = nil
  359. a.items = a.items[:len(a.items)-1]
  360. }
  361. return true
  362. }
  363. func (a *sparseArrayObject) deleteStr(name unistring.String, throw bool) bool {
  364. if idx := strToArrayIdx(name); idx != math.MaxUint32 {
  365. return a._deleteIdxProp(idx, throw)
  366. }
  367. return a.baseObject.deleteStr(name, throw)
  368. }
  369. func (a *sparseArrayObject) deleteIdx(idx valueInt, throw bool) bool {
  370. if idx := toIdx(idx); idx != math.MaxUint32 {
  371. return a._deleteIdxProp(idx, throw)
  372. }
  373. return a.baseObject.deleteStr(idx.string(), throw)
  374. }
  375. func (a *sparseArrayObject) sortLen() int {
  376. if len(a.items) > 0 {
  377. return toIntStrict(int64(a.items[len(a.items)-1].idx) + 1)
  378. }
  379. return 0
  380. }
  381. func (a *sparseArrayObject) export(ctx *objectExportCtx) interface{} {
  382. if v, exists := ctx.get(a.val); exists {
  383. return v
  384. }
  385. arr := make([]interface{}, a.length)
  386. ctx.put(a.val, arr)
  387. var prevIdx uint32
  388. for _, item := range a.items {
  389. idx := item.idx
  390. for i := prevIdx; i < idx; i++ {
  391. if a.prototype != nil {
  392. if v := a.prototype.self.getIdx(valueInt(i), nil); v != nil {
  393. arr[i] = exportValue(v, ctx)
  394. }
  395. }
  396. }
  397. v := item.value
  398. if v != nil {
  399. if prop, ok := v.(*valueProperty); ok {
  400. v = prop.get(a.val)
  401. }
  402. arr[idx] = exportValue(v, ctx)
  403. }
  404. prevIdx = idx + 1
  405. }
  406. for i := prevIdx; i < a.length; i++ {
  407. if a.prototype != nil {
  408. if v := a.prototype.self.getIdx(valueInt(i), nil); v != nil {
  409. arr[i] = exportValue(v, ctx)
  410. }
  411. }
  412. }
  413. return arr
  414. }
  415. func (a *sparseArrayObject) exportType() reflect.Type {
  416. return reflectTypeArray
  417. }
  418. func (a *sparseArrayObject) exportToArrayOrSlice(dst reflect.Value, typ reflect.Type, ctx *objectExportCtx) error {
  419. r := a.val.runtime
  420. if iter := a.getSym(SymIterator, nil); iter == r.getArrayValues() || iter == nil {
  421. l := toIntStrict(int64(a.length))
  422. if typ.Kind() == reflect.Array {
  423. if dst.Len() != l {
  424. return fmt.Errorf("cannot convert an Array into an array, lengths mismatch (have %d, need %d)", l, dst.Len())
  425. }
  426. } else {
  427. dst.Set(reflect.MakeSlice(typ, l, l))
  428. }
  429. ctx.putTyped(a.val, typ, dst.Interface())
  430. for _, item := range a.items {
  431. val := item.value
  432. if p, ok := val.(*valueProperty); ok {
  433. val = p.get(a.val)
  434. }
  435. idx := toIntStrict(int64(item.idx))
  436. if idx >= l {
  437. break
  438. }
  439. err := r.toReflectValue(val, dst.Index(idx), ctx)
  440. if err != nil {
  441. return fmt.Errorf("could not convert array element %v to %v at %d: %w", item.value, typ, idx, err)
  442. }
  443. }
  444. return nil
  445. }
  446. return a.baseObject.exportToArrayOrSlice(dst, typ, ctx)
  447. }