array_sparse.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package goja
  2. import (
  3. "math"
  4. "reflect"
  5. "sort"
  6. "strconv"
  7. )
  8. type sparseArrayItem struct {
  9. idx int64
  10. value Value
  11. }
  12. type sparseArrayObject struct {
  13. baseObject
  14. items []sparseArrayItem
  15. length int64
  16. propValueCount int
  17. lengthProp valueProperty
  18. }
  19. func (a *sparseArrayObject) init() {
  20. a.baseObject.init()
  21. a.lengthProp.writable = true
  22. a._put("length", &a.lengthProp)
  23. }
  24. func (a *sparseArrayObject) getLength() Value {
  25. return intToValue(a.length)
  26. }
  27. func (a *sparseArrayObject) findIdx(idx int64) int {
  28. return sort.Search(len(a.items), func(i int) bool {
  29. return a.items[i].idx >= idx
  30. })
  31. }
  32. func (a *sparseArrayObject) _setLengthInt(l int64, throw bool) bool {
  33. if l >= 0 && l <= math.MaxUint32 {
  34. ret := true
  35. if l <= a.length {
  36. if a.propValueCount > 0 {
  37. // Slow path
  38. for i := len(a.items) - 1; i >= 0; i-- {
  39. item := a.items[i]
  40. if item.idx <= l {
  41. break
  42. }
  43. if prop, ok := item.value.(*valueProperty); ok {
  44. if !prop.configurable {
  45. l = item.idx + 1
  46. ret = false
  47. break
  48. }
  49. a.propValueCount--
  50. }
  51. }
  52. }
  53. }
  54. idx := a.findIdx(l)
  55. aa := a.items[idx:]
  56. for i, _ := range aa {
  57. aa[i].value = nil
  58. }
  59. a.items = a.items[:idx]
  60. a.length = l
  61. if !ret {
  62. a.val.runtime.typeErrorResult(throw, "Cannot redefine property: length")
  63. }
  64. return ret
  65. }
  66. panic(a.val.runtime.newError(a.val.runtime.global.RangeError, "Invalid array length"))
  67. }
  68. func (a *sparseArrayObject) setLengthInt(l int64, throw bool) bool {
  69. if l == a.length {
  70. return true
  71. }
  72. if !a.lengthProp.writable {
  73. a.val.runtime.typeErrorResult(throw, "length is not writable")
  74. return false
  75. }
  76. return a._setLengthInt(l, throw)
  77. }
  78. func (a *sparseArrayObject) setLength(v Value, throw bool) bool {
  79. l, ok := toIntIgnoreNegZero(v)
  80. if ok && l == a.length {
  81. return true
  82. }
  83. if !a.lengthProp.writable {
  84. a.val.runtime.typeErrorResult(throw, "length is not writable")
  85. return false
  86. }
  87. if ok {
  88. return a._setLengthInt(l, throw)
  89. }
  90. panic(a.val.runtime.newError(a.val.runtime.global.RangeError, "Invalid array length"))
  91. }
  92. func (a *sparseArrayObject) getIdx(idx int64, origNameStr string, origName Value) (v Value) {
  93. i := a.findIdx(idx)
  94. if i < len(a.items) && a.items[i].idx == idx {
  95. return a.items[i].value
  96. }
  97. if a.prototype != nil {
  98. if origName != nil {
  99. v = a.prototype.self.getProp(origName)
  100. } else {
  101. v = a.prototype.self.getPropStr(origNameStr)
  102. }
  103. }
  104. return
  105. }
  106. func (a *sparseArrayObject) getProp(n Value) Value {
  107. if idx := toIdx(n); idx >= 0 {
  108. return a.getIdx(idx, "", n)
  109. }
  110. if n.String() == "length" {
  111. return a.getLengthProp()
  112. }
  113. return a.baseObject.getProp(n)
  114. }
  115. func (a *sparseArrayObject) getLengthProp() Value {
  116. a.lengthProp.value = intToValue(a.length)
  117. return &a.lengthProp
  118. }
  119. func (a *sparseArrayObject) getOwnProp(name string) Value {
  120. if idx := strToIdx(name); idx >= 0 {
  121. i := a.findIdx(idx)
  122. if i < len(a.items) && a.items[i].idx == idx {
  123. return a.items[i].value
  124. }
  125. return nil
  126. }
  127. if name == "length" {
  128. return a.getLengthProp()
  129. }
  130. return a.baseObject.getOwnProp(name)
  131. }
  132. func (a *sparseArrayObject) getPropStr(name string) Value {
  133. if i := strToIdx(name); i >= 0 {
  134. return a.getIdx(i, name, nil)
  135. }
  136. if name == "length" {
  137. return a.getLengthProp()
  138. }
  139. return a.baseObject.getPropStr(name)
  140. }
  141. func (a *sparseArrayObject) putIdx(idx int64, val Value, throw bool, origNameStr string, origName Value) {
  142. var prop Value
  143. i := a.findIdx(idx)
  144. if i < len(a.items) && a.items[i].idx == idx {
  145. prop = a.items[i].value
  146. }
  147. if prop == nil {
  148. if a.prototype != nil {
  149. var pprop Value
  150. if origName != nil {
  151. pprop = a.prototype.self.getProp(origName)
  152. } else {
  153. pprop = a.prototype.self.getPropStr(origNameStr)
  154. }
  155. if pprop, ok := pprop.(*valueProperty); ok {
  156. if !pprop.isWritable() {
  157. a.val.runtime.typeErrorResult(throw)
  158. return
  159. }
  160. if pprop.accessor {
  161. pprop.set(a.val, val)
  162. return
  163. }
  164. }
  165. }
  166. if !a.extensible {
  167. a.val.runtime.typeErrorResult(throw)
  168. return
  169. }
  170. if idx >= a.length {
  171. if !a.setLengthInt(idx+1, throw) {
  172. return
  173. }
  174. }
  175. if a.expand() {
  176. a.items = append(a.items, sparseArrayItem{})
  177. copy(a.items[i+1:], a.items[i:])
  178. a.items[i] = sparseArrayItem{
  179. idx: idx,
  180. value: val,
  181. }
  182. } else {
  183. a.val.self.(*arrayObject).putIdx(idx, val, throw, origNameStr, origName)
  184. return
  185. }
  186. } else {
  187. if prop, ok := prop.(*valueProperty); ok {
  188. if !prop.isWritable() {
  189. a.val.runtime.typeErrorResult(throw)
  190. return
  191. }
  192. prop.set(a.val, val)
  193. return
  194. } else {
  195. a.items[i].value = val
  196. }
  197. }
  198. }
  199. func (a *sparseArrayObject) put(n Value, val Value, throw bool) {
  200. if idx := toIdx(n); idx >= 0 {
  201. a.putIdx(idx, val, throw, "", n)
  202. } else {
  203. if n.String() == "length" {
  204. a.setLength(val, throw)
  205. } else {
  206. a.baseObject.put(n, val, throw)
  207. }
  208. }
  209. }
  210. func (a *sparseArrayObject) putStr(name string, val Value, throw bool) {
  211. if idx := strToIdx(name); idx >= 0 {
  212. a.putIdx(idx, val, throw, name, nil)
  213. } else {
  214. if name == "length" {
  215. a.setLength(val, throw)
  216. } else {
  217. a.baseObject.putStr(name, val, throw)
  218. }
  219. }
  220. }
  221. type sparseArrayPropIter struct {
  222. a *sparseArrayObject
  223. recursive bool
  224. idx int
  225. }
  226. func (i *sparseArrayPropIter) next() (propIterItem, iterNextFunc) {
  227. for i.idx < len(i.a.items) {
  228. name := strconv.Itoa(int(i.a.items[i.idx].idx))
  229. prop := i.a.items[i.idx].value
  230. i.idx++
  231. if prop != nil {
  232. return propIterItem{name: name, value: prop}, i.next
  233. }
  234. }
  235. return i.a.baseObject._enumerate(i.recursive)()
  236. }
  237. func (a *sparseArrayObject) _enumerate(recursive bool) iterNextFunc {
  238. return (&sparseArrayPropIter{
  239. a: a,
  240. recursive: recursive,
  241. }).next
  242. }
  243. func (a *sparseArrayObject) enumerate(all, recursive bool) iterNextFunc {
  244. return (&propFilterIter{
  245. wrapped: a._enumerate(recursive),
  246. all: all,
  247. seen: make(map[string]bool),
  248. }).next
  249. }
  250. func (a *sparseArrayObject) setValues(values []Value) {
  251. a.items = nil
  252. for i, val := range values {
  253. if val != nil {
  254. a.items = append(a.items, sparseArrayItem{
  255. idx: int64(i),
  256. value: val,
  257. })
  258. }
  259. }
  260. }
  261. func (a *sparseArrayObject) hasOwnProperty(n Value) bool {
  262. if idx := toIdx(n); idx >= 0 {
  263. i := a.findIdx(idx)
  264. if i < len(a.items) && a.items[i].idx == idx {
  265. return a.items[i].value != _undefined
  266. }
  267. return false
  268. } else {
  269. return a.baseObject.hasOwnProperty(n)
  270. }
  271. }
  272. func (a *sparseArrayObject) hasOwnPropertyStr(name string) bool {
  273. if idx := strToIdx(name); idx >= 0 {
  274. i := a.findIdx(idx)
  275. if i < len(a.items) && a.items[i].idx == idx {
  276. return a.items[i].value != _undefined
  277. }
  278. return false
  279. } else {
  280. return a.baseObject.hasOwnPropertyStr(name)
  281. }
  282. }
  283. func (a *sparseArrayObject) expand() bool {
  284. if l := len(a.items); l >= 1024 {
  285. if int(a.items[l-1].idx)/l < 8 {
  286. //log.Println("Switching sparse->standard")
  287. ar := &arrayObject{
  288. baseObject: a.baseObject,
  289. length: a.length,
  290. propValueCount: a.propValueCount,
  291. }
  292. ar.setValuesFromSparse(a.items)
  293. ar.val.self = ar
  294. ar.init()
  295. ar.lengthProp.writable = a.lengthProp.writable
  296. return false
  297. }
  298. }
  299. return true
  300. }
  301. func (a *sparseArrayObject) defineOwnProperty(n Value, descr propertyDescr, throw bool) bool {
  302. if idx := toIdx(n); idx >= 0 {
  303. var existing Value
  304. i := a.findIdx(idx)
  305. if i < len(a.items) && a.items[i].idx == idx {
  306. existing = a.items[i].value
  307. }
  308. prop, ok := a.baseObject._defineOwnProperty(n, existing, descr, throw)
  309. if ok {
  310. if idx >= a.length {
  311. if !a.setLengthInt(idx+1, throw) {
  312. return false
  313. }
  314. }
  315. if i >= len(a.items) || a.items[i].idx != idx {
  316. if a.expand() {
  317. a.items = append(a.items, sparseArrayItem{})
  318. copy(a.items[i+1:], a.items[i:])
  319. a.items[i] = sparseArrayItem{
  320. idx: idx,
  321. value: prop,
  322. }
  323. if idx >= a.length {
  324. a.length = idx + 1
  325. }
  326. } else {
  327. return a.val.self.defineOwnProperty(n, descr, throw)
  328. }
  329. } else {
  330. a.items[i].value = prop
  331. }
  332. if _, ok := prop.(*valueProperty); ok {
  333. a.propValueCount++
  334. }
  335. }
  336. return ok
  337. } else {
  338. if n.String() == "length" {
  339. return a.val.runtime.defineArrayLength(&a.lengthProp, descr, a.setLength, throw)
  340. }
  341. return a.baseObject.defineOwnProperty(n, descr, throw)
  342. }
  343. }
  344. func (a *sparseArrayObject) _deleteProp(idx int64, throw bool) bool {
  345. i := a.findIdx(idx)
  346. if i < len(a.items) && a.items[i].idx == idx {
  347. if p, ok := a.items[i].value.(*valueProperty); ok {
  348. if !p.configurable {
  349. a.val.runtime.typeErrorResult(throw, "Cannot delete property '%d' of %s", idx, a.val.ToString())
  350. return false
  351. }
  352. a.propValueCount--
  353. }
  354. copy(a.items[i:], a.items[i+1:])
  355. a.items[len(a.items)-1].value = nil
  356. a.items = a.items[:len(a.items)-1]
  357. }
  358. return true
  359. }
  360. func (a *sparseArrayObject) delete(n Value, throw bool) bool {
  361. if idx := toIdx(n); idx >= 0 {
  362. return a._deleteProp(idx, throw)
  363. }
  364. return a.baseObject.delete(n, throw)
  365. }
  366. func (a *sparseArrayObject) deleteStr(name string, throw bool) bool {
  367. if idx := strToIdx(name); idx >= 0 {
  368. return a._deleteProp(idx, throw)
  369. }
  370. return a.baseObject.deleteStr(name, throw)
  371. }
  372. func (a *sparseArrayObject) sortLen() int64 {
  373. if len(a.items) > 0 {
  374. return a.items[len(a.items)-1].idx + 1
  375. }
  376. return 0
  377. }
  378. func (a *sparseArrayObject) sortGet(i int64) Value {
  379. idx := a.findIdx(i)
  380. if idx < len(a.items) && a.items[idx].idx == i {
  381. v := a.items[idx].value
  382. if p, ok := v.(*valueProperty); ok {
  383. v = p.get(a.val)
  384. }
  385. return v
  386. }
  387. return nil
  388. }
  389. func (a *sparseArrayObject) swap(i, j int64) {
  390. idxI := a.findIdx(i)
  391. idxJ := a.findIdx(j)
  392. if idxI < len(a.items) && a.items[idxI].idx == i && idxJ < len(a.items) && a.items[idxJ].idx == j {
  393. a.items[idxI].value, a.items[idxJ].value = a.items[idxJ].value, a.items[idxI].value
  394. }
  395. }
  396. func (a *sparseArrayObject) export() interface{} {
  397. arr := make([]interface{}, a.length)
  398. for _, item := range a.items {
  399. if item.value != nil {
  400. arr[item.idx] = item.value.Export()
  401. }
  402. }
  403. return arr
  404. }
  405. func (a *sparseArrayObject) exportType() reflect.Type {
  406. return reflectTypeArray
  407. }