array_sparse.go 9.8 KB

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